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

Commit

Permalink
Added Spice
Browse files Browse the repository at this point in the history
  • Loading branch information
Getty committed Mar 24, 2012
1 parent 5a13619 commit 2d9a1da
Show file tree
Hide file tree
Showing 9 changed files with 194 additions and 10 deletions.
3 changes: 3 additions & 0 deletions dist.ini
Expand Up @@ -33,6 +33,9 @@ utf8::all = 0.004
File::ShareDir::ProjectDistDir = 0.2.0
Test::More = 0.98
Class::Load = 0.18
Module::Data = 0.001
Path::Class = 0.25
File::ShareDir = 1.03

[Prereqs / TestRequires]
Test::Dirs = 0.03
Expand Down
18 changes: 16 additions & 2 deletions lib/DDG/Meta.pm
Expand Up @@ -6,6 +6,8 @@ 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;

Expand All @@ -25,14 +27,26 @@ sub apply_goodie_keywords {
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( answer => @_ ) });
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 { 'TODO'; });
DDG::Meta::RequestHandler->apply_keywords($target,sub {
shift->spice_new(
scalar @_ == 1 && ref $_[0] eq 'HASH' ? $_[0] :
@_ % 2 ? ( js => @_ ) : ()
);
});
}

1;
42 changes: 42 additions & 0 deletions lib/DDG/Meta/ShareDir.pm
@@ -0,0 +1,42 @@
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;
6 changes: 2 additions & 4 deletions lib/DDG/Meta/ZeroClickInfo.pm
Expand Up @@ -26,7 +26,7 @@ sub check_zeroclickinfo_key {
if (grep { $key eq $_ } zeroclickinfo_attributes) {
return $key;
} else {
croak $key." is not supported on ZeroClickInfo";
croak $key." is not supported on DDG::ZeroClickInfo";
}
}

Expand All @@ -51,9 +51,7 @@ sub apply_keywords {

*{"${target}::zci_new"} = sub {
shift;
ref $_[0] eq 'HASH' ?
DDG::ZeroClickInfo->new(%zci_params, %{$_[0]}) :
DDG::ZeroClickInfo->new(%zci_params, @_)
DDG::ZeroClickInfo->new( %zci_params, ref $_[0] eq 'HASH' ? %{$_[0]} : @_ );
};
*{"${target}::zci"} = sub {
if (ref $_[0] eq 'HASH') {
Expand Down
61 changes: 61 additions & 0 deletions lib/DDG/Meta/ZeroClickInfoSpice.pm
@@ -0,0 +1,61 @@
package DDG::Meta::ZeroClickInfoSpice;

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

sub zeroclickinfospice_attributes {qw(
js
js_include
is_cached
)}

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

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 %zcispice_params = ();
no strict "refs";

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

}

1;
13 changes: 9 additions & 4 deletions lib/DDG/ZeroClickInfo/Spice.pm
Expand Up @@ -2,17 +2,22 @@ package DDG::ZeroClickInfo::Spice;

use Moo;

has js_includes => (
has js => (
is => 'ro',
default => sub {[]},
required => 1,
);

has js => (
has js_root => (
is => 'ro',
required => 1,
);

has is_memcached => (
has js_includes => (
is => 'ro',
default => sub {[]},
);

has is_cached => (
is => 'ro',
default => sub { 0 },
);
Expand Down
30 changes: 30 additions & 0 deletions t/50-spice.t
@@ -0,0 +1,30 @@
#!/usr/bin/env perl

use strict;
use warnings;
use Test::More;

use FindBin qw($Bin);
use lib "$Bin/lib";

use DDGTest::Spice::Words;
use DDGTest::Spice::Regexp;

my $spice = DDGTest::Spice::Words->new( block => undef );

isa_ok($spice,'DDGTest::Spice::Words');

is_deeply(DDGTest::Spice::Words->get_triggers,{
startend => [ "foo", "foofoo", "afoo", "afoofoo" ],
start => [ "bar", "baz", "buu", "abar", "abaz" ],
},'Checking resulting get_triggers of DDGTest::Spice::Words');

my $re = DDGTest::Spice::Regexp->new( block => undef );

isa_ok($re,'DDGTest::Spice::Regexp');

is_deeply(DDGTest::Spice::Regexp->get_triggers,{
query_raw => [qr/aregexp (.*)/i, qr/bregexp (.*) (.*)/i, qr/cregexp (.*)/i]
},'Checking resulting get_triggers of DDGTest::Spice::Regexp',);

done_testing;
11 changes: 11 additions & 0 deletions t/lib/DDGTest/Spice/Regexp.pm
@@ -0,0 +1,11 @@
package DDGTest::Spice::Regexp;

use DDG::Spice;

triggers qr/aregexp (.*)/i;

triggers qr/bregexp (.*) (.*)/i, qr/cregexp (.*)/i;

handle matches => sub { return join('|',@_) };

1;
20 changes: 20 additions & 0 deletions t/lib/DDGTest/Spice/Words.pm
@@ -0,0 +1,20 @@
package DDGTest::Spice::Words;

use DDG::Spice;

triggers startend => 'foo';

triggers start => 'bar', 'baz';
triggers start => 'buu';

triggers startend => 'foofoo';

triggers sub {
start => [qw(abar abaz)],
};

triggers startend => sub { 'afoo', 'afoofoo' };

handle remainder => sub { shift; };

1;

0 comments on commit 2d9a1da

Please sign in to comment.