| 1 | package DBIx::Class::Util::ResultSet::Iterator; | ||||||||||||||
| 2 | |||||||||||||||
| 3 | 1 | ▶ | use Moo; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 4 | |||||||||||||||
| 5 | 1 | has resultset => (is=>'ro', required=>1); | |||||||||||||
| 6 | 1 | has index => (is=>'rw', predicate=>'_has_index'); | |||||||||||||
| 7 | 1 | has _escape => (is=>'rw', predicate=>'has_escaped'); | |||||||||||||
| 8 | |||||||||||||||
| 9 | 30 | ▶ | sub _init_or_inc_index { | ||||||||||||
|
Subroutine
_init_or_inc_index: called CC=2 SCAR=6.9
| |||||||||||||||
| 10 | 30 | my $self = shift; | |||||||||||||
| 11 | 30 | ▶ | if($self->_has_index) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 12 | 23 | $self->index($self->index +1); | |||||||||||||
| 13 | } else { | ||||||||||||||
| 14 | 7 | $self->index(0); | |||||||||||||
| 15 | } | ||||||||||||||
| 16 | } | ||||||||||||||
| 17 | |||||||||||||||
| 18 | 30 | ▶ | sub count { shift->index + 1 } | ||||||||||||
|
Subroutine
count: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 19 | |||||||||||||||
| 20 | 1 | ▶ | sub escape { shift->_escape(1) } | ||||||||||||
|
Subroutine
escape: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 21 | 4 | ▶ | sub has_not_been_used { shift->_has_index ? 0:1 } | ||||||||||||
|
Subroutine
has_not_been_used: called CC=2 SCAR=6.9
Pod
undocumented
Branch: 1 branch
| |||||||||||||||
| 22 | |||||||||||||||
| 23 | 35 | ▶ | sub is_first { shift->index == 0 ? 1:0 } | ||||||||||||
|
Subroutine
is_first: called CC=2 SCAR=6.9
Pod
documented
Branch: 1 branch
| |||||||||||||||
| 24 | 5 | ▶ | sub is_not_first { shift->index == 0 ? 0:1 } | ||||||||||||
|
Subroutine
is_not_first: called CC=2 SCAR=6.9
Pod
documented
Branch: 1 branch
| |||||||||||||||
| 25 | |||||||||||||||
| 26 | 37 | ▶ | sub is_even { shift->index % 2 ? 1:0 } | ||||||||||||
|
Subroutine
is_even: called CC=2 SCAR=6.9
Pod
documented
Branch: 1 branch
| |||||||||||||||
| 27 | 43 | ▶ | sub is_odd { shift->index % 2 ? 0:1 } | ||||||||||||
|
Subroutine
is_odd: called CC=2 SCAR=6.9
Pod
documented
Branch: 1 branch
| |||||||||||||||
| 28 | |||||||||||||||
| 29 | 5 | ▶ | sub first { | ||||||||||||
|
Subroutine
first: called CC=3 SCAR=11.3
Pod
documented
| |||||||||||||||
| 30 | 5 | my ($self, $code, $fail) = @_; | |||||||||||||
| 31 | 5 | ▶ | if($self->is_first) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 32 | 1 | $code->($self); | |||||||||||||
| 33 | } elsif($fail) { | ||||||||||||||
| 34 | 0 | $fail->($self); | |||||||||||||
| 35 | } | ||||||||||||||
| 36 | 5 | return $self; | |||||||||||||
| 37 | } | ||||||||||||||
| 38 | |||||||||||||||
| 39 | 5 | ▶ | sub not_first { | ||||||||||||
|
Subroutine
not_first: called CC=3 SCAR=11.3
Pod
documented
| |||||||||||||||
| 40 | 5 | my ($self, $code, $fail) = @_; | |||||||||||||
| 41 | 5 | ▶ | if($self->is_not_first) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 42 | 4 | $code->($self); | |||||||||||||
| 43 | } elsif($fail) { | ||||||||||||||
| 44 | 0 | $fail->($self); | |||||||||||||
| 45 | } | ||||||||||||||
| 46 | 5 | return $self; | |||||||||||||
| 47 | } | ||||||||||||||
| 48 | |||||||||||||||
| 49 | 5 | ▶ | sub even { | ||||||||||||
|
Subroutine
even: called CC=3 SCAR=11.3
Pod
documented
| |||||||||||||||
| 50 | 5 | my ($self, $code, $fail) = @_; | |||||||||||||
| 51 | 5 | ▶ | if($self->is_even) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 52 | 2 | $code->($self); | |||||||||||||
| 53 | } elsif($fail) { | ||||||||||||||
| 54 | 0 | $fail->($self); | |||||||||||||
| 55 | } | ||||||||||||||
| 56 | 5 | return $self; | |||||||||||||
| 57 | } | ||||||||||||||
| 58 | |||||||||||||||
| 59 | 5 | ▶ | sub odd { | ||||||||||||
|
Subroutine
odd: called CC=3 SCAR=11.3
Pod
documented
| |||||||||||||||
| 60 | 5 | my ($self, $code, $fail) = @_; | |||||||||||||
| 61 | 5 | ▶ | if($self->is_odd) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 62 | 3 | $code->($self); | |||||||||||||
| 63 | } elsif($fail) { | ||||||||||||||
| 64 | 0 | $fail->($self); | |||||||||||||
| 65 | } | ||||||||||||||
| 66 | 5 | return $self; | |||||||||||||
| 67 | } | ||||||||||||||
| 68 | |||||||||||||||
| 69 | 38 | ▶ | sub next { | ||||||||||||
|
Subroutine
next: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||
| 70 | 38 | my $self = shift; | |||||||||||||
| 71 | 38 | ▶ | if(my $next = $self->resultset->next) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 72 | 30 | $self->_init_or_inc_index; | |||||||||||||
| 73 | 30 | return $next; | |||||||||||||
| 74 | } else { | ||||||||||||||
| 75 | 8 | return; | |||||||||||||
| 76 | } | ||||||||||||||
| 77 | } | ||||||||||||||
| 78 | |||||||||||||||
| 79 | 15 | ▶ | sub if { | ||||||||||||
|
Subroutine
if: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||
| 80 | 15 | my ($self, $cond_spec, $pass_cr, $fail_cr, @args) = @_; | |||||||||||||
| 81 | 15 | ▶ | my $cond = ref $cond_spec ? $cond_spec->($self) : $cond_spec; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 82 | 15 | ▶ | if($cond) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 83 | 8 | $pass_cr->($self, @args); | |||||||||||||
| 84 | } elsif($fail_cr) { | ||||||||||||||
| 85 | 7 | $fail_cr->($self, @args); | |||||||||||||
| 86 | } | ||||||||||||||
| 87 | 15 | return $self; | |||||||||||||
| 88 | } | ||||||||||||||
| 89 | |||||||||||||||
| 90 | 1 | 1; | |||||||||||||
| 91 | |||||||||||||||
| 92 | =head1 NAME | ||||||||||||||
| 93 | |||||||||||||||
| 94 | DBIx::Class::Util::ResultSet::Iterator - Iterator Helper | ||||||||||||||
| 95 | |||||||||||||||
| 96 | =head1 SYNOPSIS | ||||||||||||||
| 97 | |||||||||||||||
| 98 | Given a L<DBIx::Class::ResultSet> wrap a basic iterator object around it | ||||||||||||||
| 99 | |||||||||||||||
| 100 | my $rs = $schema->resultset('Bar'); | ||||||||||||||
| 101 | my $itr = DBIx::Class::Util::ResultSet::Iterator->new(resultset=>$rs); | ||||||||||||||
| 102 | while(my $row = $itr->next) { | ||||||||||||||
| 103 | ... | ||||||||||||||
| 104 | } | ||||||||||||||
| 105 | |||||||||||||||
| 106 | =head1 DESCRIPTION | ||||||||||||||
| 107 | |||||||||||||||
| 108 | A L<DBIx::Class::ResultSet> doesn't give you a lot of information by default | ||||||||||||||
| 109 | that you might wish to have, such as the location one is at in the set, etc. | ||||||||||||||
| 110 | This wraps a small class around the resultset to provide these. | ||||||||||||||
| 111 | |||||||||||||||
| 112 | =head1 METHODS | ||||||||||||||
| 113 | |||||||||||||||
| 114 | This component defines the following methods. | ||||||||||||||
| 115 | |||||||||||||||
| 116 | =head2 index | ||||||||||||||
| 117 | |||||||||||||||
| 118 | A positive number starting from zero which is the location in the set the | ||||||||||||||
| 119 | current row is at. | ||||||||||||||
| 120 | |||||||||||||||
| 121 | =head2 count | ||||||||||||||
| 122 | |||||||||||||||
| 123 | A positive number starting from one which is the location in the set the | ||||||||||||||
| 124 | current row is at. | ||||||||||||||
| 125 | |||||||||||||||
| 126 | =head2 escape | ||||||||||||||
| 127 | |||||||||||||||
| 128 | Upon completion of the current, stop execution and return the resultset at the | ||||||||||||||
| 129 | current state. | ||||||||||||||
| 130 | |||||||||||||||
| 131 | =head2 is_first | ||||||||||||||
| 132 | |||||||||||||||
| 133 | Returns boolean true if the current row is the first in the set | ||||||||||||||
| 134 | |||||||||||||||
| 135 | =head2 is_not_first | ||||||||||||||
| 136 | |||||||||||||||
| 137 | Returns boolean true if the current row is NOT the first in the set | ||||||||||||||
| 138 | |||||||||||||||
| 139 | =head2 is_even | ||||||||||||||
| 140 | |||||||||||||||
| 141 | Returns true if the count of the location in the set is even | ||||||||||||||
| 142 | |||||||||||||||
| 143 | =head2 is_odd | ||||||||||||||
| 144 | |||||||||||||||
| 145 | Returns true if the count of the location in the set is odd | ||||||||||||||
| 146 | |||||||||||||||
| 147 | =head2 resultset | ||||||||||||||
| 148 | |||||||||||||||
| 149 | Accessor for the raw L<DBIx::Class::ResultSet> we are wrapping. | ||||||||||||||
| 150 | |||||||||||||||
| 151 | =head2 first | ||||||||||||||
| 152 | |||||||||||||||
| 153 | Args: $coderef, ?$if_empty | ||||||||||||||
| 154 | |||||||||||||||
| 155 | If the current row is first in the set, execute a C<$coderef>, otherwise | ||||||||||||||
| 156 | execute a C<$if_empty> coderef. Returns the C<$each> object so you can chain. | ||||||||||||||
| 157 | |||||||||||||||
| 158 | =head2 not_first | ||||||||||||||
| 159 | |||||||||||||||
| 160 | Args: $coderef, ?$if_empty | ||||||||||||||
| 161 | |||||||||||||||
| 162 | If the current row is NOT the first in the set, execute a C<$coderef>, otherwise | ||||||||||||||
| 163 | execute a C<$if_empty> coderef. Returns the C<$each> object so you can chain. | ||||||||||||||
| 164 | |||||||||||||||
| 165 | =head2 even | ||||||||||||||
| 166 | |||||||||||||||
| 167 | Args: $coderef, ?$if_empty | ||||||||||||||
| 168 | |||||||||||||||
| 169 | If the current row is even in the set, execute a C<$coderef>, otherwise | ||||||||||||||
| 170 | execute a C<$if_empty> coderef. Returns the C<$each> object so you can chain. | ||||||||||||||
| 171 | |||||||||||||||
| 172 | $each->even( | ||||||||||||||
| 173 | sub { print "Current item is even in index" }, | ||||||||||||||
| 174 | ); | ||||||||||||||
| 175 | |||||||||||||||
| 176 | =head2 odd | ||||||||||||||
| 177 | |||||||||||||||
| 178 | Args: $coderef, ?$if_empty | ||||||||||||||
| 179 | |||||||||||||||
| 180 | If the current row is odd in the set, execute a C<$coderef>, otherwise | ||||||||||||||
| 181 | execute a C<$if_empty> coderef. Returns the C<$each> object so you can chain. | ||||||||||||||
| 182 | |||||||||||||||
| 183 | $each->odd( | ||||||||||||||
| 184 | sub { print "Current item is odd in index" }, | ||||||||||||||
| 185 | ); | ||||||||||||||
| 186 | |||||||||||||||
| 187 | =head2 next | ||||||||||||||
| 188 | |||||||||||||||
| 189 | Return the next row in the set or undef. | ||||||||||||||
| 190 | |||||||||||||||
| 191 | =head2 if | ||||||||||||||
| 192 | |||||||||||||||
| 193 | Arguments: $cond|$cond_codered, $pass_coderef, ?$fail_coderef, ?@args | ||||||||||||||
| 194 | Returns: $self | ||||||||||||||
| 195 | |||||||||||||||
| 196 | Given a condition, execute either a pass or fail anonymous subroutine. The | ||||||||||||||
| 197 | fail coderef is optional and the method returns C<$self> for chaining. | ||||||||||||||
| 198 | |||||||||||||||
| 199 | $each->if | ||||||||||||||
| 200 | ( | ||||||||||||||
| 201 | $a>$b, | ||||||||||||||
| 202 | sub { warn "$a > $b" }, | ||||||||||||||
| 203 | sub { warn "$a < $b" }, | ||||||||||||||
| 204 | ); | ||||||||||||||
| 205 | |||||||||||||||
| 206 | If the condition is a coderef, then C<$self> is passed as an argument along | ||||||||||||||
| 207 | with any other C<@args> and the return is considered in boolean context. | ||||||||||||||
| 208 | |||||||||||||||
| 209 | $each->if | ||||||||||||||
| 210 | ( | ||||||||||||||
| 211 | sub { shift->is_odd }, | ||||||||||||||
| 212 | sub { warn "is odd" }, | ||||||||||||||
| 213 | sub { warn "is even" }, | ||||||||||||||
| 214 | ); | ||||||||||||||
| 215 | |||||||||||||||
| 216 | =head1 AUTHOR | ||||||||||||||
| 217 | |||||||||||||||
| 218 | See L<DBIx::Class::ResultSet::SetControl> | ||||||||||||||
| 219 | |||||||||||||||
| 220 | =head1 COPYRIGHT & LICENSE | ||||||||||||||
| 221 | |||||||||||||||
| 222 | See L<DBIx::Class::ResultSet::SetControl> | ||||||||||||||
| 223 | |||||||||||||||
| 224 | =cut | ||||||||||||||