lib/Parse/FieldPath.pm

filter: Statementstmt 100% 67 / 67 Branchbran 96.4% 27 / 28 Conditioncond 86.9% 20 / 23 MC/DCmcdc 78.5% 11 / 14 Subroutinesub 100% 12 / 12 Podpod 100% 1 / 1 totaltotal 95.1% 138 / 145 CC 25 SCAR 26.5
CC
25
Cov
97%
_extract
22.0
extract_fields
4.0
BEGIN
1.0
CRAP 14.2
« lib/Parse/FieldPath/Parser.pm ↑ Summary script/rebuild_grammar.pl »
1 package Parse::FieldPath;
2 {
3 10 $Parse::FieldPath::VERSION = '0.005';
4 }
5  
6 # ABSTRACT: Perl module to extract fields from objects
7  
8 10 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 10 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 10 ▶use Exporter qw/import unimport/;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 10 our @EXPORT_OK = qw/extract_fields/;
13  
14 10 ▶use Scalar::Util qw/reftype blessed/;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 10 ▶use List::Util qw/first/;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 10 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 10 ▶use Parse::FieldPath::Parser;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 # Maximum number of times to allow _extract to recurse.
21 10 ▶use constant RECURSION_LIMIT => 512;
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 32 ▶sub extract_fields {
Subroutine extract_fields: called CC=4 SCAR=13.9
Pod documented
24 32 my ( $obj, $field_path ) = @_;
25  
26 32 ▶ croak "extract_fields needs an object or a hashref"
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($obj) or builtin::reftype($obj) and builtin::reftype($obj) eq "HASH" 1 31 32
Condition83%5/6
builtin::blessed($obj) or builtin::reftype($obj) and builtin::reftype($obj) eq "HASH"
l!l&&r!l&&!r
2471
builtin::reftype($obj) and builtin::reftype($obj) eq "HASH"
!ll&&!rl&&r
107
MC/DC66%2/3
builtin::blessed($obj) or builtin::reftype($obj) and builtin::reftype($obj) eq "HASH"
builtin::blessed($obj) builtin::reftype($obj) builtin::reftype($obj) eq "HASH"
Truth table75%3/4
A or B and C
A= builtin::blessed($obj)B= builtin::reftype($obj)C= builtin::reftype($obj) eq "HASH"
ABCresult
1XX1
0111
00X0
0100
27 unless blessed($obj) || ( reftype($obj) && reftype($obj) eq 'HASH' );
28  
29 31 my $tree = _build_tree($field_path);
30 31 return _extract( $obj, $tree, 0 );
31 }
32  
33 42 ▶sub _build_tree {
Subroutine _build_tree: called CC=1 SCAR=0.0
34 42 my ($field_path) = @_;
35 42 my $parser = Parse::FieldPath::Parser->new();
36 42 return $parser->parse($field_path);
37 }
38  
39 577 ▶sub _extract {
Subroutine _extract: called CC=22 SCAR=30.9
40 577 my ( $source, $tree, $recurse_count ) = @_;
41  
42 577 $recurse_count++;
43 577 ▶ die "Maximum recursion limit reached" if $recurse_count > RECURSION_LIMIT;
Branch: 1 branch
BranchTrueFalseTotal
if $recurse_count > 512 1 576 577
44  
45 576 my $is_object;
46 576 ▶ if ( blessed($source) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (builtin::blessed($source)) { } 556 20 576
elsif (builtin::reftype($source) and builtin::reftype($source) eq 'HASH') { } 11 9 20
47 556 $is_object = 1;
48 }
49 ▶ elsif ( reftype($source) && reftype($source) eq 'HASH' ) {
Condition66%2/3
builtin::reftype($source) and builtin::reftype($source) eq "HASH"
!ll&&!rl&&r
9011
MC/DC50%1/2
builtin::reftype($source) and builtin::reftype($source) eq "HASH"
builtin::reftype($source) builtin::reftype($source) eq "HASH"
Truth table66%2/3
A and B
A= builtin::reftype($source)B= builtin::reftype($source) eq "HASH"
ABresult
0X0
100
111
50 11 $is_object = 0;
51 }
52 else {
53 9 return $source;
54 }
55  
56 567 my $all_fields = [];
57 567 ▶ if ($is_object) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_object) { } 556 11 567
58 556 ▶ $all_fields = $source->all_fields() if $source->can('all_fields');
Branch: 1 branch
BranchTrueFalseTotal
if $source->can("all_fields") 556 0 556
59  
60 556 ▶ die "Expected $source->all_fields to return an arrayref"
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::reftype($all_fields) and builtin::reftype($all_fields) eq "ARRAY" 1 555 556
Condition66%2/3
builtin::reftype($all_fields) and builtin::reftype($all_fields) eq "ARRAY"
!ll&&!rl&&r
10555
MC/DC50%1/2
builtin::reftype($all_fields) and builtin::reftype($all_fields) eq "ARRAY"
builtin::reftype($all_fields) builtin::reftype($all_fields) eq "ARRAY"
Truth table66%2/3
A and B
A= builtin::reftype($all_fields)B= builtin::reftype($all_fields) eq "ARRAY"
ABresult
0X0
100
111
61 unless reftype($all_fields)
62 && reftype($all_fields) eq 'ARRAY';
63 }
64 else {
65 11 @$all_fields = keys %$source;
66 }
67  
68 566 ▶ if ( exists $tree->{'*'} || !%$tree ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $tree->{"*"} or not %$tree) 528 38 566
Condition100%3/3
exists $tree->{"*"} or not %$tree
l!l&&r!l&&!r
652238
MC/DC100%2/2
exists $tree->{"*"} or not %$tree
exists $tree->{"*"} not %$tree
Truth table100%3/3
A or not(B)
A= exists $tree->{"*"}B= not %$tree
ABresult
1X1
011
000
69  
70 # We've got an object, but not a list of fields. Get everything.
71 528 $tree->{$_} = {} for @$all_fields;
72 }
73  
74 566 ▶ $source->fields_requested( [ keys %$tree ] )
Branch: 1 branch
BranchTrueFalseTotal
if $is_object and $source->can("fields_requested") 4 562 566
Condition100%3/3
$is_object and $source->can("fields_requested")
!ll&&!rl&&r
115514
MC/DC100%2/2
$is_object and $source->can("fields_requested")
$is_object $source->can("fields_requested")
Truth table100%3/3
A and B
A= $is_objectB= $source->can("fields_requested")
ABresult
0X0
100
111
75 if $is_object && $source->can('fields_requested');
76  
77 566 my %fields;
78 566 for my $field ( keys %$tree ) {
79  
80 # Only accept fields that have been explicitly allowed
81 600 ▶ next unless first { $_ eq $field } @$all_fields;
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
unless &first(sub { $_ eq $field; } , @$all_fields) 8 592 600
82  
83 592 my $branch = $tree->{$field};
84 592 ▶ my $value = $is_object ? $source->$field : $source->{$field};
Branch: 1 branch
BranchTrueFalseTotal
$is_object ? : 574 18 592
85 592 ▶ my $value_reftype = reftype($value) || '';
Condition100%2/2
builtin::reftype($value) || ''
l!l
53557
MC/DC100%1/1
builtin::reftype($value) || ''
builtin::reftype($value)
Truth table100%2/2
A
A= builtin::reftype($value)
Aresult
11
00
86  
87 592 ▶ if ( blessed($value) || $value_reftype eq 'HASH' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (builtin::blessed($value) or $value_reftype eq 'HASH') { } 527 65 592
elsif ($value_reftype eq 'ARRAY') { } 8 57 65
Condition100%3/3
builtin::blessed($value) or $value_reftype eq "HASH"
l!l&&r!l&&!r
523465
MC/DC100%2/2
builtin::blessed($value) or $value_reftype eq "HASH"
builtin::blessed($value) $value_reftype eq "HASH"
Truth table100%3/3
A or B
A= builtin::blessed($value)B= $value_reftype eq "HASH"
ABresult
1X1
011
000
88 527 $fields{$field} = _extract( $value, $branch, $recurse_count );
89 }
90 elsif ( $value_reftype eq 'ARRAY' ) {
91 $fields{$field} =
92 8 [ map { _extract( $_, $branch, $recurse_count ) } @{$value} ];
93 }
94 else {
95 57 ▶ if (%$branch) {
Branch: 1 branch
BranchTrueFalseTotal
if (%$branch) { } 1 56 57
96  
97 # Unblessed object, but a sub-object has been requested.
98 # Setting it to undef, maybe an error should be thrown here
99 # though?
100 1 $fields{$field} = undef;
101 }
102 else {
103 56 $fields{$field} = $value;
104 }
105 }
106 }
107  
108 53 return \%fields;
109 }
110  
111 10 1;
112  
113 =pod
114  
115 =head1 NAME
116  
117 Parse::FieldPath
118  
119 =head1 ABSTRACT
120  
121 Parses an XPath inspired field list and extracts those fields from an object
122 hierarchy.
123  
124 Based on the "fields" parameter for the Google+ API:
125 http://developers.google.com/+/api/
126  
127 =head1 SYNOPSIS
128  
129 Say you have an object, with some sub-objects, that's initialized like this:
130  
131 my $cow = Cow->new();
132 $cow->color("black and white");
133 $cow->tail(Cow::Tail->new(floppy => 1));
134 $cow->mouth(Cow::Tounge->new(
135 tounge => Cow::Tounge->new,
136 teeth => Cow::Teeth->new,
137 );
138  
139 And you want a hash containing some of those fields (perhaps to pass to
140 JSON::XS, or something). Then you can do this:
141  
142 use Parse::FieldPath qw/extract_fields/;
143  
144 my $cow_hash = extract_fields($cow, "color,tail/floppy");
145 # $cow_hash is now:
146 # {
147 # color => 'black and white',
148 # tail => {
149 # floppy => 1,
150 # }
151 # }
152  
153 =head1 FUNCTIONS
154  
155 =over 4
156  
157 =item B<extract_fields ($object_or_hashref, $field_path)>
158  
159 Parses the C<field_path> and returns a hashref with the fields requested from
160 C<$object_or_hashref>.
161  
162 C<$object_or_hashref>, and any sub-objects, will need to define a method called
163 C<all_fields()>. See L<CALLBACKS> for details.
164  
165 C<field_path> is a string describing the fields to return. Each field is
166 separated by a comma, e.g. "a,b" will return fields "a" and "b".
167  
168 To request a field from a sub-objects, use the form "subobject/field". If more
169 than one field from a sub-object is required, put the field names in
170 parenthesis, "subobject(field1,field2)".
171  
172 C<field_path> can go as deep as necessary, for example, this works fine:
173 "a/b/c(d/e,f)"
174  
175 =back
176  
177 =head1 CALLBACKS
178  
179 =over 4
180  
181 =item B<all_fields()>
182  
183 A method called C<all_fields()> should be defined for any object (including
184 sub-objects), that will be used with this module. It needs to return an
185 arrayref containing all the valid fields. Any field requested that's not in the
186 list returned by C<all_fields()> will be skipped.
187  
188 A simple implementation would be:
189  
190 sub all_fields {
191 my ($self) = @_;
192 return [qw/field1 field2/];
193 }
194  
195 If the list doesn't change, a constant will work too:
196  
197 use constant all_fields => [qw/field1 field2/];
198  
199 This method is required because simply allowing any method to be called would
200 be dangerous (e.g. if your object had a "delete_everything()" method, or
201 something). It's also necessary to know which fields constitute "everything"
202 for the object.
203  
204 =item B<requested_fields($field_list)>
205  
206 Called on an object right before the accessor methods are called. It's passed a
207 list of fields that are about to be requested. This method is completely
208 optional. It's intended to allow the object to fetch anything it needs to, in
209 order to make the requested data available.
210  
211 =back
212  
213 =head1 GitHub
214  
215 https://github.com/pboyd/Parse-FieldPath
216  
217 =head1 AUTHOR
218  
219 Paul Boyd <pboyd@dev3l.net>
220  
221 =head1 COPYRIGHT AND LICENSE
222  
223 This software is copyright (c) 2011 by Paul Boyd.
224  
225 This is free software; you can redistribute it and/or modify it under
226 the same terms as the Perl 5 programming language system itself.
227  
228 =cut
« lib/Parse/FieldPath/Parser.pm ↑ Summary script/rebuild_grammar.pl »