File Coverage

blib/lib/PickLE/Property.pm
Criterion Covered Total %
statement 29 30 96.6
branch 6 8 75.0
condition n/a
subroutine 7 7 100.0
pod 2 2 100.0
total 44 47 93.6


line stmt bran cond sub pod time code
1             #!/usr/bin/env perl
2              
3             =head1 NAME
4              
5             C<PickLE::Property> - Representation of a header property in a pick list
6              
7             =cut
8              
9              
10             use strict;
11 2     2   91479 use warnings;
  2         4  
  2         52  
12 2     2   23 use Carp;
  2         3  
  2         45  
13 2     2   8 use Moo;
  2         4  
  2         95  
14 2     2   485  
  2         6425  
  2         9  
15             =head1 ATTRIBUTES
16              
17             =over 4
18              
19             =item I<name>
20              
21             Name of the property.
22              
23             =cut
24              
25             has name => (
26             is => 'rw',
27             );
28              
29             =item I<value>
30              
31             Value of the property.
32              
33             =cut
34              
35             has value => (
36             is => 'rw',
37             );
38              
39             =back
40              
41             =head1 METHODS
42              
43             =over 4
44              
45             =item I<$prop> = C<PickLE::Property>->C<new>([I<name>, I<value>])
46              
47             Initializes a pick list property object with a I<name> and I<value>.
48              
49             =item I<$prop> = C<PickLE::Property>->C<from_line>(I<$line>)
50              
51             =item I<$prop> = I<$prop>->C<from_line>(I<$line>)
52              
53             This method can be called statically, in which it will initialize a brand new
54             property object or in object context in which it'll override just the attributes
55             of the object and leave the instance intact.
56              
57             In both variants it'll parse a property I<$line> from a document and populate
58             the object. Will return C<undef> if we couldn't parse a property from the given
59             line.
60              
61             =cut
62              
63             my ($self, $line) = @_;
64             $self = $self->new() unless ref $self;
65 12     12 1 18  
66 12 50       176 # Try to parse the property line.
67             if ($line =~ /(?<name>[A-Za-z0-9 \-]+):\s+(?<value>.+)/) {
68             # Populate our object.
69 12 50       182 $self->name($+{name});
70             $self->value($+{value});
71 1     1   1787  
  1         365  
  1         133  
  12         66  
72 12         48 return $self;
73             }
74 12         31  
75             # Looks like the property line couldn't be parsed.
76             return undef;
77             }
78 0         0  
79             =item I<$str> = I<$prop>->C<as_string>()
80              
81             Gets the string representation of this object. Will return an empty string if
82             the object is poorly populated.
83              
84             =cut
85              
86             my ($self) = @_;
87              
88             # Check if we have a name.
89 6     6 1 1740 if (not defined $self->name) {
90             carp "Property can't be represented because the name is not defined";
91             return '';
92 6 100       24 }
93 1         159  
94 1         57 # Check if we have a value.
95             if (not defined $self->value) {
96             carp "Property can't be represented because the value is not defined";
97             return '';
98 5 100       16 }
99 1         73  
100 1         54 # Properly populated property.
101             return $self->name . ': ' . $self->value;
102             }
103              
104 4         20 1;
105              
106              
107             =back
108              
109             =head1 AUTHOR
110              
111             Nathan Campos <nathan@innoveworkshop.com>
112              
113             =head1 COPYRIGHT
114              
115             Copyright (c) 2022- Nathan Campos.
116              
117             =cut