File Coverage

blib/lib/PickLE/Category.pm
Criterion Covered Total %
statement 29 34 85.2
branch 4 6 66.6
condition n/a
subroutine 8 9 88.8
pod 4 4 100.0
total 45 53 84.9


line stmt bran cond sub pod time code
1             #!/usr/bin/env perl
2              
3             =head1 NAME
4              
5             C<PickLE::Category> - Representation of a category in a pick list
6              
7             =cut
8              
9              
10             use strict;
11 2     2   86727 use warnings;
  2         11  
  2         48  
12 2     2   9 use Carp;
  2         3  
  2         38  
13 2     2   8 use Moo;
  2         4  
  2         79  
14 2     2   482  
  2         6129  
  2         8  
15             =head1 ATTRIBUTES
16              
17             =over 4
18              
19             =item I<name>
20              
21             Name of the category.
22              
23             =cut
24              
25             has name => (
26             is => 'rw',
27             );
28              
29             =item I<components>
30              
31             List of components to be picked from this category.
32              
33             =cut
34              
35             has components => (
36             is => 'ro',
37             lazy => 1,
38             default => sub { [] },
39             writer => '_set_components'
40             );
41              
42             =back
43              
44             =head1 METHODS
45              
46             =over 4
47              
48             =item I<$category> = C<PickLE::Category>->C<new>([I<name>, I<components>])
49              
50             Initializes a pick list category object with a I<name> and some I<components>.
51              
52             =item I<$category> = C<PickLE::Category>->C<from_line>(I<$line>)
53              
54             =item I<$category> = I<$category>->C<from_line>(I<$line>)
55              
56             This method can be called statically, in which it will initialize a brand new
57             category object or in object context in which it'll override just the attributes
58             of the object and leave the instance intact.
59              
60             In both variants it'll parse a category I<$line> from a document and populate
61             the object. Will return C<undef> if we couldn't parse a category from the given
62             line.
63              
64             =cut
65              
66             my ($self, $line) = @_;
67             $self = $self->new() unless ref $self;
68 22     22 1 39  
69 22 50       300 # Try to parse the category line.
70             if ($line =~ /(?<name>[^:]+):\s*/) {
71             # Populate our object.
72 22 50       313 $self->name($+{name});
73              
74 22     1   139 return $self;
  1         1688  
  1         356  
  1         179  
75             }
76 22         60  
77             # Looks like the category line couldn't be parsed.
78             return undef;
79             }
80 0         0  
81             =item I<$category>->C<add_component>(I<@component>)
82              
83             Adds any number of components in the form of L<PickLE::Component> objects to the
84             category.
85              
86             =cut
87              
88             my $self = shift;
89              
90             # Go through components adding them to the components list.
91 58     58 1 2015 foreach my $component (@_) {
92             push @{$self->components}, $component;
93             }
94 58         144 }
95 58         62  
  58         796  
96             =item I<$category>->C<foreach_component>(I<$coderef>)
97              
98             Executes a block of code (I<$coderef>) for each component available in this
99             category. The component object will be passed as the first argument.
100              
101             =cut
102              
103             my ($self, $coderef) = @_;
104              
105             # Go through the components.
106             foreach my $component (@{$self->components}) {
107 0     0 1 0 $coderef->($component);
108             }
109             }
110 0         0  
  0         0  
111 0         0 =item I<$str> = I<$category>->C<as_string>()
112              
113             Gets the string representation of this object. Won't include any of the
114             associated components and will return an empty string if a I<name> isn't
115             defined.
116              
117             =cut
118              
119             my ($self) = @_;
120              
121             # Check if we have a name.
122             if (not defined $self->name) {
123             carp "Category can't be represented because the name is not defined";
124 2     2 1 1721 return '';
125             }
126              
127 2 100       38 # Properly populated category.
128 1         156 return $self->name . ':';
129 1         56 }
130              
131             1;
132              
133 1         10  
134             =back
135              
136             =head1 AUTHOR
137              
138             Nathan Campos <nathan@innoveworkshop.com>
139              
140             =head1 COPYRIGHT
141              
142             Copyright (c) 2022- Nathan Campos.
143              
144             =cut