This repository has been archived by the owner on Oct 15, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 236
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Lots of finetuning, abstractin of ddg_goodie_test and ddg_spice_test …
…to block_test
- Loading branch information
Showing
8 changed files
with
378 additions
and
295 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
Oops, something went wrong.