blib/lib/Web/Microformats2/Item.pm

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