File Coverage

blib/lib/CatalystX/CRUD/Iterator.pm
Criterion Covered Total %
statement 12 37 32.4
branch 0 14 0.0
condition n/a
subroutine 4 8 50.0
pod 4 4 100.0
total 20 63 31.7


line stmt bran cond sub pod time code
1             package CatalystX::CRUD::Iterator;
2 1     1   1170 use strict;
  1         2  
  1         32  
3 1     1   9 use warnings;
  1         2  
  1         25  
4 1     1   5 use Carp;
  1         2  
  1         57  
5 1     1   6 use base qw( CatalystX::CRUD );
  1         6  
  1         504  
6              
7             our $VERSION = '0.58';
8              
9             =head1 NAME
10              
11             CatalystX::CRUD::Iterator - generic iterator wrapper for CXCM iterator() results
12              
13             =head1 SYNOPSIS
14              
15             package MyApp::Model::MyModel;
16             use CatalystX::CRUD::Iterator;
17             use MyModel;
18             __PACKAGE__->config->{object_class} = 'MyModel::Object';
19            
20             sub iterator {
21             my ($self, $query) = @_;
22            
23             my $iterator = MyModel->search_for_something;
24            
25             # $iterator must have a next() method
26            
27             return CatalystX::CRUD::Iterator->new(
28             $iterator,
29             $self->object_class
30             );
31             }
32              
33             =head1 DESCRIPTION
34              
35             CatalystX::CRUD::Iterator is a general iterator class that wraps
36             a real iterator and blesses return results into a specified class.
37             CatalystX::CRUD::Iterator is a glue that provides
38             for a similar level of abstraction across all kinds of CXCM classes.
39              
40             =cut
41              
42             =head1 METHODS
43              
44             =head2 new( I<iterator>, I<class_name> )
45              
46             Returns a CatalystX::CRUD::Iterator instance.
47              
48             I<iterator> must have a next() method and (optionally) a finish() method.
49              
50             See next().
51              
52             =cut
53              
54             # hasa a CXCM iterator() result and calls its next() method,
55             # wrapping the result in the Iterator's CXCO class instance
56              
57             sub new {
58 0     0 1   my $class = shift;
59 0 0         my $iterator = shift or $class->throw_error("need an iterator object");
60 0 0         my $cxco_class = shift
61             or $class->throw_error("need the name of a CXCO class");
62              
63             # sanity checks
64 0 0         unless ( $iterator->can('next') ) {
65 0           $class->throw_error("iterator $iterator has no next() method");
66             }
67              
68 0 0         unless ( $cxco_class->can('new') ) {
69 0           $class->throw_error("no new() method defined for $cxco_class");
70             }
71              
72 0 0         unless ( $cxco_class->isa('CatalystX::CRUD::Object') ) {
73 0           $class->throw_error(
74             "$cxco_class does not inherit from CatalystX::CRUD::Object");
75             }
76              
77 0           return bless(
78             { iterator => $iterator,
79             cxco => $cxco_class
80             },
81             $class
82             );
83             }
84              
85             =head2 next
86              
87             Calls the next() method on the internal I<iterator> object,
88             stashing the result in an object returned by I<class_name>->new
89             under the I<method_name> accessor.
90              
91             =cut
92              
93             sub next {
94 0     0 1   my $self = shift;
95 0           my $next = $self->{iterator}->next;
96 0 0         return unless $next;
97              
98 0           my $obj = $self->{cxco}->new;
99 0           $obj->{delegate} = $next;
100 0           return $obj;
101             }
102              
103             =head2 finish
104              
105             If the internal I<iterator> object has a finish() method,
106             this will call and return it. Otherwise returns true (1).
107              
108             =cut
109              
110             sub finish {
111 0     0 1   my $self = shift;
112 0 0         if ( $self->{iterator}->can('finish') ) {
113 0           return $self->{iterator}->finish;
114             }
115 0           return 1;
116             }
117              
118             =head2 serialize
119              
120             Returns array ref of all objects, having called
121             serialize() on each one. Short-hand for:
122              
123             my $objects = [];
124             while ( my $o = $iterator->next ) {
125             push @$objects, $o->serialize();
126             }
127              
128             =cut
129              
130             sub serialize {
131 0     0 1   my $self = shift;
132 0           my $objects = [];
133 0           while ( my $o = $self->next ) {
134 0           push @$objects, $o->serialize();
135             }
136 0           return $objects;
137             }
138              
139             1;
140              
141             __END__
142              
143             =head1 AUTHOR
144              
145             Peter Karman, C<< <perl at peknet.com> >>
146              
147             =head1 BUGS
148              
149             Please report any bugs or feature requests to
150             C<bug-catalystx-crud at rt.cpan.org>, or through the web interface at
151             L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=CatalystX-CRUD>.
152             I will be notified, and then you'll automatically be notified of progress on
153             your bug as I make changes.
154              
155             =head1 SUPPORT
156              
157             You can find documentation for this module with the perldoc command.
158              
159             perldoc CatalystX::CRUD
160              
161             You can also look for information at:
162              
163             =over 4
164              
165             =item * Mailing List
166              
167             L<https://groups.google.com/forum/#!forum/catalystxcrud>
168              
169             =item * AnnoCPAN: Annotated CPAN documentation
170              
171             L<http://annocpan.org/dist/CatalystX-CRUD>
172              
173             =item * CPAN Ratings
174              
175             L<http://cpanratings.perl.org/d/CatalystX-CRUD>
176              
177             =item * RT: CPAN's request tracker
178              
179             L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=CatalystX-CRUD>
180              
181             =item * Search CPAN
182              
183             L<http://search.cpan.org/dist/CatalystX-CRUD>
184              
185             =back
186              
187             =head1 ACKNOWLEDGEMENTS
188              
189             =head1 COPYRIGHT & LICENSE
190              
191             Copyright 2007 Peter Karman, all rights reserved.
192              
193             This program is free software; you can redistribute it and/or modify it
194             under the same terms as Perl itself.
195              
196             =cut