Rabbit/Trait/XPath.pm

filter: Statementstmt 99.1% 119 / 120 Branchbran 70.6% 41 / 58 Conditioncond 33.3% 2 / 6 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 100% 17 / 17 Podpod n/a totaltotal 87.7% 179 / 204 CC 36 SCAR 17.1
CC
36
Cov
88%
_resolve_class
10.1
_resolve_xpath_query
7.8
_convert_isa_map
6.1
CRAP 5.5
↑ Summary Rabbit/Role/Document.pm »
1 15 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 15 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 package XML::Rabbit::Trait::XPath;
5 {
6 15 $XML::Rabbit::Trait::XPath::VERSION = '0.4.0';
7 }
8 15 ▶use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 15 ▶use Moose::Util::TypeConstraints;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 15 ▶use Perl6::Junction ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 15 ▶use Class::Load ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 # ABSTRACT: Base role for other xpath traits
14  
15 around '_process_options' => sub {
16 88 ▶ my ($orig, $self, $name, $options, @rest) = @_;
Subroutine __ANON__: called CC=6 SCAR=17.9
17  
18 # XPath-derived attributes should always be
19 # set using the builder, this is just a way to enforce
20 # that behaviour.
21 88 $options->{'is'} = 'ro';
22 88 $options->{'init_arg'} = undef;
23 88 $options->{'lazy'} = 1;
24  
25 # Will call _build_default which should be composited in
26 # from another role. _build_default should return a coderef that is
27 # executed in the context of the parent (the class with the attribute defined).
28 $options->{'default'} = sub {
29 66 ▶ my ($parent) = @_;
Subroutine __ANON__: called CC=2 SCAR=7.0
30 66 my $self = $parent->meta->find_attribute_by_name($name);
31 66 my $actual_builder = $self->_build_default();
32 66 ▶ return $actual_builder unless ref($actual_builder) eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $actual_builder eq "CODE" 0 66 66
33 66 return &$actual_builder( $parent );
34 88 };
35  
36 # TODO: Maybe throw raging exceptions instead?
37 88 delete $options->{'builder'};
38 88 delete $options->{'lazy_build'};
39 88 delete $options->{'required'};
40  
41 # Specifying isa_map builds 'isa' for you
42 88 ▶ unless ( exists $options->{'isa'} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $options->{"isa"}) 10 78 88
43 10 ▶ if ( $options->{'isa_map'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options->{"isa_map"}) 6 4 10
44 6 my @classes;
45 6 foreach my $value ( values %{ $options->{'isa_map'} } ) {
46 12 class_type($value);
47 12 push @classes, $value,
48 }
49 # Build union isa
50 6 my $isa = join('|', @classes);
51 # If traits indicate XPathObjectList, assume an ArrayRef
52 6 ▶ if ( Perl6::Junction::any( @{ $options->{'traits'} } ) == qr/^XML::Rabbit::Trait::XPathObjectList$/x ) {
Branch: 1 branch
BranchTrueFalseTotal
if (Perl6::Junction::any(@{$$options{"traits"};}) == qr/^XML::Rabbit::Trait::XPathObjectList$/x) 5 1 6
53 5 $isa = "ArrayRef[$isa]";
54 }
55 # If traits indicate XPathObjectMap, assume a HashRef
56 6 ▶ if ( Perl6::Junction::any( @{ $options->{'traits'} } ) == qr/^XML::Rabbit::Trait::XPathObjectMap$/x ) {
Branch: 1 branch
BranchTrueFalseTotal
if (Perl6::Junction::any(@{$$options{"traits"};}) == qr/^XML::Rabbit::Trait::XPathObjectMap$/x) 1 5 6
57 1 $isa = "HashRef[$isa]";
58 }
59 6 $options->{'isa'} = $isa;
60 }
61 }
62  
63 88 $self->$orig($name, $options, @rest);
64 15 };
65  
66  
67  
68 15 has 'xpath_query' => (
69 is => 'ro',
70 isa => 'Str|CodeRef',
71 required => 1,
72 );
73  
74 15 has '_isa_map_converted' => (
75 is => 'rw',
76 isa => 'Bool',
77 default => 0,
78 );
79  
80 sub _verify_parent_role {
81 66 ▶ my ($self, $parent) = @_;
Subroutine _verify_parent_role: called CC=2 SCAR=7.6
82  
83 # Make sure the parent class implements required role
84 66 ▶ unless ( $parent->does('XML::Rabbit::Role::Node') ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($parent->does("XML::Rabbit::Role::Node")) 0 66 66
85 0 confess( ref($parent) . " doesn't implement XML::Rabbit::Role::Node");
86 }
87  
88 66 return 1;
89 }
90  
91 sub _resolve_xpath_query {
92 66 ▶ my ($self, $parent) = @_;
Subroutine _resolve_xpath_query: called CC=6 SCAR=20.5
93  
94 # Figure out if xpath_query is code
95 66 ▶ my $query_is_object = blessed($self->xpath_query) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($self->xpath_query) ? : 0 66 66
96 66 ▶ my $query_is_coderef = ref($self->xpath_query) eq 'CODE' ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
ref $self->xpath_query eq 'CODE' ? : 0 66 66
97 66 my $query_is_code = $query_is_coderef;
98 66 ▶ $query_is_code ||= $query_is_object && $self->xpath_query->isa('Class::MOP::Method');
Condition33%2/6
$query_is_code ||= $query_is_object && $self->xpath_query->isa("Class::MOP::Method")
l!l&&r!l&&!r
0066
$query_is_object && $self->xpath_query->isa('Class::MOP::Method')
!ll&&!rl&&r
6600
MC/DC0%0/3
$query_is_code ||= $query_is_object && $self->xpath_query->isa("Class::MOP::Method")
$query_is_code $query_is_object $self->xpath_query->isa('Class::MOP::Method')
Truth table0%0/4
A ||= B && C
A= $query_is_codeB= $query_is_objectC= $self->xpath_query->isa('Class::MOP::Method')
ABCresult
1XX1
0111
00X0
0100
99  
100 # Run code reference if necessary to build xpath query.
101 # The parent object is the first param to the coderef, not the attribute.
102 # This allows the resolution of information in the coderef to happen
103 # from the perspective of the class that uses the attribute instead from
104 # the perspective of the attribute.
105 # Finally overwrite coderef with static value.
106 66 ▶ my $xpath_query = $query_is_code ? $self->xpath_query->($parent) : $self->xpath_query;
Branch: 1 branch
BranchTrueFalseTotal
$query_is_code ? : 0 66 66
107  
108 66 return $xpath_query;
109 }
110  
111 sub _resolve_class {
112 36 ▶ my ($self) = @_;
Subroutine _resolve_class: called CC=10 SCAR=23.1
113  
114 # Figure out classes mentioned in type constraint (isa)
115 36 my %classes;
116 36 ▶ if ( $self->has_type_constraint ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_type_constraint) 36 0 36
117  
118 # Visit each part of the TC in turn, deconstruct and extract class names
119 36 my @to_visit = $self->type_constraint;
120 36 ▶ while (my $item = shift @to_visit) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $item = shift @to_visit) 69 36 105
121 69 ▶ next unless blessed $item;
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($item) 0 69 69
122 69 ▶ if ( $item->isa('Moose::Meta::TypeConstraint::Class') ) {
Branch: 4 branches
BranchTrueFalseTotal
if ($item->isa('Moose::Meta::TypeConstraint::Class')) { } 38 31 69
elsif ($item->isa('Moose::Meta::TypeConstraint::Union')) { } 6 25 31
elsif ($item->isa('Moose::Meta::TypeConstraint::Parameterized')) { } 21 4 25
elsif ($item->isa('Moose::Meta::TypeConstraint::Parameterizable')) { } 4 0 4
123 # Just a plain string that is a class
124 38 $classes{ $item->class } = 1;
125 }
126 elsif ( $item->isa('Moose::Meta::TypeConstraint::Union') ) {
127 # ArrayRef[Something|SomethingElse]
128 6 push @to_visit, @{ $item->type_constraints };
129 }
130 elsif ( $item->isa('Moose::Meta::TypeConstraint::Parameterized') ) {
131 # Maybe[Something], ArrayRef[Something] or HashRef[Something]
132 21 push @to_visit, $item->type_parameter;
133 }
134 elsif ( $item->isa('Moose::Meta::TypeConstraint::Parameterizable') ) {
135 # Built-in like ArrayRef or HashRef without a parameter
136 4 next;
137 }
138 else {
139 #warn("Unsupported TC detected: $item");
140 }
141 }
142  
143 # Some debugging aid
144 #my $tc_name = $self->type_constraint->name;
145 #my $found = join(", ", sort keys %classes);
146 #warn("Classes found in TC '$tc_name': $found\n");
147 }
148  
149 # RT#81519: The above code was supposed to not return duplicate class
150 # namess when they are not present in the TC. Perl 5.17.6 introduces
151 # hash seed randomization, which caused the above code to return
152 # duplicates. Use a hash to kill duplicates. Data::Visitor::Callback
153 # should be fixed to avoid this problem.
154 36 my @classes = keys %classes;
155  
156 # Runtime load each class
157 36 Class::Load::load_class($_) for @classes;
158  
159 # Return 0 if multiple classes found,
160 # _create_instance() must use $self->isa_map to resolve class name
161 36 ▶ return scalar @classes > 1 ? 0 : $classes[0];
Branch: 1 branch
BranchTrueFalseTotal
scalar @classes > 1 ? : 6 30 36
162  
163 }
164  
165 sub _convert_isa_map {
166 36 ▶ my ($self, $parent) = @_;
Subroutine _convert_isa_map: called CC=6 SCAR=18.1
167  
168 # isa_map is optional
169 36 ▶ return unless $self->can('isa_map');
Branch: 1 branch
BranchTrueFalseTotal
unless $self->can("isa_map") 0 36 36
170  
171 # Don't let it run more than once per trait meta-instance
172 36 ▶ return if $self->_isa_map_converted;
Branch: 1 branch
BranchTrueFalseTotal
if $self->_isa_map_converted 4 32 36
173  
174 32 foreach my $key ( keys %{ $self->isa_map } ) {
175 # Skip nodes that have no prefix specified
176 16 ▶ next unless $key =~ /:/x;
Branch: 1 branch
BranchTrueFalseTotal
unless $key =~ /:/x 0 16 16
177  
178 # Find namespace URI in main mapping
179 16 my ($prefix, $node_name) = split(/:/x, $key);
180 16 my $ns_uri = $parent->namespace_map->{ $prefix };
181  
182 # Stop if namespaceURI was not found, to continue would create unstable behaviour
183 16 ▶ confess("Prefix '$prefix' not defined in namespace_map") unless $ns_uri;
Branch: 1 branch
BranchTrueFalseTotal
unless $ns_uri 0 16 16
184  
185 # Replace prefix key with namespaceURI key used by _create_instance()
186 16 my $new_key = '[' . $ns_uri . ']' . $node_name;
187 16 $self->isa_map->{ $new_key } = $self->isa_map->{$key};
188 16 delete $self->isa_map->{$key};
189 }
190  
191 32 $self->_isa_map_converted(1);
192  
193 32 return 1;
194 }
195  
196 sub _create_instance {
197 1933 ▶ my ($self, $parent, $class, $node) = @_;
Subroutine _create_instance: called CC=5 SCAR=16.2
198  
199 # Just return undef if no node passed
200 # TypeConstraint must be Maybe[XXX] though
201 # Used for optional elements
202 1933 ▶ return unless $node;
Branch: 1 branch
BranchTrueFalseTotal
unless $node 4 1929 1933
203  
204 1929 ▶ unless ( $class ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($class) 962 967 1929
205 962 ▶ my $node_name = ( $node->namespaceURI ? '[' . $node->namespaceURI . ']' : "" )
Branch: 1 branch
BranchTrueFalseTotal
$node->namespaceURI ? : 962 0 962
206 . $node->localname;
207 962 $class = $self->isa_map->{ $node_name };
208 }
209 1929 ▶ confess("Unable to resolve class for node " . $node->nodeName) unless $class;
Branch: 1 branch
BranchTrueFalseTotal
unless $class 0 1929 1929
210  
211 1929 Class::Load::load_class($class); # FIXME: This should be fixed at line 153
212 1929 my $instance = $class->new(
213 xpc => $parent->xpc,
214 node => $node,
215 namespace_map => $parent->namespace_map,
216 );
217 1929 return $instance;
218 }
219  
220 sub _find_node {
221 37 ▶ my ($self, $parent, $xpath_query) = @_;
Subroutine _find_node: called CC=3 SCAR=11.2
222 37 $self->_verify_parent_role( $parent );
223 37 my $node = $parent->xpc->find( $xpath_query, $parent->node );
224 37 ▶ return unless blessed($node); # No node found, just return undef (optional elements)
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($node) 0 37 37
225 37 ▶ $node = $node->shift if $node->isa('XML::LibXML::NodeList'); # Get first item if multiple results
Branch: 1 branch
BranchTrueFalseTotal
if $node->isa("XML::LibXML::NodeList") 37 0 37
226 37 return $node;
227 }
228  
229 sub _find_nodes {
230 29 ▶ my ($self, $parent, $xpath_query) = @_;
Subroutine _find_nodes: called CC=4 SCAR=14.2
231 29 $self->_verify_parent_role( $parent );
232 29 my @nodes;
233 29 foreach my $node ( $parent->xpc->findnodes( $xpath_query, $parent->node ) ) {
234 2433 ▶ push @nodes, $node if blessed($node);
Branch: 1 branch
BranchTrueFalseTotal
if builtin::blessed($node) 2433 0 2433
235 }
236 29 ▶ return wantarray ? @nodes : \@nodes;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 29 0 29
237 }
238  
239 15 ▶no Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
240 15 ▶no Moose::Util::TypeConstraints;
Subroutine BEGIN: called CC=1 SCAR=0.0
241  
242 15 1;
243  
244 __END__
245  
246 =pod
247  
248 =encoding utf-8
249  
250 =head1 NAME
251  
252 XML::Rabbit::Trait::XPath - Base role for other xpath traits
253  
254 =head1 VERSION
255  
256 version 0.4.0
257  
258 =head1 SYNOPSIS
259  
260 package XML::Rabbit::Trait::XPathSomething;
261 use Moose::Role;
262  
263 with 'XML::Rabbit::Trait::XPath';
264  
265 sub _build_default {
266 my ($self) = @_;
267 return sub {
268 my ($parent) = @_;
269  
270 ...
271  
272 };
273 }
274  
275 Moose::Util::meta_attribute_alias('XPathSomething');
276  
277 no Moose::Role;
278  
279 1;
280  
281 =head1 DESCRIPTION
282  
283 This module provides base methods for other xpath traits.
284  
285 See L<XML::Rabbit> for a more complete example.
286  
287 =head1 ATTRIBUTES
288  
289 =head2 xpath_query
290  
291 A string or a coderef that generates a string that is the XPath query used
292 to find the wanted value. Read Only.
293  
294 =head1 METHODS
295  
296 =head2 _build_default
297  
298 Each trait that composes this trait will need to define a method name
299 C<_build_default>. The _build_default method is called as a method on the
300 generated attribute class. It should return a code reference that will be
301 run in the content of the parent class (i.e. the class that defined the
302 attribute).
303  
304 Below you can see an example from the XPathValue trait:
305  
306 sub _build_default {
307 my ($self) = @_;
308 return sub {
309 my ($parent) = @_;
310 my $node = $self->_find_node(
311 $parent,
312 $self->_resolve_xpath_query( $parent ),
313 );
314 return blessed($node) ? $node->to_literal . "" : "";
315 };
316 }
317  
318 =head1 AUTHOR
319  
320 Robin Smidsrød <robin@smidsrod.no>
321  
322 =head1 COPYRIGHT AND LICENSE
323  
324 This software is copyright (c) 2013 by Robin Smidsrød.
325  
326 This is free software; you can redistribute it and/or modify it under
327 the same terms as the Perl 5 programming language system itself.
328  
329 =cut
↑ Summary Rabbit/Role/Document.pm »