File Coverage

blib/lib/Search/GIN/Query/Class.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 2     2   57625 use strict;
  2         5  
  2         61  
2 2     2   11 use warnings;
  2         4  
  2         123  
3             package Search::GIN::Query::Class;
4             BEGIN {
5 2     2   49 $Search::GIN::Query::Class::AUTHORITY = 'cpan:NUFFIN';
6             }
7             # ABSTRACT: Create class-based GIN queries
8             $Search::GIN::Query::Class::VERSION = '0.09';
9 2     2   1045 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 no_check => (
19             isa => "Bool",
20             is => "rw",
21             default => 0,
22             );
23              
24             has class => (
25             isa => "ArrayRef | Str",
26             is => "ro",
27             predicate => "has_class",
28             );
29              
30             has does => (
31             isa => "ArrayRef | Str",
32             is => "ro",
33             predicate => "has_does",
34             );
35              
36             has blessed => (
37             isa => "Str",
38             is => "ro",
39             predicate => "has_blessed",
40             );
41              
42             sub BUILD {
43             my $self = shift;
44              
45             croak "One of 'class', 'does', or 'blessed' is required"
46             unless $self->has_class or $self->has_does or $self->has_blessed;
47             }
48              
49             sub extract_values {
50             my $self = shift;
51              
52             return (
53             method => "all",
54             values => [ $self->process_keys({
55             ( $self->has_class ? ( class => $self->class ) : () ),
56             ( $self->has_does ? ( does => $self->does ) : () ),
57             ( $self->has_blessed ? ( blessed => $self->blessed ) : () ),
58             }) ],
59             );
60             }
61              
62             sub consistent {
63             my ( $self, $index, $object ) = @_;
64              
65             return 1 if $self->no_check;
66              
67             if ( $self->has_blessed ) {
68             return unless ref($object) eq $self->blessed;
69             }
70              
71             if ( $self->has_class ) {
72             return unless $self->check_object($object, isa => $self->class);
73             }
74              
75             if ( $self->has_does ) {
76             return unless $self->check_object($object, DOES => $self->does);
77             }
78              
79             return 1;
80             }
81              
82             sub check_object {
83             my ( $self, $object, $check, $classes ) = @_;
84              
85             my @classes = ref($classes) ? @$classes : $classes;
86              
87             foreach my $class ( @classes ) {
88             $object->$check($class) or return;
89             }
90              
91             return 1;
92             }
93              
94             __PACKAGE__->meta->make_immutable;
95              
96             1;
97              
98             __END__
99              
100             =pod
101              
102             =encoding UTF-8
103              
104             =head1 NAME
105              
106             Search::GIN::Query::Class - Create class-based GIN queries
107              
108             =head1 VERSION
109              
110             version 0.09
111              
112             =head1 SYNOPSIS
113              
114             use Search::GIN::Query::Class;
115              
116             my $query = Search::GIN::Query::Class->new(
117             class => 'Person',
118             );
119              
120             =head1 DESCRIPTION
121              
122             Creates a class-based GIN query that can be used to search records in a storage.
123              
124             This is a ready-to-use query that uses class definitions (specifically C<class>,
125             C<does> and C<blessed>) to search through the storage.
126              
127             =head1 METHODS/SUBROUTINES
128              
129             =head2 new
130              
131             Creates a new query.
132              
133             =head1 ATTRIBUTES
134              
135             =head2 class
136              
137             The class of the object you want to find.
138              
139             my $query = Search::GIN::Query::Class->new(
140             class => 'Person',
141             );
142              
143             =head2 does
144              
145             A role consumed by the object you want to find.
146              
147             my $query = Search::GIN::Query::Class->new(
148             does => 'TheMonkey',
149             );
150              
151             =head2 blessed
152              
153             The name of the package that the object is blessed into.
154              
155             my $query = Search::GIN::Query::Class->new(
156             blessed => 'Person',
157             );
158              
159             =head1 AUTHOR
160              
161             יובל קוג'מן (Yuval Kogman) <nothingmuch@woobling.org>
162              
163             =head1 COPYRIGHT AND LICENSE
164              
165             This software is copyright (c) 2008 by יובל קוג'מן (Yuval Kogman), Infinity Interactive.
166              
167             This is free software; you can redistribute it and/or modify it under
168             the same terms as the Perl 5 programming language system itself.
169              
170             =cut