Rabbit/Sugar.pm

filter: Statementstmt 93.7% 60 / 64 Branchbran 75.0% 6 / 8 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 80.0% 16 / 20 Podpod 100% 8 / 8 totaltotal 90.0% 90 / 100 CC 5 SCAR 4.1
CC
5
Cov
92%
has_xpath_object
2.0
has_xpath_object_list
2.0
has_xpath_object_map
2.0
CRAP 1.5
« Rabbit/Root.pm ↑ Summary Rabbit/Trait/XPathValueList.pm »
1 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 package XML::Rabbit::Sugar;
5 {
6 7 $XML::Rabbit::Sugar::VERSION = '0.4.0';
7 }
8  
9 # ABSTRACT: Sugar functions for easier declaration of xpath attributes
10  
11 7 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 7 ▶use Carp qw(confess);
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 7 ▶use Moose 0.89 (); # no magic, just load
Subroutine BEGIN: called CC=1 SCAR=0.0
15 7 ▶use Moose::Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 7 Moose::Exporter->setup_import_methods(
18 with_meta => [qw(
19 add_xpath_namespace
20 has_xpath_value
21 has_xpath_value_list
22 has_xpath_value_map
23 has_xpath_object
24 has_xpath_object_list
25 has_xpath_object_map
26 finalize_class
27 )],
28 also => 'Moose',
29 );
30  
31  
32 sub add_xpath_namespace {
33 5 ▶ my ($meta, $namespace, $url) = @_;
Subroutine add_xpath_namespace: called CC=2 SCAR=6.9
Pod documented
34 5 my $attr = $meta->find_attribute_by_name('namespace_map');
35 5 ▶ confess("namespace_map attribute not present") unless blessed($attr);
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($attr) 0 5 5
36 5 my $default = $attr->default;
37 5 ▶ my $new_default = sub { my $hash = $default->(@_); $hash->{$namespace} = $url; return $hash; };
Subroutine __ANON__: called CC=1 SCAR=0.0
38 5 my $new_attr = $attr->clone_and_inherit_options(default => $new_default);
39 5 $meta->add_attribute($new_attr);
40 5 return 1;
41 }
42  
43  
44 sub has_xpath_value {
45 13 ▶ my ($meta, $attr_name, $xpath_query, @moose_params) = @_;
Subroutine has_xpath_value: called CC=1 SCAR=0.0
Pod documented
46 13 $meta->add_attribute($attr_name,
47 is => 'ro',
48 isa => 'Str',
49 traits => [qw( XPathValue String )],
50 xpath_query => $xpath_query,
51 default => '',
52 @moose_params,
53 );
54 13 return 1;
55 }
56  
57  
58 sub has_xpath_value_list {
59 2 ▶ my ($meta, $attr_name, $xpath_query, @moose_params) = @_;
Subroutine has_xpath_value_list: called CC=1 SCAR=0.2
Pod documented
60 $meta->add_attribute($attr_name,
61 isa => 'ArrayRef[Str]',
62 traits => [qw( XPathValueList Array )],
63 xpath_query => $xpath_query,
64 0 ▶ default => sub { [] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
65 2 @moose_params,
66 );
67 2 return 1;
68 }
69  
70  
71 sub has_xpath_value_map {
72 5 ▶ my ($meta, $attr_name, $xpath_query, $xpath_key, $xpath_value, @moose_params) = @_;
Subroutine has_xpath_value_map: called CC=1 SCAR=0.2
Pod documented
73 $meta->add_attribute($attr_name,
74 isa => 'HashRef[Str]',
75 traits => [qw( XPathValueMap Hash )],
76 xpath_query => $xpath_query,
77 xpath_key => $xpath_key,
78 xpath_value => $xpath_value,
79 0 ▶ default => sub { +{} },
Subroutine __ANON__: not called CC=1 SCAR=6.9
80 5 @moose_params,
81 );
82 5 return 1;
83 }
84  
85  
86  
87 sub has_xpath_object {
88 6 ▶ my ($meta, $attr_name, $xpath_query, $isa, @moose_params) = @_;
Subroutine has_xpath_object: called CC=2 SCAR=7.0
Pod documented
89 6 ▶ my @isa = ref($isa) eq 'HASH'
Branch: 1 branch
BranchTrueFalseTotal
ref $isa eq 'HASH' ? : 0 6 6
90 ? ( isa_map => $isa )
91 : ( isa => $isa )
92 ;
93 6 $meta->add_attribute($attr_name, @isa,
94 traits => [qw( XPathObject )],
95 xpath_query => $xpath_query,
96 @moose_params,
97 );
98 6 return 1;
99 }
100  
101  
102  
103 sub has_xpath_object_list {
104 6 ▶ my ($meta, $attr_name, $xpath_query, $isa, @moose_params) = @_;
Subroutine has_xpath_object_list: called CC=2 SCAR=7.0
Pod documented
105 6 ▶ my @isa = ref($isa) eq 'HASH'
Branch: 1 branch
BranchTrueFalseTotal
ref $isa eq 'HASH' ? : 3 3 6
106 ? ( isa_map => $isa )
107 : ( isa => 'ArrayRef[' . $isa . ']' )
108 ;
109 $meta->add_attribute($attr_name, @isa,
110 traits => [qw( XPathObjectList Array )],
111 xpath_query => $xpath_query,
112 0 ▶ default => sub { +[] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
113 6 @moose_params,
114 );
115 6 return 1;
116 }
117  
118  
119  
120 sub has_xpath_object_map {
121 6 ▶ my ($meta, $attr_name, $xpath_query, $xpath_key, $isa, @moose_params) = @_;
Subroutine has_xpath_object_map: called CC=2 SCAR=7.0
Pod documented
122 6 ▶ my @isa = ref($isa) eq 'HASH'
Branch: 1 branch
BranchTrueFalseTotal
ref $isa eq 'HASH' ? : 1 5 6
123 ? ( isa_map => $isa )
124 : ( isa => 'HashRef[' . $isa . ']' )
125 ;
126 $meta->add_attribute($attr_name, @isa,
127 traits => [qw( XPathObjectMap Hash )],
128 xpath_query => $xpath_query,
129 xpath_key => $xpath_key,
130 0 ▶ default => sub { +{} },
Subroutine __ANON__: not called CC=1 SCAR=6.9
131 6 @moose_params,
132 );
133 6 return 1;
134 }
135  
136  
137 sub finalize_class {
138 17 ▶ my ($meta) = @_;
Subroutine finalize_class: called CC=1 SCAR=0.0
Pod documented
139 17 $meta->make_immutable();
140 17 return 1; # so we can avoid the 1; at the end of the file
141 }
142  
143 7 ▶no Moose::Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
144  
145 7 1;
146  
147 __END__
148  
149 =pod
150  
151 =encoding utf-8
152  
153 =head1 NAME
154  
155 XML::Rabbit::Sugar - Sugar functions for easier declaration of xpath attributes
156  
157 =head1 VERSION
158  
159 version 0.4.0
160  
161 =head1 FUNCTIONS
162  
163 =head2 add_xpath_namespace($namespace, $url)
164  
165 Adds the XPath namespace with its associated url to the namespace_map hash.
166  
167 =head2 has_xpath_value($attr_name, $xpath_query, @moose_params)
168  
169 Extracts a single string according to the xpath query specified. The
170 attribute isa parameter is automatically set to C<Str>. The attribute native
171 trait is automatically set to C<String>.
172  
173 has_xpath_value 'name' => './name',
174 ...
175 ;
176  
177 =head2 has_xpath_value_list($attr_name, $xpath_query, @moose_params)
178  
179 Extracts an array of strings according to the xpath query specified. The
180 attribute isa parameter is automatically set to C<ArrayRef[Str]>. The
181 attribute native trait is automatically set to C<Array>.
182  
183 has_xpath_value_list 'streets' => './street',
184 ...
185 ;
186  
187 =head2 has_xpath_value_map($attr_name, $xpath_query, $xpath_key, $xpath_value, @moose_params)
188  
189 Extracts a hash of strings according to the xpath query specified. The
190 attribute isa parameter is automatically set to C<HashRef[Str]>. The
191 attribute native trait is automatically set to C<Hash>. The xpath query
192 should represent the multiple elements you want to retrieve. The xpath_key
193 and xpath_value queries must specify how to lookup the key and value for
194 each hash entry. Most likely you'd want to use relative queries for the key
195 and value like the example below shows.
196  
197 has_xpath_value_map 'employee_map' => './employees/*',
198 './@ssn' => './name',
199 ...
200 ;
201  
202 =head2 has_xpath_object($attr_name, $xpath_query, $isa, @moose_params)
203  
204 Extracts a single object according to the xpath query specified. The
205 attribute isa parameter is automatically set to the specified class name.
206 In the example below it would be set to C<My::Department>.
207  
208 has_xpath_object 'department' => './department' => 'My::Department';
209  
210 =head2 has_xpath_object($attr_name, $xpath_query, $isa_map, @moose_params)
211  
212 Extracts a single object according to the xpath query specified. The
213 attribute isa parameter is automatically set to a union of the values in the
214 specified hash. In the example below it would be set to
215 C<My::Department|My::Team>.
216  
217 has_xpath_object 'department' => './department|./team' =>
218 {
219 'department' => 'My::Department',
220 'team' => 'My::Team',
221 },
222 ...
223 ;
224  
225 =head2 has_xpath_object_list($attr_name, $xpath_query, $isa, @moose_params)
226  
227 Extracts an array of objects according to the xpath query specified. The
228 attribute isa parameter is automatically set to C<ArrayRef[My::Customer]>
229 (in example below). The attribute native trait is automatically set to
230 C<Array>.
231  
232 has_xpath_object_list 'customers' => './customer' => 'My::Customer';
233  
234 =head2 has_xpath_object_list($attr_name, $xpath_query, $isa_map, @moose_params)
235  
236 Extracts an array of objects according to the xpath query specified. The
237 attribute isa parameter is automatically set to
238 C<ArrayRef[My::Customer|My::Partner]> (in example below). The attribute
239 native trait is automatically set to C<Array>.
240  
241 has_xpath_object_list 'externals' => './customer|./partner' =>
242 {
243 'customer' => 'My::Customer',
244 'partner' => 'My::Partner',
245 },
246 ...
247 ;
248  
249 =head2 has_xpath_object_map($attr_name, $xpath_query, $xpath_key, $isa, @moose_params)
250  
251 Extracts a hash of objects according to the xpath query specified. The
252 attribute isa parameter is automatically set to C<HashRef[My::Product]> (see
253 example). The attribute native trait is automatically set to C<Hash>. The
254 xpath query should represent the multiple elements you want to retrieve.
255 The xpath_key query must specify how to lookup the key for each hash entry.
256 Most likely you'd want to use relative queries for the key like the example
257 below shows.
258  
259 has_xpath_object_map 'product_map' => './products/*',
260 './@code' => 'My::Product',
261 ...
262 ;
263  
264 =head2 has_xpath_object_map($attr_name, $xpath_query, $xpath_key, $isa_map, @moose_params)
265  
266 Extracts a hash of objects according to the xpath query specified. The
267 attribute isa parameter is automatically set to
268 C<HashRef[My::Product|My::Service]> (see example). The attribute native
269 trait is automatically set to C<Hash>. The xpath query should represent the
270 multiple elements you want to retrieve. The xpath_key query must specify
271 how to lookup the key for each hash entry. Most likely you'd want to use
272 relative queries for the key like the example below shows.
273  
274 has_xpath_object_map 'merchandise_map' => './products/*|./services/*',
275 './@code' => {
276 'service' => 'My::Service',
277 'product' => 'My::Product',
278 },
279 ...
280 ;
281  
282 =head2 finalize_class()
283  
284 Convenience function that calls __PACKAGE__->meta->make_immutable() for you.
285 Always returns true value.
286  
287 =head1 AUTHOR
288  
289 Robin Smidsrød <robin@smidsrod.no>
290  
291 =head1 COPYRIGHT AND LICENSE
292  
293 This software is copyright (c) 2013 by Robin Smidsrød.
294  
295 This is free software; you can redistribute it and/or modify it under
296 the same terms as the Perl 5 programming language system itself.
297  
298 =cut
« Rabbit/Root.pm ↑ Summary Rabbit/Trait/XPathValueList.pm »