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