File Coverage

blib/lib/Data/Seek.pm
Criterion Covered Total %
statement 33 34 97.0
branch 7 8 87.5
condition 2 3 66.6
subroutine 7 7 100.0
pod 1 1 100.0
total 50 53 94.3


line stmt bran cond sub pod time code
1             # ABSTRACT: Search Complex Data Structures
2             package Data::Seek;
3              
4 1     1   1004 use 5.10.0;
  1         4  
  1         54  
5 1     1   6 use strict;
  1         1  
  1         38  
6 1     1   15 use warnings;
  1         2  
  1         34  
7              
8 1     1   792 use Data::Seek::Data;
  1         3  
  1         34  
9 1     1   865 use Data::Seek::Search;
  1         3  
  1         30  
10              
11 1     1   6 use Mo 'default';
  1         2  
  1         4  
12              
13             our $VERSION = '0.05'; # VERSION
14              
15             has 'cache',
16             default => 0;
17              
18             has 'data',
19             default => sub {{}};
20              
21             has 'ignore',
22             default => 0;
23              
24             sub search {
25 47     47 1 12952 my ($self, @criteria) = @_;
26 47         184 my $cache = $self->cache;
27 47         533 my $data = $self->data;
28 47         443 my $ignore = $self->ignore;
29              
30 47 50       477 unless (UNIVERSAL::isa($data, 'HASH')) {
31 0         0 Data::Seek::Exception->throw(
32             message => 'DATA NOT A HASH REFERENCE OR BLESSED HASH OBJECT'
33             );
34             }
35              
36 47         70 my $search;
37              
38 47 100       128 if ($self->cache) {
39 6 100       61 if ($search = $self->{cached_search}) {
40 5         16 $search->criteria({});
41             }
42             }
43              
44 47   66     716 $search //= Data::Seek::Search->new(
45             cache => $cache,
46             ignore => $ignore,
47             data => Data::Seek::Data->new(
48             object => $data
49             ),
50             );
51              
52 47         1963 $search->criterion($_) for @criteria;
53              
54 47 100       148 if ($self->cache) {
55 6         46 $self->{cached_search} = $search;
56             }
57             else {
58 41         321 $self->{cached_search} = undef;
59             }
60              
61 47         193 return $search->result;
62             }
63              
64             1;
65              
66             __END__
67              
68             =pod
69              
70             =encoding UTF-8
71              
72             =head1 NAME
73              
74             Data::Seek - Search Complex Data Structures
75              
76             =head1 VERSION
77              
78             version 0.05
79              
80             =head1 SYNOPSIS
81              
82             use Data::Seek;
83              
84             my $hash = {...};
85             my $seeker = Data::Seek->new(data => $hash);
86             my $result = $seeker->search('*');
87              
88             $result->data;
89              
90             =head1 DESCRIPTION
91              
92             Data::Seek is a module for traversing complex data structures. This module
93             allows you to select specific node(s) in a hierarchical data structure using a
94             criteria. A criteria is an expression consisting of one or more criterion. A
95             criterion is the part of the criteria that is used to select node(s) and
96             sub-node(s) to be returned in the result. Data::Seek is akin to L<Data::Dpath>
97             but with far fewer features, a simpler node selection syntax, and a
98             non-recursive approach toward traversal which makes Data::Seek extremely fast
99             and efficient. An even better reason to use Data::Seek is its ability to throw
100             exception objects which explain, in detail, why a search failed. This is very
101             useful internally, and externally when processing foreign data structures where
102             you need to provide detailed errors explaining how to resolve the missing or
103             malformed data nodes. For more information on the underlying concepts, please
104             see L<Data::Seek::Concepts>.
105              
106             =head1 ATTRIBUTES
107              
108             =head2 cache
109              
110             $seeker->cache;
111             $seeker->cache(1);
112              
113             Encode the data structure and cache the result. Allows multiple queries to
114             execute faster. Caching is disabled by default.
115              
116             =head2 data
117              
118             $seeker->data;
119             $seeker->data(Data::Seek::Data->new(...));
120              
121             The data structure to be introspected, must be a hash reference, blessed or not,
122             which defaults to or becomes a L<Data::Seek::Data> object.
123              
124             =head2 ignore
125              
126             $seeker->ignore;
127             $seeker->ignore(1);
128              
129             Bypass exceptions thrown when a criterion finds an unknown or invalid node in
130             the data structure.
131              
132             =head1 METHODS
133              
134             =head2 search
135              
136             my @criteria = ('id', 'person.name.*');
137             my $result = $seeker->search(@criteria);
138              
139             Prepare a search object to use the supplied criteria and return a result
140             object. Introspection is triggered when the result object is enacted. See
141             L<Data::Seek::Search::Result> for usage information.
142              
143             =encoding utf8
144              
145             =head1 AUTHOR
146              
147             Al Newkirk <anewkirk@ana.io>
148              
149             =head1 COPYRIGHT AND LICENSE
150              
151             This software is copyright (c) 2014 by Al Newkirk.
152              
153             This is free software; you can redistribute it and/or modify it under
154             the same terms as the Perl 5 programming language system itself.
155              
156             =cut