Rabbit/Trait/XPath.pm

filter: Statementstmt 99.1% 118 / 119 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.6% 178 / 203 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 15 $XML::Rabbit::Trait::XPath::VERSION = '0.4.1';
6 15 ▶use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 15 ▶use Moose::Util::TypeConstraints;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 15 ▶use Perl6::Junction ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 15 ▶use Class::Load ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 # ABSTRACT: Base role for other xpath traits
12  
13 88 ▶around '_process_options' => sub {
Subroutine __ANON__: called CC=6 SCAR=17.9
14 88 my ($orig, $self, $name, $options, @rest) = @_;
15  
16 # XPath-derived attributes should always be
17 # set using the builder, this is just a way to enforce
18 # that behaviour.
19 88 $options->{'is'} = 'ro';
20 88 $options->{'init_arg'} = undef;
21 88 $options->{'lazy'} = 1;
22  
23 # Will call _build_default which should be composited in
24 # from another role. _build_default should return a coderef that is
25 # executed in the context of the parent (the class with the attribute defined).
26 66 ▶ $options->{'default'} = sub {
Subroutine __ANON__: called CC=2 SCAR=7.0
27 66 my ($parent) = @_;
28 66 my $self = $parent->meta->find_attribute_by_name($name);
29 66 my $actual_builder = $self->_build_default();
30 66 ▶ return $actual_builder unless ref($actual_builder) eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $actual_builder eq "CODE" 0 66 66
31 66 return &$actual_builder( $parent );
32 88 };
33  
34 # TODO: Maybe throw raging exceptions instead?
35 88 delete $options->{'builder'};
36 88 delete $options->{'lazy_build'};
37 88 delete $options->{'required'};
38  
39 # Specifying isa_map builds 'isa' for you
40 88 ▶ unless ( exists $options->{'isa'} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $options->{"isa"}) 10 78 88
41 10 ▶ if ( $options->{'isa_map'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options->{"isa_map"}) 6 4 10
42 6 my @classes;
43 6 foreach my $value ( values %{ $options->{'isa_map'} } ) {
44 12 class_type($value);
45 12 push @classes, $value,
46 }
47 # Build union isa
48 6 my $isa = join('|', @classes);
49 # If traits indicate XPathObjectList, assume an ArrayRef
50 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
51 5 $isa = "ArrayRef[$isa]";
52 }
53 # If traits indicate XPathObjectMap, assume a HashRef
54 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
55 1 $isa = "HashRef[$isa]";
56 }
57 6 $options->{'isa'} = $isa;
58 }
59 }
60  
61 88 $self->$orig($name, $options, @rest);
62 15 };
63  
64  
65  
66 15 has 'xpath_query' => (
67 is => 'ro',
68 isa => 'Str|CodeRef',
69 required => 1,
70 );
71  
72 15 has '_isa_map_converted' => (
73 is => 'rw',
74 isa => 'Bool',
75 default => 0,
76 );
77  
78 66 ▶sub _verify_parent_role {
Subroutine _verify_parent_role: called CC=2 SCAR=7.6
79 66 my ($self, $parent) = @_;
80  
81 # Make sure the parent class implements required role
82 66 ▶ unless ( $parent->does('XML::Rabbit::Role::Node') ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($parent->does("XML::Rabbit::Role::Node")) 0 66 66
83 0 confess( ref($parent) . " doesn't implement XML::Rabbit::Role::Node");
84 }
85  
86 66 return 1;
87 }
88  
89 66 ▶sub _resolve_xpath_query {
Subroutine _resolve_xpath_query: called CC=6 SCAR=20.5
90 66 my ($self, $parent) = @_;
91  
92 # Figure out if xpath_query is code
93 66 ▶ my $query_is_object = blessed($self->xpath_query) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($self->xpath_query) ? : 0 66 66
94 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
95 66 my $query_is_code = $query_is_coderef;
96 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
97  
98 # Run code reference if necessary to build xpath query.
99 # The parent object is the first param to the coderef, not the attribute.
100 # This allows the resolution of information in the coderef to happen
101 # from the perspective of the class that uses the attribute instead from
102 # the perspective of the attribute.
103 # Finally overwrite coderef with static value.
104 66 ▶ my $xpath_query = $query_is_code ? $self->xpath_query->($parent) : $self->xpath_query;
Branch: 1 branch
BranchTrueFalseTotal
$query_is_code ? : 0 66 66
105  
106 66 return $xpath_query;
107 }
108  
109 36 ▶sub _resolve_class {
Subroutine _resolve_class: called CC=10 SCAR=23.1
110 36 my ($self) = @_;
111  
112 # Figure out classes mentioned in type constraint (isa)
113 36 my %classes;
114 36 ▶ if ( $self->has_type_constraint ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_type_constraint) 36 0 36
115  
116 # Visit each part of the TC in turn, deconstruct and extract class names
117 36 my @to_visit = $self->type_constraint;
118 36 ▶ while (my $item = shift @to_visit) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $item = shift @to_visit) 69 36 105
119 69 ▶ next unless blessed $item;
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($item) 0 69 69
120 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
121 # Just a plain string that is a class
122 38 $classes{ $item->class } = 1;
123 }
124 elsif ( $item->isa('Moose::Meta::TypeConstraint::Union') ) {
125 # ArrayRef[Something|SomethingElse]
126 6 push @to_visit, @{ $item->type_constraints };
127 }
128 elsif ( $item->isa('Moose::Meta::TypeConstraint::Parameterized') ) {
129 # Maybe[Something], ArrayRef[Something] or HashRef[Something]
130 21 push @to_visit, $item->type_parameter;
131 }
132 elsif ( $item->isa('Moose::Meta::TypeConstraint::Parameterizable') ) {
133 # Built-in like ArrayRef or HashRef without a parameter
134 4 next;
135 }
136 else {
137 #warn("Unsupported TC detected: $item");
138 }
139 }
140  
141 # Some debugging aid
142 #my $tc_name = $self->type_constraint->name;
143 #my $found = join(", ", sort keys %classes);
144 #warn("Classes found in TC '$tc_name': $found\n");
145 }
146  
147 # RT#81519: The above code was supposed to not return duplicate class
148 # namess when they are not present in the TC. Perl 5.17.6 introduces
149 # hash seed randomization, which caused the above code to return
150 # duplicates. Use a hash to kill duplicates. Data::Visitor::Callback
151 # should be fixed to avoid this problem.
152 36 my @classes = keys %classes;
153  
154 # Runtime load each class
155 36 Class::Load::load_class($_) for @classes;
156  
157 # Return 0 if multiple classes found,
158 # _create_instance() must use $self->isa_map to resolve class name
159 36 ▶ return scalar @classes > 1 ? 0 : $classes[0];
Branch: 1 branch
BranchTrueFalseTotal
scalar @classes > 1 ? : 6 30 36
160  
161 }
162  
163 36 ▶sub _convert_isa_map {
Subroutine _convert_isa_map: called CC=6 SCAR=18.1
164 36 my ($self, $parent) = @_;
165  
166 # isa_map is optional
167 36 ▶ return unless $self->can('isa_map');
Branch: 1 branch
BranchTrueFalseTotal
unless $self->can("isa_map") 0 36 36
168  
169 # Don't let it run more than once per trait meta-instance
170 36 ▶ return if $self->_isa_map_converted;
Branch: 1 branch
BranchTrueFalseTotal
if $self->_isa_map_converted 4 32 36
171  
172 32 foreach my $key ( keys %{ $self->isa_map } ) {
173 # Skip nodes that have no prefix specified
174 16 ▶ next unless $key =~ /:/x;
Branch: 1 branch
BranchTrueFalseTotal
unless $key =~ /:/x 0 16 16
175  
176 # Find namespace URI in main mapping
177 16 my ($prefix, $node_name) = split(/:/x, $key);
178 16 my $ns_uri = $parent->namespace_map->{ $prefix };
179  
180 # Stop if namespaceURI was not found, to continue would create unstable behaviour
181 16 ▶ confess("Prefix '$prefix' not defined in namespace_map") unless $ns_uri;
Branch: 1 branch
BranchTrueFalseTotal
unless $ns_uri 0 16 16
182  
183 # Replace prefix key with namespaceURI key used by _create_instance()
184 16 my $new_key = '[' . $ns_uri . ']' . $node_name;
185 16 $self->isa_map->{ $new_key } = $self->isa_map->{$key};
186 16 delete $self->isa_map->{$key};
187 }
188  
189 32 $self->_isa_map_converted(1);
190  
191 32 return 1;
192 }
193  
194 1933 ▶sub _create_instance {
Subroutine _create_instance: called CC=5 SCAR=16.2
195 1933 my ($self, $parent, $class, $node) = @_;
196  
197 # Just return undef if no node passed
198 # TypeConstraint must be Maybe[XXX] though
199 # Used for optional elements
200 1933 ▶ return unless $node;
Branch: 1 branch
BranchTrueFalseTotal
unless $node 4 1929 1933
201  
202 1929 ▶ unless ( $class ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($class) 962 967 1929
203 962 ▶ my $node_name = ( $node->namespaceURI ? '[' . $node->namespaceURI . ']' : "" )
Branch: 1 branch
BranchTrueFalseTotal
$node->namespaceURI ? : 962 0 962
204 . $node->localname;
205 962 $class = $self->isa_map->{ $node_name };
206 }
207 1929 ▶ confess("Unable to resolve class for node " . $node->nodeName) unless $class;
Branch: 1 branch
BranchTrueFalseTotal
unless $class 0 1929 1929
208  
209 1929 Class::Load::load_class($class); # FIXME: This should be fixed at line 153
210 1929 my $instance = $class->new(
211 xpc => $parent->xpc,
212 node => $node,
213 namespace_map => $parent->namespace_map,
214 );
215 1929 return $instance;
216 }
217  
218 37 ▶sub _find_node {
Subroutine _find_node: called CC=3 SCAR=11.2
219 37 my ($self, $parent, $xpath_query) = @_;
220 37 $self->_verify_parent_role( $parent );
221 37 my $node = $parent->xpc->find( $xpath_query, $parent->node );
222 37 ▶ return unless blessed($node); # No node found, just return undef (optional elements)
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($node) 0 37 37
223 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
224 37 return $node;
225 }
226  
227 29 ▶sub _find_nodes {
Subroutine _find_nodes: called CC=4 SCAR=14.2
228 29 my ($self, $parent, $xpath_query) = @_;
229 29 $self->_verify_parent_role( $parent );
230 29 my @nodes;
231 29 foreach my $node ( $parent->xpc->findnodes( $xpath_query, $parent->node ) ) {
232 2433 ▶ push @nodes, $node if blessed($node);
Branch: 1 branch
BranchTrueFalseTotal
if builtin::blessed($node) 2433 0 2433
233 }
234 29 ▶ return wantarray ? @nodes : \@nodes;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 29 0 29
235 }
236  
237 15 ▶no Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
238 15 ▶no Moose::Util::TypeConstraints;
Subroutine BEGIN: called CC=1 SCAR=0.0
239  
240 15 1;
241  
242 __END__
↑ Summary Rabbit/Role/Document.pm »