File Coverage

lib/CatalystX/CRUD/ModelAdapter.pm
Criterion Covered Total %
statement 18 36 50.0
branch n/a
condition n/a
subroutine 6 24 25.0
pod 18 18 100.0
total 42 78 53.8


line stmt bran cond sub pod time code
1             package CatalystX::CRUD::ModelAdapter;
2 4     4   38 use strict;
  4         10  
  4         174  
3 4     4   52 use warnings;
  4         60  
  4         135  
4 4         725 use base qw(
5             CatalystX::CRUD
6             Class::Accessor::Fast
7 4     4   35 );
  4         9  
8 4     4   31 use MRO::Compat;
  4         17  
  4         130  
9 4     4   49 use mro 'c3';
  4         18  
  4         36  
10 4     4   212 use Carp;
  4         29  
  4         1973  
11              
12             __PACKAGE__->mk_accessors(qw( model_name model_meta context app_class ));
13              
14             =head1 NAME
15              
16             CatalystX::CRUD::ModelAdapter - make CRUD Controllers work with non-CRUD models
17              
18             =head1 SYNOPSIS
19              
20             package My::ModelAdapter::Foo;
21             use base qw( CatalystX::CRUD::ModelAdapter );
22            
23             # must implement the following methods
24             sub new_object { }
25             sub fetch { }
26             sub search { }
27             sub iterator { }
28             sub count { }
29             sub make_query { }
30             sub search_related { }
31             sub iterator_related { }
32             sub count_related { }
33            
34             1;
35            
36             # then in your CX::CRUD::Controller subclass
37             package MyApp::Controller::CRUD;
38             use base qw( CatalystX::CRUD::Controller );
39            
40             __PACKAGE__->config(
41             'model_adapter' => 'My::ModelAdapter::Foo'
42             );
43            
44             1;
45            
46             =head1 DESCRIPTION
47              
48             CatalystX::CRUD::ModelAdapter allows you to use existing, non-CRUD Models with
49             the CatalystX::CRUD::Controller API. The ModelAdapter class implements a similar
50             API to the CX::CRUD::Model, but does not inherit from Catalyst::Model and should
51             not sit in the ::Model namespace.
52              
53             If a 'model_adapter' config value is present
54             in a CX::CRUD::Controller subclass, the ModelAdapter instance will be called
55             instead of the 'model_name' instance. The B<model_name> accessor is available
56             on the ModelAdapter instance and is set automatically at instantiation time
57             by the calling Controller.
58              
59             This documentation is intended for ModelAdapter developers.
60              
61             =head1 CONFIGURATION
62              
63             You may configure your CXCM-derived Models in the usual way (see the Catalyst
64             Manual).
65              
66             =head1 METHODS
67              
68             CatalystX::CRUD::ModelAdapter inherits from CatalystX::CRUD.
69              
70             The following methods should be implemented in your subclass.
71              
72             =head2 new_object( I<controller>, I<context> )
73              
74             Should return a new instance from the Model you are adapting.
75              
76             =cut
77              
78 0     0 1   sub new_object { shift->throw_error("must implement new_object"); }
79              
80             =head2 fetch( I<controller>, I<context>, I<args> )
81              
82             Should return an instance of the Model you are adapting, based
83             on I<args>.
84              
85             =cut
86              
87 0     0 1   sub fetch { shift->throw_error("must implement fetch") }
88              
89             =head2 search( I<controller>, I<context>, I<args> )
90              
91             Should return an arrayref of instances of the Model you are adapting,
92             based on I<args>.
93              
94             =cut
95              
96 0     0 1   sub search { shift->throw_error("must implement search") }
97              
98             =head2 iterator( I<controller>, I<context>, I<args> )
99              
100             Should return an iterator of instances of the Model you are adapting,
101             based on I<args>.
102              
103             =cut
104              
105 0     0 1   sub iterator { shift->throw_error("must implement iterator") }
106              
107             =head2 count( I<controller>, I<context>, I<args> )
108              
109             Should return an integer representing the numbef of matching instances
110             of the Model you are adapting, based on I<args>.
111              
112             =cut
113              
114 0     0 1   sub count { shift->throw_error("must implement count") }
115              
116             =head2 make_query( I<controller>, I<context> )
117              
118             Should return appropriate values for passing to search(), iterator() and
119             count(). See CataystX::CRUD::Model for examples.
120              
121             =cut
122              
123 0     0 1   sub make_query { shift->throw_error("must implement make_query()") }
124              
125             =head2 search_related( I<controller>, I<context>, I<obj>, I<relationship> )
126              
127             Returns zero or more CXCO instances like search().
128             The instances are related to I<obj> via I<relationship>.
129              
130             =head2 iterator_related( I<controller>, I<context>, I<obj>, I<relationship> )
131              
132             Like search_related() but returns an iterator.
133              
134             =head2 count_related( I<controller>, I<context>, I<obj>, I<relationship> )
135              
136             Like search_related() but returns an integer.
137              
138             =cut
139              
140 0     0 1   sub search_related { shift->throw_error("must implement search_related") }
141 0     0 1   sub iterator_related { shift->throw_error("must implement iterator_related") }
142 0     0 1   sub count_related { shift->throw_error("must implement count_related") }
143              
144             =head2 add_related( I<controller>, I<context>, I<obj>, I<rel_name>, I<foreign_value> )
145              
146             Associate foreign object identified by I<foreign_value> with I<obj>
147             via the relationship I<rel_name>.
148              
149             It is up to the subclass to implement this method.
150              
151             =head2 rm_related( I<controller>, I<context>, I<obj>, I<rel_name>, I<foreign_value> )
152              
153             Dissociate foreign object identified by I<foreign_value> from I<obj>
154             via the relationship I<rel_name>.
155              
156             It is up to the subclass to implement this method.
157              
158             =head2 put_related( I<obj>, I<rel_name>, I<foreign_value> )
159              
160             Create new related foreign object. Unlike add_related(),
161             the foreign object need not already exist. put_related()
162             should be idempotent.
163              
164             =head2 find_related( I<obj>, I<rel_name>, I<foreign_value> )
165              
166             Return related object for I<foreign_value> based on I<rel_name>
167             for I<obj>.
168              
169             =head2 has_relationship( I<controller>, I<context>, I<obj>, I<rel_name> )
170              
171             Should return true or false as to whether I<rel_name> exists for
172             I<obj>.
173              
174             It is up to the subclass to implement this method.
175              
176              
177             =cut
178              
179 0     0 1   sub add_related { shift->throw_error("must implement add_related()") }
180 0     0 1   sub rm_related { shift->throw_error("must implement rm_related()") }
181 0     0 1   sub put_related { shift->throw_error("must implement put_related()") }
182 0     0 1   sub find_related { shift->throw_error("must implement find_related()") }
183              
184             sub has_relationship {
185 0     0 1   shift->throw_error("must implement has_relationship()");
186             }
187              
188             =head1 CRUD Methods
189              
190             The following methods are implemented in CatalystX::CRUD::Object when
191             using the CatalystX::CRUD::Model API. When using the ModelAdapter API, they
192             should be implemented in the adapter class.
193              
194             =head2 create( I<context>, I<object> )
195              
196             Should implement the C in CRUD.
197              
198             =cut
199              
200 0     0 1   sub create { shift->throw_error("must implement create()") }
201              
202             =head2 read( I<context>, I<object> )
203              
204             Should implement the R in CRUD.
205              
206             =cut
207              
208 0     0 1   sub read { shift->throw_error("must implement read()") }
209              
210             =head2 update( I<context>, I<object> )
211              
212             Should implement the U in CRUD.
213              
214             =cut
215              
216 0     0 1   sub update { shift->throw_error("must implement update()") }
217              
218             =head2 delete( I<context>, I<object> )
219              
220             Should implement the D in CRUD.
221              
222             =cut
223              
224 0     0 1   sub delete { shift->throw_error("must implement delete()") }
225              
226             1;
227              
228             __END__
229              
230             =head1 AUTHOR
231              
232             Peter Karman, C<< <perl at peknet.com> >>
233              
234             =head1 BUGS
235              
236             Please report any bugs or feature requests to
237             C<bug-catalystx-crud at rt.cpan.org>, or through the web interface at
238             L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=CatalystX-CRUD>.
239             I will be notified, and then you'll automatically be notified of progress on
240             your bug as I make changes.
241              
242             =head1 SUPPORT
243              
244             You can find documentation for this module with the perldoc command.
245              
246             perldoc CatalystX::CRUD
247              
248             You can also look for information at:
249              
250             =over 4
251              
252             =item * Mailing List
253              
254             L<https://groups.google.com/forum/#!forum/catalystxcrud>
255              
256             =item * AnnoCPAN: Annotated CPAN documentation
257              
258             L<http://annocpan.org/dist/CatalystX-CRUD>
259              
260             =item * CPAN Ratings
261              
262             L<http://cpanratings.perl.org/d/CatalystX-CRUD>
263              
264             =item * RT: CPAN's request tracker
265              
266             L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=CatalystX-CRUD>
267              
268             =item * Search CPAN
269              
270             L<http://search.cpan.org/dist/CatalystX-CRUD>
271              
272             =back
273              
274             =head1 COPYRIGHT & LICENSE
275              
276             Copyright 2008 Peter Karman, all rights reserved.
277              
278             This program is free software; you can redistribute it and/or modify it
279             under the same terms as Perl itself.
280              
281             =cut