File Coverage

blib/lib/Search/GIN/Query/Attributes.pm
Criterion Covered Total %
statement 33 36 91.6
branch 2 4 50.0
condition 1 2 50.0
subroutine 8 9 88.8
pod 0 4 0.0
total 44 55 80.0


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