Skip to content

Instantly share code, notes, and snippets.

@xsawyerx
Created February 24, 2010 12:05
Show Gist options
  • Save xsawyerx/313365 to your computer and use it in GitHub Desktop.
Save xsawyerx/313365 to your computer and use it in GitHub Desktop.
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' );
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 } );
}
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),
);
has '+extra_args' => (
default => sub { {
extract => MusicPref::Extractor->new,
# ...
} },
);
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