line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Data::Transpose::Iterator::Base; |
2
|
|
|
|
|
|
|
|
3
|
13
|
|
|
13
|
|
22256
|
use strict; |
|
13
|
|
|
|
|
22
|
|
|
13
|
|
|
|
|
583
|
|
4
|
13
|
|
|
13
|
|
60
|
use warnings; |
|
13
|
|
|
|
|
20
|
|
|
13
|
|
|
|
|
406
|
|
5
|
|
|
|
|
|
|
|
6
|
13
|
|
|
13
|
|
603
|
use Moo; |
|
13
|
|
|
|
|
11086
|
|
|
13
|
|
|
|
|
66
|
|
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
=head1 NAME |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
Data::Transpose::Iterator::Base - Iterator for Data::Transpose. |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
This iterator provides basic methods for iteration, like number |
13
|
|
|
|
|
|
|
of records (count) and getting next record (next). |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
=head1 SYNOPSIS |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
$cart = [{isbn => '978-0-2016-1622-4', |
18
|
|
|
|
|
|
|
title => 'The Pragmatic Programmer', |
19
|
|
|
|
|
|
|
quantity => 1}, |
20
|
|
|
|
|
|
|
{isbn => '978-1-4302-1833-3', |
21
|
|
|
|
|
|
|
title => 'Pro Git', |
22
|
|
|
|
|
|
|
quantity => 1}, |
23
|
|
|
|
|
|
|
]; |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
$iter = new Data::Transpose::Iterator::Base(records => $cart); |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
print "Count: ", $iter->count, "\n"; |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
while ($record = $iter->next) { |
30
|
|
|
|
|
|
|
print "Title: ", $record->title(), "\n"; |
31
|
|
|
|
|
|
|
} |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
$iter->reset; |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
$iter->seed({isbn => '978-0-9779201-5-0', |
36
|
|
|
|
|
|
|
title => 'Modern Perl', |
37
|
|
|
|
|
|
|
quantity => 10}); |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
=cut |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
sub BUILDARGS { |
42
|
63
|
|
|
63
|
0
|
132412
|
my ( $class, @args ) = @_; |
43
|
|
|
|
|
|
|
|
44
|
63
|
100
|
|
|
|
234
|
if (@args == 1) { |
45
|
2
|
|
|
|
|
37
|
return {records => $args[0]}; |
46
|
|
|
|
|
|
|
} |
47
|
|
|
|
|
|
|
else { |
48
|
61
|
|
|
|
|
1353
|
return {@args}; |
49
|
|
|
|
|
|
|
} |
50
|
|
|
|
|
|
|
} |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
=head1 ATTRIBUTES |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
=head2 records |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
Creates a Data::Transpose::Iterator::Base object. The elements of the |
57
|
|
|
|
|
|
|
iterator are hash references. They can be passed to the constructor |
58
|
|
|
|
|
|
|
as array or array reference. |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
=cut |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
has records => ( |
63
|
|
|
|
|
|
|
is => 'rw', |
64
|
|
|
|
|
|
|
trigger => 1, |
65
|
|
|
|
|
|
|
); |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
=head2 count |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
Number of elements (if supported). |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
=cut |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
has count => ( |
74
|
|
|
|
|
|
|
is => 'rwp', |
75
|
|
|
|
|
|
|
lazy => 1, |
76
|
|
|
|
|
|
|
default => sub {return 0;}, |
77
|
|
|
|
|
|
|
); |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
=head2 index |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
Current position (starting from 0). |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
=cut |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
has index => ( |
86
|
|
|
|
|
|
|
is => 'rwp', |
87
|
|
|
|
|
|
|
lazy => 1, |
88
|
|
|
|
|
|
|
default => sub {return 0;}, |
89
|
|
|
|
|
|
|
); |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
=head1 METHODS |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
=head2 next |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
Returns next record or undef. |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
=cut |
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
sub next { |
100
|
163
|
|
|
163
|
1
|
5576
|
my ($self) = @_; |
101
|
163
|
|
|
|
|
3494
|
my $index = $self->index; |
102
|
|
|
|
|
|
|
|
103
|
163
|
50
|
|
|
|
6942
|
if ($index <= $self->count) { |
104
|
163
|
|
|
|
|
1432
|
$self->_set_index($index + 1); |
105
|
163
|
|
|
|
|
3201
|
return $self->records->[$index]; |
106
|
|
|
|
|
|
|
} |
107
|
|
|
|
|
|
|
|
108
|
0
|
|
|
|
|
0
|
return; |
109
|
|
|
|
|
|
|
}; |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
=head2 reset |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
Resets iterator. |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
=cut |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
# Reset method - rewind index of iterator |
119
|
|
|
|
|
|
|
sub reset { |
120
|
194
|
|
|
194
|
1
|
1783
|
my ($self) = @_; |
121
|
|
|
|
|
|
|
|
122
|
194
|
|
|
|
|
438
|
$self->_set_index(0); |
123
|
|
|
|
|
|
|
|
124
|
194
|
|
|
|
|
2908
|
return $self; |
125
|
|
|
|
|
|
|
} |
126
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
=head2 seed |
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
Seeds iterator. |
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
=cut |
132
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
sub seed { |
134
|
3
|
|
|
3
|
1
|
2118
|
my ($self, @args) = @_; |
135
|
|
|
|
|
|
|
|
136
|
3
|
100
|
|
|
|
12
|
if (ref($args[0]) eq 'ARRAY') { |
137
|
1
|
|
|
|
|
25
|
$self->records($args[0]); |
138
|
|
|
|
|
|
|
} |
139
|
|
|
|
|
|
|
else { |
140
|
2
|
|
|
|
|
51
|
$self->records(\@args); |
141
|
|
|
|
|
|
|
} |
142
|
|
|
|
|
|
|
|
143
|
3
|
|
|
|
|
55
|
return $self->count; |
144
|
|
|
|
|
|
|
} |
145
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
=head2 sort |
147
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
Sorts records of the iterator. |
149
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
Parameters are: |
151
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
=over 4 |
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
=item $sort |
155
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
Field used for sorting. |
157
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
=item $unique |
159
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
Whether results should be unique (optional). |
161
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
=back |
163
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
=cut |
165
|
|
|
|
|
|
|
|
166
|
|
|
|
|
|
|
sub sort { |
167
|
2
|
|
|
2
|
1
|
2126
|
my ($self, $sort, $unique) = @_; |
168
|
2
|
|
|
|
|
3
|
my (@data, @tmp); |
169
|
|
|
|
|
|
|
|
170
|
2
|
|
|
|
|
3
|
@data = sort {lc($a->{$sort}) cmp lc($b->{$sort})} @{$self->records}; |
|
6
|
|
|
|
|
485
|
|
|
2
|
|
|
|
|
30
|
|
171
|
|
|
|
|
|
|
|
172
|
2
|
100
|
|
|
|
7
|
if ($unique) { |
173
|
1
|
|
|
|
|
2
|
my $sort_value = ''; |
174
|
|
|
|
|
|
|
|
175
|
1
|
|
|
|
|
3
|
for my $record (@data) { |
176
|
3
|
100
|
|
|
|
10
|
next if $record->{$sort} eq $sort_value; |
177
|
2
|
|
|
|
|
3
|
$sort_value = $record->{$sort}; |
178
|
2
|
|
|
|
|
2
|
push (@tmp, $record); |
179
|
|
|
|
|
|
|
} |
180
|
|
|
|
|
|
|
|
181
|
1
|
|
|
|
|
20
|
$self->records(\@tmp); |
182
|
|
|
|
|
|
|
} |
183
|
|
|
|
|
|
|
else { |
184
|
1
|
|
|
|
|
38
|
$self->records(\@data); |
185
|
|
|
|
|
|
|
} |
186
|
|
|
|
|
|
|
} |
187
|
|
|
|
|
|
|
|
188
|
|
|
|
|
|
|
sub _trigger_records { |
189
|
125
|
|
|
125
|
|
9093
|
my ($self, $records) = @_; |
190
|
|
|
|
|
|
|
|
191
|
125
|
50
|
|
|
|
401
|
if (ref($records) eq 'ARRAY') { |
192
|
125
|
|
|
|
|
433
|
$self->_set_count(scalar @$records); |
193
|
|
|
|
|
|
|
} |
194
|
|
|
|
|
|
|
else { |
195
|
0
|
|
|
|
|
0
|
die "Arguments for records should be an arrayref.\n"; |
196
|
|
|
|
|
|
|
} |
197
|
|
|
|
|
|
|
|
198
|
125
|
|
|
|
|
356
|
$self->reset; |
199
|
|
|
|
|
|
|
}; |
200
|
|
|
|
|
|
|
|
201
|
|
|
|
|
|
|
=head1 AUTHOR |
202
|
|
|
|
|
|
|
|
203
|
|
|
|
|
|
|
Stefan Hornburg (Racke), |
204
|
|
|
|
|
|
|
|
205
|
|
|
|
|
|
|
=head1 LICENSE AND COPYRIGHT |
206
|
|
|
|
|
|
|
|
207
|
|
|
|
|
|
|
Copyright 2010-2016 Stefan Hornburg (Racke) . |
208
|
|
|
|
|
|
|
|
209
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify it |
210
|
|
|
|
|
|
|
under the terms of either: the GNU General Public License as published |
211
|
|
|
|
|
|
|
by the Free Software Foundation; or the Artistic License. |
212
|
|
|
|
|
|
|
|
213
|
|
|
|
|
|
|
See http://dev.perl.org/licenses/ for more information. |
214
|
|
|
|
|
|
|
|
215
|
|
|
|
|
|
|
=cut |
216
|
|
|
|
|
|
|
|
217
|
|
|
|
|
|
|
1; |