Rabbit/Trait/XPathValueMap.pm

filter: Statementstmt 100% 34 / 34 Branchbran 75.0% 3 / 4 Conditioncond 66.6% 2 / 3 MC/DCmcdc 50.0% 1 / 2 Subroutinesub 100% 7 / 7 Podpod n/a totaltotal 94.0% 47 / 50 CC 5 SCAR 8.2
CC
5
Cov
95%
__ANON__
4.0
__ANON__
2.0
_build_default
1.0
CRAP 2.3
« Rabbit/Role/Document.pm ↑ Summary Rabbit/Trait/XPathObjectMap.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::XPathValueMap;
5 {
6 15 $XML::Rabbit::Trait::XPathValueMap::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 value xpath extractor trait
13  
14 around '_process_options' => sub {
15 6 ▶ my ($orig, $self, $name, $options, @rest) = @_;
Subroutine __ANON__: called CC=2 SCAR=7.1
16  
17 6 $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 6 ▶ confess("isa attribute is required") unless defined( $options->{'isa'} );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $options->{"isa"} 0 6 6
23 15 };
24  
25  
26 15 has 'xpath_key' => (
27 is => 'ro',
28 isa => 'Str',
29 required => 1,
30 );
31  
32  
33 15 has 'xpath_value' => (
34 is => 'ro',
35 isa => 'Str',
36 required => 1,
37 );
38  
39  
40 sub _build_default {
41 4 ▶ my ($self) = @_;
Subroutine _build_default: called CC=1 SCAR=0.0
42 return sub {
43 4 ▶ my ($parent) = @_;
Subroutine __ANON__: called CC=4 SCAR=13.9
44 4 my $xpath_query = $self->_resolve_xpath_query( $parent );
45 4 my %node_map;
46 4 foreach my $node ( $self->_find_nodes( $parent, $xpath_query ) ) {
47 510 my $key = $parent->xpc->findvalue( $self->xpath_key, $node );
48 510 ▶ if ( defined($key) and length $key > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $key and length $key > 0) 509 1 510
Condition66%2/3
defined $key and length $key > 0
!ll&&!rl&&r
01509
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
49 509 my $value = $parent->xpc->findvalue( $self->xpath_value, $node );
50 509 $node_map{$key} = $value;
51 }
52 }
53 4 return \%node_map;
54 4 };
55 }
56  
57 15 Moose::Util::meta_attribute_alias('XPathValueMap');
58  
59 15 ▶no Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
60  
61 15 1;
62  
63 __END__
64  
65 =pod
66  
67 =encoding utf-8
68  
69 =head1 NAME
70  
71 XML::Rabbit::Trait::XPathValueMap - Multiple value xpath extractor trait
72  
73 =head1 VERSION
74  
75 version 0.4.0
76  
77 =head1 SYNOPSIS
78  
79 package MyXMLSyntaxNode;
80 use Moose;
81 with 'XML::Rabbit::RootNode';
82  
83 has reference_map => (
84 isa => 'HashRef[Str]',
85 traits => [qw(XPathValueMap)],
86 xpath_query => '//*[@href]',
87 xpath_key => './@href',
88 xpath_value => './@title';
89 );
90  
91 no Moose;
92 __PACKAGE__->meta->make_immutable();
93  
94 1;
95  
96 =head1 DESCRIPTION
97  
98 This module provides the extraction of primitive values from an XML node based on an XPath query.
99  
100 See L<XML::Rabbit> for a more complete example.
101  
102 =head1 ATTRIBUTES
103  
104 =head2 xpath_key
105  
106 The xpath query that specifies what will be put in the key in the hash. Required.
107  
108 =head2 xpath_value
109  
110 The xpath query that specifies what will be put in the value in the hash. Required.
111  
112 =head1 METHODS
113  
114 =head2 _build_default
115  
116 Returns a coderef that is run to build the default value of the parent attribute. Read Only.
117  
118 =head1 AUTHOR
119  
120 Robin Smidsrød <robin@smidsrod.no>
121  
122 =head1 COPYRIGHT AND LICENSE
123  
124 This software is copyright (c) 2013 by Robin Smidsrød.
125  
126 This is free software; you can redistribute it and/or modify it under
127 the same terms as the Perl 5 programming language system itself.
128  
129 =cut
« Rabbit/Role/Document.pm ↑ Summary Rabbit/Trait/XPathObjectMap.pm »