Rabbit/Trait/XPathObjectMap.pm

filter: Statementstmt 100% 36 / 36 Branchbran 75.0% 3 / 4 Conditioncond 66.6% 2 / 3 MC/DCmcdc 50.0% 1 / 2 Subroutinesub 100% 8 / 8 Podpod n/a totaltotal 94.3% 50 / 53 CC 5 SCAR 7.8
CC
5
Cov
95%
__ANON__
4.0
__ANON__
2.0
_build_default
1.0
CRAP 2.2
« Rabbit/Trait/XPathValueMap.pm ↑ Summary Rabbit.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::XPathObjectMap;
5 {
6 15 $XML::Rabbit::Trait::XPathObjectMap::VERSION = '0.4.0';
7 }
8 15 ▶use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 15 with 'XML::Rabbit::Trait::XPath';
11  
12 # ABSTRACT: Multiple XML DOM object xpath extractor trait
13  
14 around '_process_options' => sub {
15 8 ▶ my ($orig, $self, $name, $options, @rest) = @_;
Subroutine __ANON__: called CC=2 SCAR=7.1
16  
17 8 $self->$orig($name, $options, @rest);
18  
19 # This should really be:
20 # has '+isa' => ( required => 1 );
21 # but for some unknown reason Moose doesn't allow that
22 8 ▶ confess("isa attribute is required") unless defined( $options->{'isa'} );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $options->{"isa"} 0 8 8
23  
24 15 };
25  
26  
27 has 'isa_map' => (
28 is => 'ro',
29 isa => 'HashRef[Str]',
30 lazy => 1,
31 4 ▶ default => sub { +{} },
Subroutine __ANON__: called CC=1 SCAR=0.0
32 15 );
33  
34  
35 15 has 'xpath_key' => (
36 is => 'ro',
37 isa => 'Str',
38 required => 1,
39 );
40  
41  
42 sub _build_default {
43 6 ▶ my ($self) = @_;
Subroutine _build_default: called CC=1 SCAR=0.0
44 return sub {
45 6 ▶ my ($parent) = @_;
Subroutine __ANON__: called CC=4 SCAR=13.9
46 6 my $xpath_query = $self->_resolve_xpath_query( $parent );
47 6 $self->_convert_isa_map( $parent );
48 6 my $class = $self->_resolve_class();
49 6 my %node_map;
50 6 foreach my $node ( $self->_find_nodes($parent, $xpath_query ) ) {
51 1460 my $key = $parent->xpc->findvalue( $self->xpath_key, $node );
52 1460 ▶ if ( defined($key) and length $key > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $key and length $key > 0) 1459 1 1460
Condition66%2/3
defined $key and length $key > 0
!ll&&!rl&&r
011459
MC/DC50%1/2
defined $key and length $key > 0
defined $key length $key > 0
Truth table66%2/3
A and B
A= defined $keyB= length $key > 0
ABresult
0X0
100
111
53 1459 $node_map{ $key } = $self->_create_instance( $parent, $class, $node );
54 }
55 }
56 6 return \%node_map;
57 6 };
58 }
59  
60 15 Moose::Util::meta_attribute_alias('XPathObjectMap');
61  
62 15 ▶no Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
63  
64 15 1;
65  
66 __END__
67  
68 =pod
69  
70 =encoding utf-8
71  
72 =head1 NAME
73  
74 XML::Rabbit::Trait::XPathObjectMap - Multiple XML DOM object xpath extractor trait
75  
76 =head1 VERSION
77  
78 version 0.4.0
79  
80 =head1 SYNOPSIS
81  
82 package MyXMLSyntaxNode;
83 use Moose;
84 with 'XML::Rabbit::Node';
85  
86 has 'persons' => (
87 isa => 'HashRef[MyXMLSyntax::Person]',
88 traits => [qw(XPathObjectMap)],
89 xpath_query => './persons/*',
90 xpath_key => './@name',
91 );
92  
93 no Moose;
94 __PACKAGE__->meta->make_immutable();
95  
96 1;
97  
98 =head1 DESCRIPTION
99  
100 This module provides the extraction of multiple complex values (subtrees)
101 from an XML node based on an XPath query. Each subtree is used as input for
102 the constructor of the class specified in the isa attribute. All of the
103 extracted objects are then put into an arrayref which is accessible via the
104 parent attribute.
105  
106 See L<XML::Rabbit> for a more complete example.
107  
108 =head1 ATTRIBUTES
109  
110 =head2 isa_map
111  
112 Specifies the prefix:tag to class name mapping used with union xpath
113 queries. See L<XML::Rabbit> for more detailed information.
114  
115 =head2 xpath_key
116  
117 The xpath query that specifies what will be put in the key in the hash. Required.
118  
119 =head1 METHODS
120  
121 =head2 _build_default
122  
123 Returns a coderef that is run to build the default value of the parent attribute. Read Only.
124  
125 =head1 AUTHOR
126  
127 Robin Smidsrød <robin@smidsrod.no>
128  
129 =head1 COPYRIGHT AND LICENSE
130  
131 This software is copyright (c) 2013 by Robin Smidsrød.
132  
133 This is free software; you can redistribute it and/or modify it under
134 the same terms as the Perl 5 programming language system itself.
135  
136 =cut
« Rabbit/Trait/XPathValueMap.pm ↑ Summary Rabbit.pm »