Created
February 24, 2010 12:05
-
-
Save xsawyerx/313365 to your computer and use it in GitHub Desktop.
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
package MusicPref; | |
use Moose; | |
use namespace::autoclean; | |
has 'name' => ( is => 'ro', isa => 'Str' ); | |
package Character; | |
use Moose; | |
use namespace::autoclean; | |
has 'name' => ( is => 'ro', isa => 'Str' ); | |
has 'music_prefs' => ( is => 'rw', does => 'KiokuDB::Set' ); |
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
package MusicPref::Extractor; | |
use Moose; | |
use namespace::autoclean; | |
with qw( | |
Search::GIN::Extract | |
Search::GIN::Keys::Deep | |
); | |
sub extract_values { | |
my ( $self, $obj, @args ) = @_; | |
ref $obj eq 'Character' or return; | |
my $set = $obj->members || return; | |
my @prefs = scalar $set->members > 0 ? | |
map { $_->music_prefs } $set->members : | |
(); | |
$self->process_keys( { music_pref => \@prefs } ); | |
} | |
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
use MusicPref; | |
use Character; | |
use KiokuDB::Util 'set'; | |
# the music preferences | |
my $jazz = MusicPref->new( name => 'Jazz' ); | |
my $blues = MusicPref->new( name => 'Blues' ); | |
my $jingles = MusicPref->new( name => 'Commercial Jingles' ); | |
# the characters themselves | |
my $lisa = Character->new( | |
name => 'Lisa Simpson', | |
music_prefs => set( $jazz, $blues ), | |
); | |
my $barney = Character->new( | |
name => 'Barney Gumble', | |
music_prefs => set($blues), | |
); | |
my $homer = Character->new( | |
name => 'Homer Simpson', | |
music_prefs => set($jingles), | |
); |
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
has '+extra_args' => ( | |
default => sub { { | |
extract => MusicPref::Extractor->new, | |
# ... | |
} }, | |
); |
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
use Search::GIN::Query::Manual; | |
my $query = Search::GIN::Query::Manual->new( | |
values => { music_pref => 'blues' }, | |
); | |
my @characters = $db->search($query)->all; # Lisa, Barney objects |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment