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