Valiant/HTML/Util/Collection.pm

filter: Statementstmt 89.4% 34 / 38 Branchbran 100% 6 / 6 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 82.3% 14 / 17 Podpod 100% 8 / 8 totaltotal 89.8% 62 / 69 CC 4 SCAR 3.7
CC
4
Cov
91%
new
2.0
_build_item
2.0
next
2.0
CRAP 1.4
« Valiant/Proxy/Hash.pm ↑ Summary Valiant/Filter/Lower.pm »
1 package Valiant::HTML::Util::Collection;
2  
3 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 5 ▶use Scalar::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 sub new {
8 15 ▶ my ($class) = shift;
Subroutine new: called CC=2 SCAR=6.9
Pod documented
9 my @items = map {
10 15 ▶ Scalar::Util::blessed($_) ? $_ : $class->_build_item($_)
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($_) ? : 19 22 41
11 } @_;
12 15 return bless +{ collection=>\@items, pointer=>0 }, $class;
13 }
14  
15 sub _build_item {
16 22 ▶ my ($class, $item) = @_;
Subroutine _build_item: called CC=2 SCAR=6.9
17 22 ▶ $item = [$item, $item] unless ref($item);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $item 18 4 22
18 22 return bless $item, 'Valiant::HTML::Util::Collection::Item';
19 }
20  
21 sub next {
22 112 ▶ my $self = shift;
Subroutine next: called CC=2 SCAR=6.9
Pod documented
23 112 ▶ if(my $item = $self->{collection}->[$self->{pointer}]) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $item = $self->{'collection'}[$self->{'pointer'}]) { } 84 28 112
24 84 $self->{pointer}++;
25 84 return $item;
26 } else {
27 28 return;
28 }
29 }
30  
31 sub build {
32 3 ▶ my $self = shift;
Subroutine build: called CC=1 SCAR=0.0
Pod documented
33 3 return bless \@_, 'Valiant::HTML::Util::Collection::Item';
34 }
35  
36 0 ▶sub current_index { return shift->{pointer} }
Subroutine current_index: not called CC=1 SCAR=6.9
Pod documented
37  
38 0 ▶sub current_item { return $_[0]->{collection}->[$_[0]->{pointer}] }
Subroutine current_item: not called CC=1 SCAR=6.9
Pod documented
39  
40 0 ▶sub size { scalar @{$_[0]->{collection}} }
Subroutine size: not called CC=1 SCAR=6.9
Pod documented
41  
42 23 ▶sub reset { $_[0]->{pointer} = 0 }
Subroutine reset: called CC=1 SCAR=0.0
Pod documented
43  
44 5 ▶sub all { @{$_[0]->{collection}} }
Subroutine all: called CC=1 SCAR=0.0
Pod documented
45  
46 package Valiant::HTML::Util::Collection::Item;
47  
48 use overload
49 166 ▶ bool => sub { shift->value },
Subroutine __ANON__: called CC=1 SCAR=0.0
50 9 ▶ '""' => sub { shift->value },
Subroutine __ANON__: called CC=1 SCAR=0.0
51 5 ▶ fallback => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
52  
53 32 ▶sub label { return shift->[0] }
Subroutine label: called CC=1 SCAR=0.0
54 212 ▶sub value { return shift->[1] }
Subroutine value: called CC=1 SCAR=0.0
55  
56 5 1;
57  
58 =head1 NAME
59  
60 Valiant::HTML::Util::Collection - A utility class for creating collections of items.
61  
62 =head1 SYNOPSIS
63  
64 use Valiant::HTML::Util::Collection;
65  
66 my $collection = Valiant::HTML::Util::Collection->new(@items);
67  
68 while(my $item = $collection->next) {
69 # do something with $item
70 }
71  
72 $collection->reset;
73  
74 my $size = $collection->size;
75  
76 =head1 DESCRIPTION
77  
78 Valiant::HTML::Util::Collection is a utility class for creating collections of items. The collection can
79 contain any type of item, but each item is represented as a LValiant::HTML::Util::Collection::Item object.
80  
81 =head1 METHODS
82  
83 =over 4
84  
85 =item new(@items)
86  
87 Constructs a new Valiant::HTML::Util::Collection object with the provided items. Each item can be any type of
88 scalar value, or an object that has been blessed into the Valiant::HTML::Util::Collection::Item package.
89  
90 =item next
91  
92 Returns the next item in the collection, or undef if there are no more items. The current position within
93 the collection is advanced by one.
94  
95 =item build
96  
97 Constructs a new Valiant::HTML::Util::Collection::Item object from the provided arguments. The first argument
98 is the label, and the second argument is the value.
99  
100 =item current_index
101  
102 Returns the current position within the collection.
103  
104 =item current_item
105  
106 Returns the current item in the collection.
107  
108 =item size
109  
110 Returns the size of the collection.
111  
112 =item reset
113  
114 Resets the current position within the collection to the beginning.
115  
116 =item all
117  
118 Returns all of the items in the collection as a list.
119  
120 =back
121  
122 =head1 SEE ALSO
123  
124 L<Valiant>, L<Valiant::HTML::SafeString>, L<Valiant::HTML::FormBuilder>
125  
126 =head1 AUTHOR
127
128 See L<Valiant>
129  
130 =head1 COPYRIGHT & LICENSE
131
132 See L<Valiant>
133  
134 =cut
« Valiant/Proxy/Hash.pm ↑ Summary Valiant/Filter/Lower.pm »