File Coverage

blib/lib/Search/GIN/Query/Attributes.pm
Criterion Covered Total %
statement 8 10 80.0
branch n/a
condition n/a
subroutine 4 4 100.0
pod n/a
total 12 14 85.7


line stmt bran cond sub pod time code
1 1     1   34003 use strict;
  1         2  
  1         32  
2 1     1   6 use warnings;
  1         2  
  1         63  
3             package Search::GIN::Query::Attributes;
4             BEGIN {
5 1     1   28 $Search::GIN::Query::Attributes::AUTHORITY = 'cpan:NUFFIN';
6             }
7             # ABSTRACT: Create attributes-based GIN queries
8             $Search::GIN::Query::Attributes::VERSION = '0.09';
9 1     1   463 use Moose;
  0            
  0            
10             use Carp qw(croak);
11             use namespace::clean -except => [qw(meta)];
12              
13             with qw(
14             Search::GIN::Query
15             Search::GIN::Keys::Deep
16             );
17              
18             has attributes => (
19             isa => "HashRef",
20             is => "rw",
21             required => 1,
22             );
23              
24             has compare => (
25             isa => "Str|CodeRef",
26             is => "rw",
27             default => "compare_naive",
28             );
29              
30             sub extract_values {
31             my $self = shift;
32              
33             return (
34             method => "all",
35             values => [ $self->process_keys($self->attributes) ],
36             );
37             }
38              
39             sub consistent {
40             my ( $self, $index, $obj ) = @_;
41              
42             my $class = ref $obj;
43              
44             my $meta = Class::MOP::get_metaclass_by_name($class);
45              
46             my $query = $self->attributes;
47              
48             my %got;
49              
50             foreach my $attr_name ( keys %$query ) {
51             my $expected = $query->{$attr_name};
52              
53             my $meta_attr = $meta->find_attribute_by_name($attr_name) || return;
54             $got{$attr_name} = $meta_attr->get_value($obj);
55             }
56              
57             my $cmp = $self->compare;
58              
59             return $self->$cmp( \%got, $query );
60             }
61              
62             sub compare_naive {
63             my ( $self, $got, $exp ) = @_;
64              
65             return unless keys %$got == keys %$exp;
66              
67             foreach my $key ( keys %$exp ) {
68             return unless overload::StrVal($got->{$key}) eq overload::StrVal($exp->{$key});
69             }
70              
71             return 1;
72             }
73              
74             sub compare_test_deep {
75             my ( $self, $got, $exp ) = @_;
76              
77             require Test::Deep::NoTest;
78             Test::Deep::NoTest::eq_deeply($got, $exp);
79             }
80              
81             # FIXME Data::Compare too
82              
83             __PACKAGE__->meta->make_immutable;
84              
85             1;
86              
87             __END__
88              
89             =pod
90              
91             =encoding UTF-8
92              
93             =head1 NAME
94              
95             Search::GIN::Query::Attributes - Create attributes-based GIN queries
96              
97             =head1 VERSION
98              
99             version 0.09
100              
101             =head1 SYNOPSIS
102              
103             use Search::GIN::Query::Attributes;
104              
105             my $query = Search::GIN::Query::Attributes->new(
106             attributes => {
107             name => 'Homer',
108             },
109             );
110              
111             =head1 DESCRIPTION
112              
113             Creates an attributes-based GIN query that can be used to search records in a
114             storage.
115              
116             This is a ready-to-use query that uses an object's attributes to search through
117             the storage.
118              
119             =head1 METHODS/SUBROUTINES
120              
121             =head2 new
122              
123             Creates a new query.
124              
125             =head1 ATTRIBUTES
126              
127             =head2 attributes
128              
129             Attributes of the object you want to find.
130              
131             my $query = Search::GIN::Query::Attributes->new(
132             attributes => {
133             name => 'Homer',
134             city => 'Springfield',
135             },
136             );
137              
138             =head1 AUTHOR
139              
140             יובל קוג'מן (Yuval Kogman) <nothingmuch@woobling.org>
141              
142             =head1 COPYRIGHT AND LICENSE
143              
144             This software is copyright (c) 2008 by יובל קוג'מן (Yuval Kogman), Infinity Interactive.
145              
146             This is free software; you can redistribute it and/or modify it under
147             the same terms as the Perl 5 programming language system itself.
148              
149             =cut