blib/lib/WWW/Shopify/Model/Item.pm

filter: Statementstmt 24.0% 45 / 187 Branchbran 5.0% 4 / 80 Conditioncond 37.5% 9 / 24 MC/DCmcdc 35.7% 5 / 14 Subroutinesub 16.2% 13 / 80 Podpod 57.1% 40 / 70 totaltotal 25.4% 116 / 455 CC 50 SCAR 43.0
CC
50
Cov
20%
to_json
380.0
decodeForRef
156.0
metafields
20.0
CRAP 73.8
« blib/lib/WWW/Shopify/Test.pm ↑ Summary blib/lib/WWW/Shopify/Common/DBIxGroup.pm »
1 #!/usr/bin/perl
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 package WWW::Shopify::Model::Item;
7 1 ▶use DateTime;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use WWW::Shopify::Query;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 =head1 NAME
11  
12 Item - Main superclass for all the shopify objects.
13  
14 =cut
15  
16 =head1 DESCRIPTION
17  
18 Items are the main super class for all Shopify objects. This class holds glue code that ties the schemas (WWW::Shopify::Model::Product, for example), which only hold definitions about how the Shopify API works, and WWW::Shopify, which is the acutal method for calling Shopify's interface.
19  
20 These objects can be used in many ways; they boil down to one of three states.
21  
22 =over 4
23  
24 =item DBIx::Class Object: The item has been mapped to a DBIx::Class schema and can be stored in a relational database.
25  
26 =item WWW::Shopify::Model Object: The item is in an intemediate state, suitable for querying, perusing and manipulating.
27  
28 =item JSON-Like Hash: The item is similar to the WWW::Shopify::Model, but lacks all-non canonical shopify references, and is ready to be encode_json'd before being sent off to Shopify.
29  
30 =back
31  
32 =cut
33  
34 =head1 METHODS
35  
36 Most of these methods you shouldn't have to mess with, most of them are used to describe the objects appropriately so that testing mock frameworks and database can be created with ease.
37  
38 =cut
39  
40 1 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
41 1 our @EXPORT = qw(generate_accessor);
42  
43 =head2 new($%params)
44  
45 Generates a new intermediate stage (WWW::Shopify::Model::...) object.
46  
47 =cut
48  
49 1 ▶use Scalar::Util qw(weaken);
Subroutine BEGIN: called CC=1 SCAR=0.0
50  
51 sub new {
52 0 ▶ my $self = (defined $_[1]) ? $_[1] : {};
Subroutine new: not called CC=3 SCAR=24.8
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
defined $_[1] ? : 0 0 0
53 0 ▶ weaken($self->{associated_parent}) if $self->{associated_parent};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"associated_parent"} 0 0 0
54 0 return bless $self, $_[0];
55 }
56  
57 =head2 parent($package)
58  
59 Does this package have a parent?
60  
61 =cut
62  
63 0 ▶sub parent { return undef; }
Subroutine parent: not called CC=1 SCAR=6.9
Pod documented
64 0 ▶sub is_item { return 1; }
Subroutine is_item: not called CC=1 SCAR=6.9
Pod undocumented
65  
66 =head2 is_shop($package)
67  
68 Convenience method telling us if we're a WWW::Shopify::Model::Shop.
69  
70 =cut
71  
72 0 ▶sub is_shop { return undef; }
Subroutine is_shop: not called CC=1 SCAR=6.9
Pod documented
73  
74 =head2 singular($package)
75  
76 Returns the text representing the singular form of this package. (WWW::Shopify::Model::Order->singular eq "order").
77  
78 =cut
79  
80 # Returns the singular form of the object. Usually the name of the file.
81 # Is like this, because we want to be able to call this on the package as well as an object in it.
82 sub singular {
83 # If we have a package instead of an instantiated object.
84 0 ▶ if (!ref($_[0])) {
Subroutine singular: not called CC=3 SCAR=24.8
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $_[0]) 0 0 0
85 0 ▶ die $_[0] unless $_[0] =~ m/(\:\:)?(\w+)$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $_[0] =~ /(\:\:)?(\w+)$/ 0 0 0
86 0 my $name = $2;
87 0 $name =~ s/([a-z])([A-Z])/$1_$2/g;
88 0 return (lc $name);
89 }
90 # Here, we have an object, so run this function again with the package name.
91 0 return singular(ref($_[0]));
92 }
93  
94 # Specifies in a text-friendly manner the FULL specific name of this package.
95 sub singular_fully_qualified {
96 0 ▶ if (!ref($_[0])) {
Subroutine singular_fully_qualified: not called CC=3 SCAR=24.8
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $_[0]) 0 0 0
97 0 ▶ die $_[0] unless $_[0] =~ m/Model::([:\w]+)$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $_[0] =~ /Model::([:\w]+)$/ 0 0 0
98 0 my $name = lc($1);
99 0 $name =~ s/\:\:/-/g;
100 0 return $name;
101 }
102 # Here, we have an object, so run this function again with the package name.
103 0 return singular_fully_qualified(ref($_[0]));
104 }
105  
106 =head2 plural($package)
107  
108 Returns the text representing the plural form of this package. (WWW::Shopify::Model::Address->plural eq "addresses").
109  
110 =cut
111  
112 0 ▶sub plural { return $_[0]->singular() . "s"; }
Subroutine plural: not called CC=1 SCAR=6.9
Pod documented
113  
114  
115 0 ▶sub needs_login { return undef; }
Subroutine needs_login: not called CC=1 SCAR=6.9
Pod undocumented
116  
117 =head2 needs_plus($package)
118  
119 Returns whether or not a Shopify+ account is required to use this resource.
120  
121 =cut
122  
123 0 ▶sub needs_plus { return undef; }
Subroutine needs_plus: not called CC=1 SCAR=6.9
Pod documented
124  
125 # List of fields that should be filled automatically on creation.
126 0 ▶sub is_nested { return undef; }
Subroutine is_nested: not called CC=1 SCAR=6.9
Pod undocumented
127 0 ▶sub identifier { return ("id"); }
Subroutine identifier: not called CC=1 SCAR=6.9
Pod undocumented
128  
129 # Counts the ::. Is helpful in a bunch of ways.
130 sub nested_level {
131 0 ▶ my ($package) = @_;
Subroutine nested_level: not called CC=2 SCAR=17.9
Pod undocumented
132 0 my $level = -2;
133 0 ▶ while ($package =~ m/::/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($package =~ /::/g) 0 0 0
134 0 ++$level;
135 }
136 0 return $level;
137 }
138  
139  
140 # I cannot fucking believe I'm doing this. Everything can be counted.
141 # Oh, except themes. We don't count those. For some arbitrary reason.
142  
143 =head2 countable($package), gettable($package), creatable($package), updatable($package), deletable($package), searchable($package)
144  
145 Determines whether or not this package can perform these main actions. 1 for yes, undef for no.
146  
147 =cut
148  
149 0 ▶sub countable { return 1; }
Subroutine countable: not called CC=1 SCAR=6.9
Pod documented
150 0 ▶sub gettable { return 1; }
Subroutine gettable: not called CC=1 SCAR=6.9
Pod documented
151 0 ▶sub singlable { return $_[0]->gettable; }
Subroutine singlable: not called CC=1 SCAR=6.9
Pod undocumented
152 0 ▶sub creatable { return 1; }
Subroutine creatable: not called CC=1 SCAR=6.9
Pod documented
153 0 ▶sub updatable { my @fields = $_[0]->update_fields; return int(@fields) > 0; }
Subroutine updatable: not called CC=1 SCAR=6.9
Pod documented
154 0 ▶sub deletable { return 1; }
Subroutine deletable: not called CC=1 SCAR=6.9
Pod documented
155 0 ▶sub searchable { return undef; }
Subroutine searchable: not called CC=1 SCAR=6.9
Pod documented
156  
157 =head2 actions($package)
158  
159 Returns an array of special actions that this package can perform (for orders, they can open, close, and cancel).
160  
161 =cut
162  
163 58 ▶sub actions { return qw(); }
Subroutine actions: called CC=1 SCAR=0.0
Pod documented
164 1 ▶use List::Util qw(first);
Subroutine BEGIN: called CC=1 SCAR=0.0
165 0 ▶sub can_action { return defined first { $_ eq $_[1] } $_[0]->actions; }
Subroutine __ANON__: not called CC=1 SCAR=6.9
Subroutine can_action: not called CC=1 SCAR=6.9
Pod undocumented
166 0 ▶sub can_open { return $_[0]->can_action("open"); }
Subroutine can_open: not called CC=1 SCAR=6.9
Pod undocumented
167 0 ▶sub can_activate { return $_[0]->can_action("activate"); }
Subroutine can_activate: not called CC=1 SCAR=6.9
Pod undocumented
168 0 ▶sub can_deactivate { return $_[0]->can_action("deactivate"); }
Subroutine can_deactivate: not called CC=1 SCAR=6.9
Pod undocumented
169 0 ▶sub can_enable { return $_[0]->can_action("enable"); }
Subroutine can_enable: not called CC=1 SCAR=6.9
Pod undocumented
170 0 ▶sub can_disable { return $_[0]->can_action("disable"); }
Subroutine can_disable: not called CC=1 SCAR=6.9
Pod undocumented
171 0 ▶sub can_close { return $_[0]->can_action("close"); }
Subroutine can_close: not called CC=1 SCAR=6.9
Pod undocumented
172 0 ▶sub can_cancel { return $_[0]->can_action("cancel"); }
Subroutine can_cancel: not called CC=1 SCAR=6.9
Pod undocumented
173  
174 =head2 create_method($package), update_method($package), delete_method($package)
175  
176 Shopify occasionally arbitrarily changes their update/create methods from POST and PUT around. Sometimes it makes them the same. These say which method is used where, but generally the convention is:
177  
178 =over 4
179  
180 =item CREATE => POST
181  
182 =item UPDATE => PUT
183  
184 =item DELETE => DELETE
185  
186 =back
187  
188 But not always.
189  
190 =cut
191  
192 # I CANNOT FUCKING BELIEVE I AM DOING THIS; WHAT THE FUCK SHOPIFY. WHY!? WHY MAKE IT DIFFERENT ARBITRARILY!?
193 0 ▶sub create_method { return "POST"; }
Subroutine create_method: not called CC=1 SCAR=6.9
Pod documented
194 0 ▶sub update_method { return "PUT"; }
Subroutine update_method: not called CC=1 SCAR=6.9
Pod documented
195 0 ▶sub delete_method { return "DELETE"; }
Subroutine delete_method: not called CC=1 SCAR=6.9
Pod documented
196  
197 =head2 queries($package)
198  
199 Returns a hash of WWW::Shopify::Query objects which describe you what kind of filters you can run on this package. (Limit, since_id, created_at_min, etc...)
200  
201 =cut
202  
203 52 ▶sub queries { return {}; }
Subroutine queries: called CC=1 SCAR=0.0
Pod documented
204  
205 =head2 unique_fields($package)
206  
207 An array of fields which have to be unique in this package (WWW::Shopify::Model::Customer's email, for example)
208  
209 =cut
210  
211 0 ▶sub unique_fields { return qw(); }
Subroutine unique_fields: not called CC=1 SCAR=6.9
Pod documented
212  
213 =head2 get_fields($package)
214  
215 Usually every field in a class, but not always; this returns an array of keys that tells you which fields will come back when you perform a normal get request.
216  
217 =cut
218  
219 0 ▶sub get_fields { return keys(%{$_[0]->fields}); }
Subroutine get_fields: not called CC=1 SCAR=6.9
Pod documented
220  
221 =head2 creation_minimal($package)
222  
223 Returns an array of fields that are required before you send an object off to be created on Shopify.
224  
225 =cut
226  
227 0 ▶sub creation_minimal { return (); }
Subroutine creation_minimal: not called CC=1 SCAR=6.9
Pod documented
228  
229 =head2 creation_filled($package)
230  
231 Returns an array of fields that you should be getting back filled with data when the object is created on Shopify.
232  
233 =cut
234  
235 0 ▶sub creation_filled { return qw(id); }
Subroutine creation_filled: not called CC=1 SCAR=6.9
Pod documented
236  
237 =head2 update_fields($package)
238  
239 Returns an array of fields that you can update when an object is already created.
240  
241 =cut
242  
243 0 ▶sub update_fields { return qw(); }
Subroutine update_fields: not called CC=1 SCAR=6.9
Pod documented
244  
245 =head2 update_filled($package)
246  
247 Returns an array of fields that will be changed/filled when an object is updated.
248  
249 =cut
250  
251 0 ▶sub update_filled { return qw(); }
Subroutine update_filled: not called CC=1 SCAR=6.9
Pod documented
252  
253  
254 =head2 throws_webhooks($package), throws_create_webhooks($package), throws_update_webhooks($package), throws_delete_webhooks($package)
255  
256 Tells you whether or not the object throws webhooks for a living.
257  
258 =cut
259  
260 0 ▶sub throws_webhooks { return undef; }
Subroutine throws_webhooks: not called CC=1 SCAR=6.9
Pod documented
261 0 ▶sub throws_create_webhooks { return $_[0]->throws_webhooks; }
Subroutine throws_create_webhooks: not called CC=1 SCAR=6.9
Pod documented
262 0 ▶sub throws_update_webhooks { return $_[0]->throws_webhooks; }
Subroutine throws_update_webhooks: not called CC=1 SCAR=6.9
Pod documented
263 0 ▶sub throws_delete_webhooks { return $_[0]->throws_webhooks; }
Subroutine throws_delete_webhooks: not called CC=1 SCAR=6.9
Pod documented
264  
265 # Oh fucking WOW. WHAT THE FUCK. Variants, of course, delete directly with their id, and modify with it.
266 # Metafields delete with their id, but modify through their parent. They also get through their parents.
267 # Variants of course, get through their id directly. I'm at a loss for words. Why!? Article are different, yet again.
268  
269 =head2 get_all_through_parent($package), get_through_parent($package), create_through_parent($package), update_through_parent($package), delete_through_parent($package)
270  
271 Occasionally, Shopify decides they wanna change up their normal API conventions, and decides to throw us a curve ball, and change up whether or not certain objects have to be accessed through their parent objects /product/3242134/variants/342342.json vs. /variants/342342.json
272  
273 These tell you which objects have to go through their parent, and which don't. It tends to be rather arbitrary.
274  
275 =cut
276  
277 0 ▶sub get_all_through_parent { return defined $_[0]->parent; }
Subroutine get_all_through_parent: not called CC=1 SCAR=6.9
Pod documented
278 0 ▶sub get_through_parent { return defined $_[0]->parent; }
Subroutine get_through_parent: not called CC=1 SCAR=6.9
Pod documented
279 0 ▶sub count_through_parent { return $_[0]->get_all_through_parent; }
Subroutine count_through_parent: not called CC=1 SCAR=6.9
Pod undocumented
280 0 ▶sub create_through_parent { return defined $_[0]->parent; }
Subroutine create_through_parent: not called CC=1 SCAR=6.9
Pod documented
281 0 ▶sub update_through_parent { return defined $_[0]->parent; }
Subroutine update_through_parent: not called CC=1 SCAR=6.9
Pod documented
282 0 ▶sub delete_through_parent { return defined $_[0]->parent; }
Subroutine delete_through_parent: not called CC=1 SCAR=6.9
Pod documented
283 0 ▶sub activate_through_parent { return defined $_[0]->parent; }
Subroutine activate_through_parent: not called CC=1 SCAR=6.9
Pod undocumented
284 0 ▶sub deactivate_through_parent { return defined $_[0]->parent; }
Subroutine deactivate_through_parent: not called CC=1 SCAR=6.9
Pod undocumented
285 0 ▶sub open_through_parent { return defined $_[0]->parent; }
Subroutine open_through_parent: not called CC=1 SCAR=6.9
Pod undocumented
286 0 ▶sub close_through_parent { return defined $_[0]->parent; }
Subroutine close_through_parent: not called CC=1 SCAR=6.9
Pod undocumented
287 0 ▶sub cancel_through_parent { return defined $_[0]->parent; }
Subroutine cancel_through_parent: not called CC=1 SCAR=6.9
Pod undocumented
288 0 ▶sub enable_through_parent { return defined $_[0]->parent; }
Subroutine enable_through_parent: not called CC=1 SCAR=6.9
Pod undocumented
289 0 ▶sub disable_through_parent { return defined $_[0]->parent; }
Subroutine disable_through_parent: not called CC=1 SCAR=6.9
Pod undocumented
290  
291 =head2 included_in_parent
292  
293 Tells you whether a sub-object is included by defualt when you get an object as a sub-object. Most cases is 1.
294  
295 =cut
296  
297 0 ▶sub included_in_parent { return 1; }
Subroutine included_in_parent: not called CC=1 SCAR=6.9
Pod documented
298  
299 =head2 field($package, $name), fields($package)
300  
301 Returns a WWW::Shopify::Field::... which describes the specified field, or returns a hash containing all fields.
302  
303 =cut
304  
305 ▶sub fields { }
Subroutine fields: not called CC=1 SCAR=0.0
Pod documented
306 1492 ▶sub field { my ($package, $name) = @_; return $package->fields->{$name}; }
Subroutine field: called CC=1 SCAR=0.0
Pod documented
307  
308 =head2 associate($self)
309  
310 Returns the WWW::Shopify object that created/updated/deleted this object last.
311  
312 =cut
313  
314 0 ▶sub associate { $_[0]->{associated_sa} = $_[1] if $_[1]; return $_[0]->{associated_sa}; }
Subroutine associate: not called CC=2 SCAR=17.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
if $_[1] 0 0 0
315  
316 =head2 associate_parent($self)
317  
318 Returns the parent associated with this object. If we're a variant, it'll return the parent product (if it had access to it at one point; it usually does). Weak reference.
319  
320 =cut
321  
322  
323 1 ▶use Scalar::Util qw(weaken);
Subroutine BEGIN: called CC=1 SCAR=0.0
324 sub associated_parent {
325 0 ▶ if (defined $_[1]) {
Subroutine associated_parent: not called CC=2 SCAR=17.9
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_[1]) 0 0 0
326 0 $_[0]->{associated_parent} = $_[1];
327 0 weaken($_[0]->{associated_parent});
328 }
329 0 return $_[0]->{associated_parent};
330 }
331  
332 0 ▶sub has_metafields { return defined $_[0]->field('metafields'); }
Subroutine has_metafields: not called CC=1 SCAR=6.9
Pod undocumented
333  
334 =head2 metafields($self)
335  
336 Returns the metafields associated with this item. If they haven't been gotten before, a request is made, and they're cahced inside the object.
337  
338 If you change a metafield, and later want to access it through a copy of the object, and this copy already has looked at the metafields, you'll get a stale copy.
339  
340 If this is the case, call the method below.
341  
342 =cut
343  
344 1 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
345 sub metafields {
346 0 ▶ my $sa = $_[0]->associate;
Subroutine metafields: not called CC=4 SCAR=30.0
Pod documented
347 0 ▶ if (!defined $_[0]->{metafields}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $_[0]{"metafields"}) 0 0 0
348 0 ▶ die new WWW::Shopify::Exception("You cannot call metafields on an unassociated item.") unless $sa;
Branch: 1 branch
BranchTrueFalseTotal
unless $sa 0 0 0
349 0 $_[0]->{metafields} = [$sa->get_all('Metafield', { parent => $_[0] })];
350 }
351 0 ▶ return $_[0]->{metafields} unless wantarray;
Branch: 1 branch
BranchTrueFalseTotal
unless wantarray 0 0 0
352 0 return @{$_[0]->{metafields}};
353 }
354  
355 =head2 refesh_metafields($self)
356  
357 Returns the metafields associated with item, but ALWAYS performs a get request.
358  
359 =cut
360  
361 sub refresh_metafields {
362 0 ▶ delete $_[0]->{metafields} if exists $_[0]->{metafields};
Subroutine refresh_metafields: not called CC=2 SCAR=17.9
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
if exists $_[0]{"metafields"} 0 0 0
363 0 return $_[0]->metafields;
364 }
365  
366 =head2 add_metafield($self, $metafield)
367  
368 Takes a WWW::Shopify::Model::Metafield object.
369  
370 =cut
371  
372 sub add_metafield {
373 0 ▶ my ($self, $metafield) = @_;
Subroutine add_metafield: not called CC=2 SCAR=17.9
Pod documented
374 0 my $sa = $_[0]->associate;
375 0 ▶ die new WWW::Shopify::Exception("You cannot add metafields on an unassociated item.") unless $sa;
Branch: 1 branch
BranchTrueFalseTotal
unless $sa 0 0 0
376 0 $metafield->associated_parent($self);
377 0 return $sa->create($metafield);
378 }
379  
380 =head2 from_json($package, $json_hash, [$associated])
381  
382 Returns a WWW::Shopify::Model::... intermediate object from a hash that's been decoded from a JSON object (i.e. normal shopify object in the API docs, decoded using JSON qw(decode_json); ). Does a bunch of nice conversions, like putting DateTime objects in the proper places, and associate each individual obejct with a reference to its parent and to the WWW::Shopify object that created it.
383  
384 =cut
385  
386 sub from_json {
387 0 ▶ my ($package, $json, $associated) = @_;
Subroutine from_json: not called CC=3 SCAR=24.8
Pod documented
388  
389 sub decodeForRef {
390 0 ▶ my ($self, $json, $ref, $associated) = @_;
Subroutine decodeForRef: not called CC=12 SCAR=50.5
Pod undocumented
391 0 for (keys(%{$ref})) {
392 0 ▶ if ($ref->{$_}->is_relation()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ref->{$_}->is_relation) { } 0 0 0
393 0 my $package = $ref->{$_}->relation();
394 0 ▶ if ($ref->{$_}->is_many()) {
Branch: 3 branches
BranchTrueFalseTotal
if ($ref->{$_}->is_many) { } 0 0 0
elsif ($ref->{$_}->is_one and $ref->{$_}->is_own) { } 0 0 0
elsif ($ref->{$_}->is_one) { } 0 0 0
395 0 ▶ next unless exists $json->{$package->plural()};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $$json{$package->plural} 0 0 0
396 $self->{$_} = [map {
397 0 my $o = $package->from_json($_, $associated);
398 0 $o->associated_parent($self);
399 0 $o;
400 0 } @{$json->{$package->plural()}}];
401 }
402 ▶ elsif ($ref->{$_}->is_one && $ref->{$_}->is_own) {
Condition0%0/3
$ref->{$_}->is_one and $ref->{$_}->is_own
!ll&&!rl&&r
000
MC/DC0%0/2
$ref->{$_}->is_one and $ref->{$_}->is_own
$ref->{$_}->is_one $ref->{$_}->is_own
Truth table0%0/3
A and B
A= $ref->{$_}->is_oneB= $ref->{$_}->is_own
ABresult
0X0
100
111
403 0 ▶ next unless (exists $json->{$_});
Branch: 1 branch
BranchTrueFalseTotal
unless exists $json->{$_} 0 0 0
404 0 $self->{$_} = $package->from_json($json->{$_}, $associated);
405 0 ▶ $self->{$_}->associated_parent($self) if $self->{$_};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{$_} 0 0 0
406 }
407 elsif ($ref->{$_}->is_one) {
408 0 ▶ $self->{$_} = $json->{$_} if exists $json->{$_};
Branch: 1 branch
BranchTrueFalseTotal
if exists $json->{$_} 0 0 0
409 }
410 else {
411 0 die "Relationship specified must be either many, or one in $package.";
412 }
413 }
414 else {
415 0 ▶ $self->{$_} = $ref->{$_}->from_shopify($json->{$_}) if exists $json->{$_};
Branch: 1 branch
BranchTrueFalseTotal
if exists $json->{$_} 0 0 0
416 }
417 }
418 }
419 0 ▶ return undef unless $json;
Branch: 1 branch
BranchTrueFalseTotal
unless $json 0 0 0
420  
421 0 my $self = $package->new();
422 0 ▶ $self->associate($associated) if ($associated);
Branch: 1 branch
BranchTrueFalseTotal
if $associated 0 0 0
423  
424 0 $self->decodeForRef($json, $self->fields, $associated);
425 0 return $self;
426 }
427  
428 =head2 to_json($self)
429  
430 Returns a hash that's ready to be converted in to a JSON string using encode_json from the JSON package.
431  
432 =cut
433  
434 sub to_json($) {
435 0 ▶ my ($self) = @_;
Subroutine to_json: not called CC=19 SCAR=59.4
Pod documented
436 0 my $fields = $self->fields();
437 0 my $final = {};
438 0 foreach my $key (keys(%$self)) {
439 0 ▶ next unless exists $fields->{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $fields->{$key} 0 0 0
440 0 ▶ if ($fields->{$key}->is_relation()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fields->{$key}->is_relation) { } 0 0 0
441 0 ▶ if ($fields->{$key}->is_many()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fields->{$key}->is_many) 0 0 0
442 # Since metafields don't come prepackaged, we don't get them. Unless we've already got them.
443 0 ▶ next if $key eq "metafields" && !$_[0]->{metafields};
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "metafields" and not $_[0]{"metafields"} 0 0 0
Condition0%0/3
$key eq "metafields" and not $_[0]{"metafields"}
!ll&&!rl&&r
000
MC/DC0%0/2
$key eq "metafields" and not $_[0]{"metafields"}
$key eq "metafields" not $_[0]{"metafields"}
Truth table0%0/3
A and not(B)
A= $key eq "metafields"B= not $_[0]{"metafields"}
ABresult
0X0
100
111
444 0 my @results = $self->$key();
445 0 ▶ if (int(@results)) {
Branch: 1 branch
BranchTrueFalseTotal
if (int @results) { } 0 0 0
446 0 $final->{$key} = [map { $_->to_json() } @results];
447 }
448 else {
449 0 ▶ $final->{$key} = [] if exists $self->{$key};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{$key} 0 0 0
450 }
451 }
452 0 ▶ if ($fields->{$key}->is_one() && $fields->{$key}->is_reference()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fields->{$key}->is_one and $fields->{$key}->is_reference) 0 0 0
Condition0%0/3
$fields->{$key}->is_one and $fields->{$key}->is_reference
!ll&&!rl&&r
000
MC/DC0%0/2
$fields->{$key}->is_one and $fields->{$key}->is_reference
$fields->{$key}->is_one $fields->{$key}->is_reference
Truth table0%0/3
A and B
A= $fields->{$key}->is_oneB= $fields->{$key}->is_reference
ABresult
0X0
100
111
453 0 ▶ if (defined $self->$key()) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->$key) { } 0 0 0
454 # This is inconsistent; this if is a stop-gap measure.
455 # Getting directly from teh database seems to make this automatically an id.
456 0 ▶ if (ref($self->$key())) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self->$key) { } 0 0 0
457 0 $final->{$key} = $self->$key()->id();
458 }
459 else {
460 0 $final->{$key} = $self->$key();
461 }
462 }
463 else {
464 0 ▶ $final->{$key} = undef if exists $self->{$key};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{$key} 0 0 0
465 }
466 }
467 0 ▶ $final->{$key} = ($self->$key ? $self->$key->to_json() : undef) if (exists $self->{$key} && $fields->{$key}->is_one() && $fields->{$key}->is_own());
Branch: 2 branches
BranchTrueFalseTotal
if exists $self->{$key} and $fields->{$key}->is_one and $fields->{$key}->is_own 0 0 0
$self->$key ? : 0 0 0
Condition0%0/6
exists $self->{$key} and $fields->{$key}->is_one and $fields->{$key}->is_own
!ll&&!rl&&r
000
exists $self->{$key} and $fields->{$key}->is_one
!ll&&!rl&&r
000
MC/DC0%0/3
exists $self->{$key} and $fields->{$key}->is_one and $fields->{$key}->is_own
exists $self->{$key} $fields->{$key}->is_one $fields->{$key}->is_own
Truth table0%0/4
A and B and C
A= exists $self->{$key}B= $fields->{$key}->is_oneC= $fields->{$key}->is_own
ABCresult
0XX0
10X0
1100
1111
468 }
469 else {
470 0 ▶ $final->{$key} = $fields->{$key}->to_shopify($self->$key) if exists $self->{$key};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{$key} 0 0 0
471 }
472 }
473 0 return $final;
474 }
475  
476 sub generate_accessors {
477 return join("\n",
478 68 (map { "__PACKAGE__->queries->{$_}->name('$_');" } keys(%{$_[0]->queries})),
479 682 (map { "__PACKAGE__->fields->{$_}->name('$_');" } keys(%{$_[0]->fields})),
480 32 ▶ (map { "sub $_ { \$_[0]->{$_} = \$_[1] if defined \$_[1]; return undef unless defined \$_[0]->{$_}; return \@{\$_[0]->{$_}} if wantarray; return \$_[0]->{$_}; }" } grep { $_ ne "metafields" && $_[0]->field($_)->is_relation && $_[0]->field($_)->is_many } keys(%{$_[0]->fields})),
Branch: 1 branch
BranchTrueFalseTotal
$_ ne "metafields" and $_[0]->field($_)->is_relation and $_[0]->field($_)->is_many 74 608 682
Condition100%3/3
$_ ne "metafields" and $_[0]->field($_)->is_relation
!ll&&!rl&&r
1059874
MC/DC100%2/2
$_ ne "metafields" and $_[0]->field($_)->is_relation
$_ ne "metafields" $_[0]->field($_)->is_relation
Truth table100%3/3
A and B
A= $_ ne "metafields"B= $_[0]->field($_)->is_relation
ABresult
0X0
100
111
481 640 ▶ (map { "sub $_ { \$_[0]->{$_} = \$_[1] if defined \$_[1]; return \$_[0]->{$_}; }" } grep { $_ ne "metafields" && (!$_[0]->field($_)->is_relation || !$_[0]->field($_)->is_many) } keys(%{$_[0]->fields})),
Branch: 1 branch
BranchTrueFalseTotal
$_ ne "metafields" and not $_[0]->field($_)->is_relation && $_[0]->field($_)->is_many 672 10 682
Condition100%6/6
$_ ne 'metafields' && !($_[0]->field($_)->is_relation && $_[0]->field($_)->is_many)
!ll&&!rl&&r
1032640
$_[0]->field($_)->is_relation && $_[0]->field($_)->is_many
!ll&&!rl&&r
5984232
MC/DC100%3/3
$_ ne 'metafields' && !($_[0]->field($_)->is_relation && $_[0]->field($_)->is_many)
$_ ne 'metafields' $_[0]->field($_)->is_relation $_[0]->field($_)->is_many
Truth table100%4/4
A && not(B && C)
A= $_ ne 'metafields'B= $_[0]->field($_)->is_relationC= $_[0]->field($_)->is_many
ABCresult
0XX0
1110
10X1
1101
482 65 ▶ (map { "sub $_ { my \$sa = \$_[0]->associate; die \"Can't call a special action on an unassociated item.\" unless \$sa; return \$sa->$_(\$_[0]); }" } $_[0]->actions)
Subroutine generate_accessors: called CC=5 SCAR=16.1
Pod undocumented
483 );
484 }
485  
486 0 ▶sub read_scope { return undef; }
Subroutine read_scope: not called CC=1 SCAR=6.9
Pod undocumented
487 0 ▶sub write_scope { return undef; }
Subroutine write_scope: not called CC=1 SCAR=6.9
Pod undocumented
488  
489 =head1 SEE ALSO
490  
491 L<WWW::Shopify>
492  
493 =head1 AUTHOR
494  
495 Adam Harrison
496  
497 =head1 LICENSE
498  
499 See LICENSE in the main directory.
500  
501 =cut
502  
503 1 1
« blib/lib/WWW/Shopify/Test.pm ↑ Summary blib/lib/WWW/Shopify/Common/DBIxGroup.pm »