File Coverage

blib/lib/MooseX/Iterator.pm
Criterion Covered Total %
statement 1 3 33.3
branch n/a
condition n/a
subroutine 1 1 100.0
pod n/a
total 2 4 50.0


line stmt bran cond sub pod time code
1             package MooseX::Iterator;
2              
3             our $VERSION = '0.11';
4             our $AUTHORITY = 'cpan:RLB';
5              
6 3     3   79552 use MooseX::Iterator::Array;
  0            
  0            
7             use MooseX::Iterator::Hash;
8             use MooseX::Iterator::Meta::Iterable;
9              
10             1;
11              
12             __END__
13              
14             =pod
15              
16             =head1 NAME
17              
18             MooseX::Iterator - Iterate over collections
19              
20             =head1 SYNOPSIS
21              
22             Access the Iterator directly:
23              
24             use Moose;
25             use MooseX::Iterator;
26              
27             my $iter = MooseX::Iterator::Array->new( collection => [ 1, 2, 3, 4, 5, 6 ] );
28              
29             my $count = 1;
30             while ( $iter->has_next ) {
31             print $iter->next;
32             }
33              
34             Or use the meta class:
35              
36             package TestIterator;
37              
38             use Moose;
39             use MooseX::Iterator;
40              
41             has collection => (
42             is => 'ro',
43             isa => 'HashRef',
44             default => sub { { one => '1', two => '2', three => '3' } },
45             );
46              
47             has iter => (
48             metaclass => 'Iterable',
49             iterate_over => 'collection',
50             );
51              
52             no Moose;
53              
54             package main;
55             use Data::Dumper;
56              
57             my $test = TestIterator->new;
58              
59             my $iter = $test->iter;
60              
61             while ( $iter->has_next ) {
62             my $next = $iter->next;
63             print $next->{'key'} . "\n";
64             print $next->{'value'} . "\n";
65             }
66              
67             =head1 DESCRIPTION
68              
69             This is an attempt to add smalltalk-like streams to Moose. It currently works with ArrayRefs and HashRefs.
70              
71              
72             =over
73              
74             =item next
75              
76             The next method provides the next item in the colletion.
77              
78             For arrays it returns the element of the array
79            
80             For hashs it returns a pair as a hashref with the keys: key and value
81              
82             =item has_next
83              
84             The has_next method is a boolean method that is true if there is another item in the colletion after the current item. and falue if there isn't.
85              
86             =item peek
87              
88             The peek method returns the next item without moving the state of the iterator forward. It returns undef if it is at the end of the collection.
89              
90             =item reset
91              
92             Resets the cursor, so you can iterate through the elements again.
93              
94             =back
95              
96             =item Subclassing MooseX::Iterator::Meta::Iterable
97              
98             When subclassing MooseX::Iterator::Meta::Iterable for your own iterators override MooseX::Iterator::Meta::Iterable::_calculate_iterator_class_for_type to
99             returns the name of the class that iterates over your new collection type. The class must implement the MooseX::Iterator::Role role.
100              
101             =back
102              
103             =head1 AUTHOR
104              
105             Robert Boone E<lt>rlb@cpan.orgE<gt>
106              
107             And thank you to Steven Little (steven) and Matt Trout (mst) for the help and advice they gave.
108              
109             =head1 CONTRIBUTORS
110              
111             Johannes Plunien
112              
113             =head1 Code Repository
114              
115             Git - http://github.com/rlb3/moosex-iterator/tree/master
116              
117             =head1 COPYRIGHT AND LICENSE
118              
119             This library is free software; you can redistribute it and/or modify
120             it under the same terms as Perl itself.
121              
122             =cut