| 1 | package Fey::ORM::Role::Iterator; | ||||||||||
| 2 | { | ||||||||||
| 3 | 13 | $Fey::ORM::Role::Iterator::VERSION = '0.46'; | |||||||||
| 4 | } | ||||||||||
| 5 | |||||||||||
| 6 | 13 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 13 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 13 | ▶ | use namespace::autoclean; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | |||||||||||
| 10 | 13 | ▶ | use Fey::ORM::Types qw( ArrayRefOfClasses Bool Int ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | 13 | ▶ | use List::AllUtils qw( pairwise ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 12 | |||||||||||
| 13 | 13 | ▶ | use Moose::Role; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 14 | |||||||||||
| 15 | 13 | requires qw( _get_next_result reset ); | |||||||||
| 16 | |||||||||||
| 17 | 13 | has classes => ( | |||||||||
| 18 | is => 'ro', | ||||||||||
| 19 | isa => ArrayRefOfClasses, | ||||||||||
| 20 | coerce => 1, | ||||||||||
| 21 | required => 1, | ||||||||||
| 22 | ); | ||||||||||
| 23 | |||||||||||
| 24 | 13 | has index => ( | |||||||||
| 25 | traits => ['Counter'], | ||||||||||
| 26 | is => 'ro', | ||||||||||
| 27 | isa => Int, | ||||||||||
| 28 | default => 0, | ||||||||||
| 29 | init_arg => undef, | ||||||||||
| 30 | handles => { | ||||||||||
| 31 | _inc_index => 'inc', | ||||||||||
| 32 | _reset_index => 'reset', | ||||||||||
| 33 | }, | ||||||||||
| 34 | ); | ||||||||||
| 35 | |||||||||||
| 36 | has _can_make_hashes => ( | ||||||||||
| 37 | is => 'ro', | ||||||||||
| 38 | isa => Bool, | ||||||||||
| 39 | lazy => 1, | ||||||||||
| 40 | init_arg => undef, | ||||||||||
| 41 | default => sub { | ||||||||||
| 42 | 0 | ▶ | List::AllUtils::all { $_->can('Table') } @{ $_[0]->classes() }; | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 43 | }, | ||||||||||
| 44 | 13 | ); | |||||||||
| 45 | |||||||||||
| 46 | sub next { | ||||||||||
| 47 | 0 | ▶ | my $self = shift; | ||||||||
|
Subroutine
next: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||
| 48 | |||||||||||
| 49 | 0 | my $result = $self->_get_next_result(); | |||||||||
| 50 | |||||||||||
| 51 | 0 | ▶ | return unless $result; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 52 | |||||||||||
| 53 | 0 | $self->_inc_index(); | |||||||||
| 54 | |||||||||||
| 55 | 0 | ▶ | return wantarray ? @{$result} : $result->[0]; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 56 | } | ||||||||||
| 57 | |||||||||||
| 58 | sub next_as_hash { | ||||||||||
| 59 | 0 | ▶ | my $self = shift; | ||||||||
|
Subroutine
next_as_hash: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||
| 60 | |||||||||||
| 61 | 0 | ▶ | die 'Cannot make a hash unless all classes have a Table() method' | ||||||||
|
Branch: 1 branch
| |||||||||||
| 62 | unless $self->_can_make_hashes(); | ||||||||||
| 63 | |||||||||||
| 64 | 0 | my @result = $self->next(); | |||||||||
| 65 | |||||||||||
| 66 | 0 | ▶ | return unless @result; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 67 | |||||||||||
| 68 | 0 | ▶ | return pairwise { $a->Table()->name() => $b } @{ $self->classes() }, | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 69 | @result; | ||||||||||
| 70 | } | ||||||||||
| 71 | |||||||||||
| 72 | sub all { | ||||||||||
| 73 | 0 | ▶ | my $self = shift; | ||||||||
|
Subroutine
all: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||
| 74 | |||||||||||
| 75 | 0 | ▶ | $self->reset() if $self->index(); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 76 | |||||||||||
| 77 | 0 | return $self->remaining(); | |||||||||
| 78 | } | ||||||||||
| 79 | |||||||||||
| 80 | sub all_as_hashes { | ||||||||||
| 81 | 0 | ▶ | my $self = shift; | ||||||||
|
Subroutine
all_as_hashes: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||
| 82 | |||||||||||
| 83 | 0 | ▶ | $self->reset() if $self->index(); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 84 | |||||||||||
| 85 | 0 | return $self->remaining_as_hashes(); | |||||||||
| 86 | } | ||||||||||
| 87 | |||||||||||
| 88 | sub remaining { | ||||||||||
| 89 | 0 | ▶ | my $self = shift; | ||||||||
|
Subroutine
remaining: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||
| 90 | |||||||||||
| 91 | 0 | my @result; | |||||||||
| 92 | 0 | ▶ | while ( my @r = $self->next() ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 93 | 0 | ▶ | push @result, @r == 1 ? @r : \@r; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 94 | } | ||||||||||
| 95 | |||||||||||
| 96 | 0 | return @result; | |||||||||
| 97 | } | ||||||||||
| 98 | |||||||||||
| 99 | sub remaining_as_hashes { | ||||||||||
| 100 | 0 | ▶ | my $self = shift; | ||||||||
|
Subroutine
remaining_as_hashes: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||
| 101 | |||||||||||
| 102 | 0 | my @result; | |||||||||
| 103 | 0 | ▶ | while ( my %r = $self->next_as_hash() ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 104 | 0 | push @result, \%r; | |||||||||
| 105 | } | ||||||||||
| 106 | |||||||||||
| 107 | 0 | return @result; | |||||||||
| 108 | } | ||||||||||
| 109 | |||||||||||
| 110 | 13 | 1; | |||||||||
| 111 | |||||||||||
| 112 | # ABSTRACT: A role for things that iterate over Fey::Object::Table objects | ||||||||||
| 113 | |||||||||||
| 114 | __END__ | ||||||||||
| 115 | |||||||||||
| 116 | =pod | ||||||||||
| 117 | |||||||||||
| 118 | =head1 NAME | ||||||||||
| 119 | |||||||||||
| 120 | Fey::ORM::Role::Iterator - A role for things that iterate over Fey::Object::Table objects | ||||||||||
| 121 | |||||||||||
| 122 | =head1 VERSION | ||||||||||
| 123 | |||||||||||
| 124 | version 0.46 | ||||||||||
| 125 | |||||||||||
| 126 | =head1 SYNOPSIS | ||||||||||
| 127 | |||||||||||
| 128 | package My::Iterator; | ||||||||||
| 129 | |||||||||||
| 130 | use Moose; | ||||||||||
| 131 | |||||||||||
| 132 | with 'Fey::ORM::Role::Iterator'; | ||||||||||
| 133 | |||||||||||
| 134 | =head1 DESCRIPTION | ||||||||||
| 135 | |||||||||||
| 136 | This role provides some common methods used by | ||||||||||
| 137 | C<Fey::Object::Iterator> classes, as well as defining a consistent | ||||||||||
| 138 | interface for iterators. | ||||||||||
| 139 | |||||||||||
| 140 | =head1 REQUIRED METHODS | ||||||||||
| 141 | |||||||||||
| 142 | Classes which consume this role must provide C<_get_next_result()> and | ||||||||||
| 143 | C<reset()> methods. | ||||||||||
| 144 | |||||||||||
| 145 | =head1 PROVIDED ATTRIBUTES | ||||||||||
| 146 | |||||||||||
| 147 | This role provides the following attributes. | ||||||||||
| 148 | |||||||||||
| 149 | =head2 $iterator->classes() | ||||||||||
| 150 | |||||||||||
| 151 | An array reference of class names. Each class must be a subclass of | ||||||||||
| 152 | L<Fey::Object::Table>. | ||||||||||
| 153 | |||||||||||
| 154 | =head2 $iterator->index() | ||||||||||
| 155 | |||||||||||
| 156 | The current iterator index. Also provides C<_inc_index()> and | ||||||||||
| 157 | C<_reset_index()> methods. | ||||||||||
| 158 | |||||||||||
| 159 | =head1 PROVIDED METHODS | ||||||||||
| 160 | |||||||||||
| 161 | This role provides the following methods. These methods are documented | ||||||||||
| 162 | in L<Fey::Object::Iterator::FromSelect>. | ||||||||||
| 163 | |||||||||||
| 164 | =head2 $iterator->next | ||||||||||
| 165 | |||||||||||
| 166 | =head2 $iterator->next_as_hash() | ||||||||||
| 167 | |||||||||||
| 168 | =head2 $iterator->all() | ||||||||||
| 169 | |||||||||||
| 170 | =head2 $iterator->all_as_hashes() | ||||||||||
| 171 | |||||||||||
| 172 | =head2 $iterator->remaining() | ||||||||||
| 173 | |||||||||||
| 174 | =head2 $iterator->remaining_as_hashes() | ||||||||||
| 175 | |||||||||||
| 176 | =head1 AUTHOR | ||||||||||
| 177 | |||||||||||
| 178 | Dave Rolsky <autarch@urth.org> | ||||||||||
| 179 | |||||||||||
| 180 | =head1 COPYRIGHT AND LICENSE | ||||||||||
| 181 | |||||||||||
| 182 | This software is copyright (c) 2011 by Dave Rolsky. | ||||||||||
| 183 | |||||||||||
| 184 | This is free software; you can redistribute it and/or modify it under | ||||||||||
| 185 | the same terms as the Perl 5 programming language system itself. | ||||||||||
| 186 | |||||||||||
| 187 | =cut | ||||||||||