blib/lib/Web/Microformats2/Item.pm

filter: Statementstmt 96.0% 73 / 76 Branchbran 70.0% 7 / 10 Conditioncond 44.4% 4 / 9 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 100% 18 / 18 Podpod 57.1% 4 / 7 totaltotal 84.8% 106 / 125 CC 11 SCAR 8.4
CC
11
Cov
88%
add_property
5.5
TO_JSON
3.0
get_property
2.1
CRAP 2.3
« blib/lib/Web/Microformats2/Document.pm ↑ Summary Build »
1 package Web::Microformats2::Item;
2 2 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use MooX::HandlesVia;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use Types::Standard qw(HashRef ArrayRef Str Maybe InstanceOf);
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 has 'properties' => (
8 is => 'ro',
9 isa => HashRef,
10 handles_via => 'Hash',
11 5 ▶ default => sub { {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
12 2 handles => {
13 has_properties => 'count',
14 has_property => 'get',
15 },
16 );
17  
18 has 'p_properties' => (
19 is => 'ro',
20 isa => HashRef,
21 handles_via => 'Hash',
22 5 ▶ default => sub { {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
23 2 handles => {
24 has_p_properties => 'count',
25 has_p_property => 'get',
26 },
27 );
28  
29 has 'u_properties' => (
30 is => 'ro',
31 isa => HashRef,
32 handles_via => 'Hash',
33 5 ▶ default => sub { {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
34 2 handles => {
35 has_u_properties => 'count',
36 has_u_property => 'get',
37 },
38 );
39  
40 has 'e_properties' => (
41 is => 'ro',
42 isa => HashRef,
43 handles_via => 'Hash',
44 5 ▶ default => sub { {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
45 2 handles => {
46 has_e_properties => 'count',
47 has_e_property => 'get',
48 },
49 );
50  
51 has 'dt_properties' => (
52 is => 'ro',
53 isa => HashRef,
54 handles_via => 'Hash',
55 5 ▶ default => sub { {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
56 2 handles => {
57 has_dt_properties => 'count',
58 has_dt_property => 'get',
59 },
60 );
61 2 has 'parent' => (
62 is => 'ro',
63 isa => Maybe[InstanceOf['Web::Microformats2::Item']],
64 weak_ref => 1,
65 );
66  
67 has 'children' => (
68 is => 'ro',
69 isa => ArrayRef[InstanceOf['Web::Microformats2::Item']],
70 5 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
71 2 handles_via => 'Array',
72 handles => {
73 add_child => 'push',
74 has_children => 'count',
75 },
76 );
77  
78 2 has 'types' => (
79 is => 'ro',
80 isa => ArrayRef[Str],
81 required => 1,
82 handles_via => 'Array',
83 handles => {
84 find_type => 'first',
85 },
86  
87 );
88  
89 2 has 'value' => (
90 is => 'rw',
91 isa => Maybe[Str],
92 );
93  
94 2 has 'last_seen_date' => (
95 is => 'rw',
96 isa => Maybe[InstanceOf['DateTime']],
97 );
98  
99 sub add_property {
100 7 ▶ my $self = shift;
Subroutine add_property: called CC=5 SCAR=17.1
Pod undocumented
101  
102 7 my ( $key, $value ) = @_;
103  
104 7 my ( $prefix, $base ) = $key =~ /^(\w+)-(.*)$/;
105  
106 7 ▶ unless ( $prefix && $base ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($prefix and $base) 0 7 7
Condition33%1/3
$prefix and $base
!ll&&!rl&&r
007
MC/DC0%0/2
$prefix and $base
$prefix $base
Truth table33%1/3
A and B
A= $prefixB= $base
ABresult
0X0
100
111
107 0 croak "You must call add_property with the full property name, "
108 . "including its prefix. (e.g. 'p-name', not 'name' )";
109 }
110  
111 7 my $base_method = "properties";
112 7 my $prefix_method = "${prefix}_properties";
113  
114 7 foreach ($base_method, $prefix_method ) {
115 14 ▶ $self->$_->{$base} ||= [];
Condition50%1/2
$self->$_->{$base} ||= []
l!l
014
MC/DC0%0/1
$self->$_->{$base} ||= []
$self->$_->{$base}
Truth table50%1/2
A
A= $self->$_->{$base}
Aresult
11
00
116 14 push @{ $self->$_->{$base} }, $value;
117 }
118 }
119  
120 # add_base_property: Like add_property, but don't look for or insist on
121 # prefixes. Good for inflating from JSON, which has no
122 # prefix information.
123 sub add_base_property {
124 8 ▶ my $self = shift;
Subroutine add_base_property: called CC=2 SCAR=7.0
Pod undocumented
125  
126 8 my ( $key, $value ) = @_;
127  
128 8 ▶ $self->properties->{$key} ||= [];
Condition50%1/2
$self->properties->{$key} ||= []
l!l
08
MC/DC0%0/1
$self->properties->{$key} ||= []
$self->properties->{$key}
Truth table50%1/2
A
A= $self->properties->{$key}
Aresult
11
00
129 8 push @{ $self->properties->{$key} }, $value;
130 }
131  
132 sub get_properties {
133 3 ▶ my $self = shift;
Subroutine get_properties: called CC=2 SCAR=7.1
Pod documented
134  
135 3 my ( $key ) = @_;
136  
137 3 ▶ return $self->{properties}->{$key} || [];
Condition50%1/2
$self->{'properties'}{$key} || []
l!l
30
MC/DC0%0/1
$self->{'properties'}{$key} || []
$self->{'properties'}{$key}
Truth table50%1/2
A
A= $self->{'properties'}{$key}
Aresult
11
00
138 }
139  
140 sub get_property {
141 3 ▶ my $self = shift;
Subroutine get_property: called CC=2 SCAR=7.4
Pod documented
142  
143 3 my $properties_ref = $self->get_properties( @_ );
144  
145 3 ▶ if ( @$properties_ref > 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$properties_ref > 1) 0 3 3
146 0 carp "get_property called with multiple properties set\n";
147 }
148  
149 3 return $properties_ref->[0];
150  
151 }
152  
153 sub has_type {
154 4 ▶ my $self = shift;
Subroutine has_type: called CC=1 SCAR=0.0
Pod documented
155 4 my ( $type ) = @_;
156  
157 4 $type =~ s/^h-//;
158  
159 4 ▶ return $self->find_type( sub { $_ eq $type } );
Subroutine __ANON__: called CC=1 SCAR=0.0
160 }
161  
162 sub all_types {
163 2 ▶ my $self = shift;
Subroutine all_types: called CC=2 SCAR=6.9
Pod documented
164 2 my ( %args ) = @_;
165  
166 2 my @types = @{ $self->types };
167  
168 # We add the 'h-' prefix to returned types unless told not to.
169 2 ▶ unless ( $args{ 'no_prefixes' } ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($args{"no_prefixes"}) 1 1 2
170 1 @types = map { "h-$_" } @types;
171 }
172  
173 2 return @types;
174 }
175  
176 sub TO_JSON {
177 4 ▶ my $self = shift;
Subroutine TO_JSON: called CC=3 SCAR=11.1
Pod undocumented
178  
179 my $data = {
180 properties => $self->properties,
181 4 type => [ map { "h-$_" } @{ $self->types } ],
182 };
183 4 ▶ if ( defined $self->value ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->value) 2 2 4
184 2 $data->{value} = $self->value;
185 }
186 4 ▶ if ( @{$self->children} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$self->children;}) 0 4 4
187 0 $data->{children} = $self->children;
188 }
189 4 return $data;
190 }
191  
192 2 1;
193  
194  
195 =pod
196  
197 =head1 NAME
198  
199 Web::Microformats2::Item - A parsed Microformats2 item
200  
201 =head1 DESCRIPTION
202  
203 An object of this class represents a Microformats2 item, contained
204 within a larger, parsed Microformats2 data structure. An item represents
205 a single semantically meaningful something-or-other: an article, a
206 person, a photograph, et cetera.
207  
208 The expected use-case is that you will never directly construct item
209 objects. Instead, your code will receive item objects by querying a
210 L<Web::Microformats2::Document> instance, itself created by running
211 Microformats2-laden HTML or JSON through a L<Web::Microformats2::Parser>
212 object.
213  
214 See L<Web::Microformats2> for further context and purpose.
215  
216 =head1 METHODS
217  
218 =head2 Object Methods
219  
220 =head3 get_properties
221  
222 $properties_ref = $item->get_properties( $property_key )
223  
224 # To get all "u-url" properties of this item:
225 $urls = $item->get_properties( 'url' );
226 # To get all "p-name" properties:
227 $names = $item->get_properties( 'name' );
228  
229 Returns a list reference of all property values identified by the given
230 key. Values can be strings, unblessed references, or more item objects.
231  
232 Note that Microformats2 stores its properties' keys without their
233 prefixes, so that's how you should query for them here. In order words,
234 pass in "name" or "url" as an argument, not "p-name" or "u-url".
235  
236 =head3 get_property
237  
238 $property_value = $item->get_property( $property_key );
239  
240 # To get the first (and maybe only?) "u-url" property of this item:
241 $url = $item->get_property( 'url' );
242 # To get its "p-name" property:
243 $name = $item->get_property( 'name' );
244  
245 Like L<"get_properties">, but returns only the first property value identified
246 by the given key.
247  
248 If this item contains more than one such value, this will also emit a warning.
249  
250 =head3 value
251  
252 $value = $item->value;
253  
254 Returns the special C<value> attribute, if this item has it defined.
255  
256 =head3 all_types
257  
258 my @types = $item->all_types;
259 # Returns e.g. ( 'h-org', 'h-card' )
260  
261 my @short_types = $item->all_types( no_prefixes => 1 );
262 # Returns e.g. ( 'org', 'card' )
263  
264 Returns a list of all the types that this item identifies as.
265 Guaranteed to contain at least one value.
266  
267 Takes an argument hash, which recognizes the following single key:
268  
269 =over
270  
271 =item no_prefixes
272  
273 If set to a true value, then this object will remove the prefixes from
274 the list of item types before returning it to you. Practically speaking,
275 this means that it will remove the leading C<h-> from every type.
276  
277 =back
278  
279 =head3 has_type
280  
281 $bool = $item->has_type( $type )
282  
283 Returns true if the item claims to be of the given type, false
284 otherwise. (The argument can include a prefix, so e.g. both "entry" and
285 "h-entry" return the same result here.)
286  
287 =head3 parent
288  
289 $parent = $item->parent;
290  
291 Returns this item's parent item, if set.
292  
293 =head3 children
294  
295 $children_ref = $item->children;
296  
297 Returns a list reference of child items.
298  
299 =head1 AUTHOR
300  
301 Jason McIntosh (jmac@jmac.org)
302  
303 =head1 COPYRIGHT AND LICENSE
304  
305 This software is Copyright (c) 2018 by Jason McIntosh.
306  
307 This is free software, licensed under:
308  
309 The MIT (X11) License
« blib/lib/Web/Microformats2/Document.pm ↑ Summary Build »