Skip to content
This repository has been archived by the owner on Oct 15, 2022. It is now read-only.

Commit

Permalink
Lots of finetuning, abstractin of ddg_goodie_test and ddg_spice_test …
Browse files Browse the repository at this point in the history
…to block_test
  • Loading branch information
Getty committed Mar 25, 2012
1 parent b3106d2 commit c4a052c
Show file tree
Hide file tree
Showing 8 changed files with 378 additions and 295 deletions.
102 changes: 51 additions & 51 deletions lib/DDG/Meta.pm
Original file line number Diff line number Diff line change
@@ -1,52 +1,52 @@
package DDG::Meta;

use strict;
use warnings;
use Carp;

use DDG::Meta::RequestHandler;
use DDG::Meta::ZeroClickInfo;
use DDG::Meta::ZeroClickInfoSpice;
use DDG::Meta::ShareDir;
use DDG::Meta::Block;
require Moo::Role;

sub apply_base_to_package {
my ( $class, $target ) = @_;

eval qq{
package $target;
use Moo;
use Data::Printer;
use utf8::all;
};
}

sub apply_goodie_keywords {
my ( $class, $target ) = @_;
DDG::Meta::ZeroClickInfo->apply_keywords($target);
DDG::Meta::Block->apply_keywords($target);
Moo::Role->apply_role_to_package($target,'DDG::Block::Blockable');
DDG::Meta::RequestHandler->apply_keywords($target,sub {
shift->zci_new(
scalar @_ == 1 && ref $_[0] eq 'HASH' ? $_[0] :
@_ % 2 ? ( answer => @_ ) : ()
);
});
}

sub apply_spice_keywords {
my ( $class, $target ) = @_;
DDG::Meta::ZeroClickInfoSpice->apply_keywords($target);
DDG::Meta::ShareDir->apply_keywords($target);
DDG::Meta::Block->apply_keywords($target);
Moo::Role->apply_role_to_package($target,'DDG::Block::Blockable');
DDG::Meta::RequestHandler->apply_keywords($target,sub {
shift->spice_new(
scalar @_ == 1 && ref $_[0] eq 'HASH' ? $_[0] :
@_ % 2 ? ( js => @_ ) : ()
);
});
}

package DDG::Meta;

use strict;
use warnings;
use Carp;

use DDG::Meta::RequestHandler;
use DDG::Meta::ZeroClickInfo;
use DDG::Meta::ZeroClickInfoSpice;
use DDG::Meta::ShareDir;
use DDG::Meta::Block;
require Moo::Role;

sub apply_base_to_package {
my ( $class, $target ) = @_;

eval qq{
package $target;
use Moo;
use Data::Printer;
use utf8::all;
};
}

sub apply_goodie_keywords {
my ( $class, $target ) = @_;
DDG::Meta::ZeroClickInfo->apply_keywords($target);
DDG::Meta::Block->apply_keywords($target);
Moo::Role->apply_role_to_package($target,'DDG::Block::Blockable');
DDG::Meta::RequestHandler->apply_keywords($target,sub {
shift->zci_new(
scalar @_ == 1 && ref $_[0] eq 'HASH' ? $_[0] :
@_ % 2 ? ( answer => @_ ) : @_
);
});
}

sub apply_spice_keywords {
my ( $class, $target ) = @_;
DDG::Meta::ZeroClickInfoSpice->apply_keywords($target);
DDG::Meta::ShareDir->apply_keywords($target);
DDG::Meta::Block->apply_keywords($target);
Moo::Role->apply_role_to_package($target,'DDG::Block::Blockable');
DDG::Meta::RequestHandler->apply_keywords($target,sub {
shift->spice_new(
scalar @_ == 1 && ref $_[0] eq 'HASH' ? $_[0] :
@_ % 2 ? ( call => @_ ) : @_
);
});
}

1;
86 changes: 44 additions & 42 deletions lib/DDG/Meta/ShareDir.pm
Original file line number Diff line number Diff line change
@@ -1,42 +1,44 @@
package DDG::Meta::ShareDir;

use strict;
use warnings;
use Carp;
use Module::Data;
use Path::Class;
use File::ShareDir ':ALL';

my %applied;

sub apply_keywords {
my ( $class, $target ) = @_;

return if exists $applied{$target};
$applied{$target} = undef;

my @parts = split('::',$target);
shift @parts;
my $share_path = join('/',map { s/([a-z])([A-Z])/$1_$2/; lc; } @parts);

my $moddata = Module::Data->new($target);

my $share;

if ( -e $moddata->root->parent->subdir('lib') ) {
return unless -e $moddata->root->parent->subdir('share');
$share = dir($moddata->root->parent->subdir('share'),$share_path);
} else {
$share = dir(module_dir($target));
}

{
no strict "refs";

*{"${target}::module_share_dir"} = sub { dir('share',$share_path) };
*{"${target}::share"} = sub { $share };
}

}

1;
package DDG::Meta::ShareDir;

use strict;
use warnings;
use Carp;
use Module::Data;
use Path::Class;
use File::ShareDir ':ALL';

my %applied;

sub apply_keywords {
my ( $class, $target ) = @_;

return if exists $applied{$target};
$applied{$target} = undef;

my @parts = split('::',$target);
shift @parts;
my $share_path = join('/',map { s/([a-z])([A-Z])/$1_$2/; lc; } @parts);

my $moddata = Module::Data->new($target);

my $share;

if ( -e $moddata->root->parent->subdir('lib') ) {
return unless -e $moddata->root->parent->subdir('share');
$share = dir($moddata->root->parent->subdir('share'),$share_path);
} else {
$share = dir(module_dir($target));
}

{
no strict "refs";

*{"${target}::module_share_dir"} = sub { dir('share',$share_path) };
*{"${target}::share"} = sub {
@_ ? -d dir($share,@_) ? $share->subdir(@_) : $share->file(@_) : $share
};
}

}

1;
147 changes: 74 additions & 73 deletions lib/DDG/Meta/ZeroClickInfo.pm
Original file line number Diff line number Diff line change
@@ -1,73 +1,74 @@
package DDG::Meta::ZeroClickInfo;

use strict;
use warnings;
use Carp;
use DDG::ZeroClickInfo;

sub zeroclickinfo_attributes {qw(
abstract
abstract_text
abstract_source
abstract_url
image
heading
answer
answer_type
definition
definition_source
definition_url
type
is_cached
)}

sub check_zeroclickinfo_key {
my $key = shift;
if (grep { $key eq $_ } zeroclickinfo_attributes) {
return $key;
} else {
croak $key." is not supported on DDG::ZeroClickInfo";
}
}

my %applied;

sub apply_keywords {
my ( $class, $target ) = @_;

return if exists $applied{$target};
$applied{$target} = undef;

my @parts = split('::',$target);
shift @parts;
shift @parts;
my $answer_type = lc(join(' ',@parts));

{
my %zci_params = (
answer_type => $answer_type,
);
no strict "refs";

*{"${target}::zci_new"} = sub {
shift;
DDG::ZeroClickInfo->new( %zci_params, ref $_[0] eq 'HASH' ? %{$_[0]} : @_ );
};
*{"${target}::zci"} = sub {
if (ref $_[0] eq 'HASH') {
for (keys %{$_[0]}) {
$zci_params{check_zeroclickinfo_key($_)} = $_[0]->{$_};
}
} else {
while (@_) {
my $key = shift;
my $value = shift;
$zci_params{check_zeroclickinfo_key($key)} = $value;
}
}
};
}

}

1;
package DDG::Meta::ZeroClickInfo;

use strict;
use warnings;
use Carp;
use DDG::ZeroClickInfo;

sub zeroclickinfo_attributes {qw(
abstract
abstract_text
abstract_source
abstract_url
image
heading
answer
answer_type
definition
definition_source
definition_url
type
is_cached
ttl
)}

sub check_zeroclickinfo_key {
my $key = shift;
if (grep { $key eq $_ } zeroclickinfo_attributes) {
return $key;
} else {
croak $key." is not supported on DDG::ZeroClickInfo";
}
}

my %applied;

sub apply_keywords {
my ( $class, $target ) = @_;

return if exists $applied{$target};
$applied{$target} = undef;

my @parts = split('::',$target);
shift @parts;
shift @parts;
my $answer_type = lc(join(' ',@parts));

{
my %zci_params = (
answer_type => $answer_type,
);
no strict "refs";

*{"${target}::zci_new"} = sub {
shift;
DDG::ZeroClickInfo->new( %zci_params, ref $_[0] eq 'HASH' ? %{$_[0]} : @_ );
};
*{"${target}::zci"} = sub {
if (ref $_[0] eq 'HASH') {
for (keys %{$_[0]}) {
$zci_params{check_zeroclickinfo_key($_)} = $_[0]->{$_};
}
} else {
while (@_) {
my $key = shift;
my $value = shift;
$zci_params{check_zeroclickinfo_key($key)} = $value;
}
}
};
}

}

1;
Loading

0 comments on commit c4a052c

Please sign in to comment.