Crop/Object/Warehouse.pm untested

filter: Statementstmt 0.0% 0 / 84 Branchbran 0.0% 0 / 22 Conditioncond 0.0% 0 / 16 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 0.0% 0 / 17 Podpod 0.0% 0 / 11 totaltotal 0.0% 0 / 159 CC 12 SCAR 19.3
CC
12
Cov
0%
new
20.0
AUTOLOAD
6.0
all
6.0
CRAP 6.9
1 package Crop::Object::Warehouse;
2 use base qw/ Crop /;
3 # use base qw/ Crop::Object /;
4  
5 =begin nd
6 Class: Crop::Object::Warehouse
7 Warehouse stores objects.
8
9 WH routes requests to appropriate database and returns response to a caller by
10 composing the workers responses.
11
12 Each table has appropriate the worker entry in the main config file.
13  
14 TODO: AUTOLOAD for all the remaining methods; then get understanding of the use of this class.
15 =cut
16  
17 use v5.14;
18 use strict;
19  
20 use Crop::Util qw/ load_class /;
21 use Crop::Error;
22  
23 use Crop::Debug;
24  
25 =begin nd
26 Variable: our $AUTOLOAD
27 Name of the method call.
28 =cut
29 our $AUTOLOAD;
30  
31 =begin nd
32 Variable: my $WH
33 Class Singleton.
34 =cut
35 my $WH;
36  
37 =begin nd
38 Constructor: new ( )
39 Load workers for all the tables.
40  
41 Singleton.
42
43 Defines role for warehouse.
44  
45 Returns:
46 $self - if ok
47 undef - otherwise
48 =cut
49 sub new {
50 my $class = shift;
51
52 return warn "WAREHOUSE|ALERT: Constructor new() must be redefined by subclass $class" unless $class eq __PACKAGE__;
53
54 return $WH if defined $WH;
55
56 my $mode = $class->C->{install}{mode};
57 my $role = $mode eq 'test' ? 'admin' : 'user';
58
59 my %driver;
60 while (my ($db, $conf) = each %{$class->C->{warehouse}{db}}) {
61 my $driver_name = __PACKAGE__ . "::$conf->{driver}";
62 load_class $driver_name or return warn "WAREHOUSE|ALERT: Can not access DB driver $driver_name";
63
64 $driver{$db} = $driver_name->new(
65 host => $conf->{server}{host},
66 port => $conf->{server}{port},
67 name => $conf->{name},
68 login => $conf->{role}{$role}{login},
69 pass => $conf->{role}{$role}{pass},
70 );
71 }
72  
73 my %worker;
74 while (my ($table, $db) = each %{$class->C->{warehouse}{relation}}) {
75 $worker{$table} = $driver{$db};
76 }
77
78 my $self = $WH = bless {
79 worker => \%worker,
80 }, $class;
81 }
82  
83 =begin nd
84 Method: AUTOLOAD ($either, $obj, @data)
85 Mostly, all the methods do the same work.
86  
87 Param:
88 $either - $self or class name of descendant of this package
89 $obj - class name of object in the warehouse
90 @data - remaining arguments
91 =cut
92 sub AUTOLOAD {
93 my ($self, $obj, @data) = @_;
94 my $class = ref $self;
95 my ($method) = $AUTOLOAD =~ /(\w+)\z/;
96 # debug 'CROPOBJECTWAREHOUSE_AUTOLOAD_METHOD=', $method;
97  
98 # debug 'CROPOBJECTWAREHOUSE_AUTOLOAD_PACKAGE=', __PACKAGE__;
99 # debug 'CROPOBJECTWAREHOUSE_AUTOLOAD_CLASS=', $class;
100 return warn "WAREHOUSE|ALERT: method '$method' must be redefined by subclass $class" unless $class eq __PACKAGE__;
101  
102 my $table = $obj->Table;
103 my $worker = $self->_worker($obj) or return warn "WAREHOUSE|ALERT: No worker exist for table '$table'";
104 $worker->$method($obj, @data);
105 }
106  
107 =begin nd
108 Method: all ($obj, @filter)
109 Get collection of objects corresponding to the @filter.
110
111 Param:
112 $obj - class name of objects
113 @filter - clause
114 =cut
115 sub all {
116 my ($self, $obj, @filter) = @_;
117 my $class = ref $self;
118  
119 return warn "WAREHOUSE|ALERT: method all() must be redefined by subclass $class" unless $class eq __PACKAGE__;
120  
121 my $table = $obj->Table;
122 my $worker = $self->_worker($obj) or return warn "WAREHOUSE|ALERT: No worker exist for table '$table'";
123 $worker->all($obj, @filter);
124 }
125  
126 =begin nd
127 Method: create ($obj)
128 Create object of general type.
129
130 Call appropriate Warehouse method.
131
132 Parameters:
133 $obj - object to create
134
135 Returns:
136 object - if ok
137 undef - error
138 =cut
139 sub create {
140 my ($self, $obj) = @_;
141 my $class = ref $self;
142
143 return warn "WAREHOUSE|ALERT: method create() must be redefined by subclass $class" unless $class eq __PACKAGE__;
144  
145 my $worker = $self->_worker($obj) or return warn 'WH|ALERT: Can not create object: table \'' . $obj->Table . '\' do not has worker';
146
147 $self->_worker($obj)->create($obj);
148
149 $obj;
150 }
151  
152 =begin nd
153 Method: create_auto_id ($obj)
154 Create an object of <Crop::Object::Simple> type in the warehouse.
155
156 Parameters:
157 $obj - hash with data
158
159 Returns:
160 id - if ok
161 undef - fail
162 =cut
163 sub create_auto_id {
164 my ($self, $obj) = @_;
165 my $class = ref $self;
166
167 return warn "WAREHOUSE|ALERT: method create_auto_id() must be redefined by subclass $class" unless $class eq __PACKAGE__;
168  
169 my $worker = $self->_worker($obj) or return warn 'WH|ALERT: Can not create object: table \'' . $obj->Table . '\' do not has worker';
170
171 my $id = $self->_worker($obj)->create_auto_id($obj);
172 }
173  
174 =begin nd
175 Method: get_id ($obj)
176 Get id for object.
177
178 Parameters:
179 $obj - object that needs id
180
181 Returns:
182 $obj
183 =cut
184 sub get_id {
185 my ($self, $obj) = @_;
186
187 $self->_worker($obj)->get_id($obj);
188
189 $obj;
190 }
191  
192 =begin nd
193 Method: global_delete ($obj_class, $clause)
194 Delete entire class.
195
196 Performs delete immediately.
197
198 Parameters:
199 $obj_class - class to update
200 $clause - defines exemplars to update
201
202 Returns:
203 true - if ok
204 false - otherwise
205 =cut
206 sub global_delete {
207 my ($either, $obj_class, $clause) = @_;
208 my $class = ref $either || $either;
209
210 return warn "WAREHOUSE|ALERT: method global_delete() must be redefined by subclass $class" unless $class eq __PACKAGE__;
211
212 $either->_worker($obj_class)->global_delete($obj_class, $clause);
213 }
214  
215 =begin nd
216 Method: global_update ($obj_class, \%values, \%clause)
217 Update entire class.
218
219 Performs update immediately.
220
221 Parameters:
222 $obj_class - class to update
223 $values - hash of new values for each attribute
224 $clause - defines exemplars to update
225
226 Returns:
227 true - if ok
228 false - otherwise
229 =cut
230 sub global_update {
231 my ($either, $obj_class, $values, $clause) = @_;
232 my $class = ref $either || $either;
233
234 return warn "WAREHOUSE|ALERT: method global_update() must be redefined by subclass $class" unless $class eq __PACKAGE__;
235
236 $either->_worker($obj_class)->global_update($obj_class, $values, $clause);
237  
238 1;
239 }
240  
241 =begin nd
242 Method: Refresh ($obj)
243 Update exemplar in a warehouse.
244  
245 Parameters:
246 $obj - exemplar to update
247
248 Returns:
249 $obj
250 =cut
251 sub refresh {
252 my ($self, $obj) = @_;
253 my $class = ref $self;
254
255 return warn "WAREHOUSE|ALERT: method refresh() must be redefined by subclass $class" unless $class eq __PACKAGE__;
256
257 $self->_worker($obj)->refresh($obj);
258
259 $obj;
260 }
261  
262 =begin nd
263 Method: remove ($obj)
264 Delete exemplar from warehouse.
265
266 Parameters:
267 $obj - exemplar to delete
268
269 Returns:
270 true - if ok
271 undef - an error
272 =cut
273 sub remove {
274 my ($self, $obj) = @_;
275 my $class = ref $self;
276
277 return warn "WAREHOUSE|ALERT: method remove() must be redefined by subclass $class" unless $class eq __PACKAGE__;
278
279 $self->_worker($obj)->remove($obj);
280
281 1;
282 }
283  
284 =begin nd
285 Method: _worker ($table)
286 Get a worker for the specified $table.
287
288 Parameters:
289 $table - table name
290
291 Returns:
292 driver name as a string
293 =cut
294 sub _worker {
295 my ($self, $obj) = @_;
296
297 $self->{worker}{$obj->Table};
298 }
299  
300 1;