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

filter: Statementstmt 24.7% 46 / 186 Branchbran 5.0% 4 / 80 Conditioncond 37.5% 9 / 24 MC/DCmcdc 35.7% 5 / 14 Subroutinesub 17.7% 14 / 79 Podpod 56.5% 39 / 69 totaltotal 25.8% 117 / 452 CC 50 SCAR 43.1
CC
50
Cov
20%
to_json
380.0
decodeForRef
156.0
metafields
20.0
CRAP 74.4
« blib/lib/WWW/Shopify/Test.pm ↑ Summary blib/lib/WWW/Shopify.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 # List of fields that should be filled automatically on creation.
118 0 ▶sub is_nested { return undef; }
Subroutine is_nested: not called CC=1 SCAR=6.9
Pod undocumented
119 1 ▶sub identifier { return ("id"); }
Subroutine identifier: called CC=1 SCAR=0.0
Pod undocumented
120  
121 # Counts the ::. Is helpful in a bunch of ways.
122 sub nested_level {
123 0 ▶ my ($package) = @_;
Subroutine nested_level: not called CC=2 SCAR=17.9
Pod undocumented
124 0 my $level = -2;
125 0 ▶ while ($package =~ m/::/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($package =~ /::/g) 0 0 0
126 0 ++$level;
127 }
128 0 return $level;
129 }
130  
131  
132 # I cannot fucking believe I'm doing this. Everything can be counted.
133 # Oh, except themes. We don't count those. For some arbitrary reason.
134  
135 =head2 countable($package), gettable($package), creatable($package), updatable($package), deletable($package), searchable($package)
136  
137 Determines whether or not this package can perform these main actions. 1 for yes, undef for no.
138  
139 =cut
140  
141 0 ▶sub countable { return 1; }
Subroutine countable: not called CC=1 SCAR=6.9
Pod documented
142 0 ▶sub gettable { return 1; }
Subroutine gettable: not called CC=1 SCAR=6.9
Pod documented
143 0 ▶sub singlable { return $_[0]->gettable; }
Subroutine singlable: not called CC=1 SCAR=6.9
Pod undocumented
144 0 ▶sub creatable { return 1; }
Subroutine creatable: not called CC=1 SCAR=6.9
Pod documented
145 0 ▶sub updatable { my @fields = $_[0]->update_fields; return int(@fields) > 0; }
Subroutine updatable: not called CC=1 SCAR=6.9
Pod documented
146 0 ▶sub deletable { return 1; }
Subroutine deletable: not called CC=1 SCAR=6.9
Pod documented
147 0 ▶sub searchable { return undef; }
Subroutine searchable: not called CC=1 SCAR=6.9
Pod documented
148  
149 =head2 actions($package)
150  
151 Returns an array of special actions that this package can perform (for orders, they can open, close, and cancel).
152  
153 =cut
154  
155 57 ▶sub actions { return qw(); }
Subroutine actions: called CC=1 SCAR=0.0
Pod documented
156 1 ▶use List::Util qw(first);
Subroutine BEGIN: called CC=1 SCAR=0.0
157 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
158 0 ▶sub can_open { return $_[0]->can_action("open"); }
Subroutine can_open: not called CC=1 SCAR=6.9
Pod undocumented
159 0 ▶sub can_activate { return $_[0]->can_action("activate"); }
Subroutine can_activate: not called CC=1 SCAR=6.9
Pod undocumented
160 0 ▶sub can_deactivate { return $_[0]->can_action("deactivate"); }
Subroutine can_deactivate: not called CC=1 SCAR=6.9
Pod undocumented
161 0 ▶sub can_enable { return $_[0]->can_action("enable"); }
Subroutine can_enable: not called CC=1 SCAR=6.9
Pod undocumented
162 0 ▶sub can_disable { return $_[0]->can_action("disable"); }
Subroutine can_disable: not called CC=1 SCAR=6.9
Pod undocumented
163 0 ▶sub can_close { return $_[0]->can_action("close"); }
Subroutine can_close: not called CC=1 SCAR=6.9
Pod undocumented
164 0 ▶sub can_cancel { return $_[0]->can_action("cancel"); }
Subroutine can_cancel: not called CC=1 SCAR=6.9
Pod undocumented
165  
166 =head2 create_method($package), update_method($package), delete_method($package)
167  
168 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:
169  
170 =over 4
171  
172 =item CREATE => POST
173  
174 =item UPDATE => PUT
175  
176 =item DELETE => DELETE
177  
178 =back
179  
180 But not always.
181  
182 =cut
183  
184 # I CANNOT FUCKING BELIEVE I AM DOING THIS; WHAT THE FUCK SHOPIFY. WHY!? WHY MAKE IT DIFFERENT ARBITRARILY!?
185 0 ▶sub create_method { return "POST"; }
Subroutine create_method: not called CC=1 SCAR=6.9
Pod documented
186 0 ▶sub update_method { return "PUT"; }
Subroutine update_method: not called CC=1 SCAR=6.9
Pod documented
187 0 ▶sub delete_method { return "DELETE"; }
Subroutine delete_method: not called CC=1 SCAR=6.9
Pod documented
188  
189 =head2 queries($package)
190  
191 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...)
192  
193 =cut
194  
195 51 ▶sub queries { return {}; }
Subroutine queries: called CC=1 SCAR=0.0
Pod documented
196  
197 =head2 unique_fields($package)
198  
199 An array of fields which have to be unique in this package (WWW::Shopify::Model::Customer's email, for example)
200  
201 =cut
202  
203 0 ▶sub unique_fields { return qw(); }
Subroutine unique_fields: not called CC=1 SCAR=6.9
Pod documented
204  
205 =head2 get_fields($package)
206  
207 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.
208  
209 =cut
210  
211 0 ▶sub get_fields { return keys(%{$_[0]->fields}); }
Subroutine get_fields: not called CC=1 SCAR=6.9
Pod documented
212  
213 =head2 creation_minimal($package)
214  
215 Returns an array of fields that are required before you send an object off to be created on Shopify.
216  
217 =cut
218  
219 0 ▶sub creation_minimal { return (); }
Subroutine creation_minimal: not called CC=1 SCAR=6.9
Pod documented
220  
221 =head2 creation_filled($package)
222  
223 Returns an array of fields that you should be getting back filled with data when the object is created on Shopify.
224  
225 =cut
226  
227 0 ▶sub creation_filled { return qw(id); }
Subroutine creation_filled: not called CC=1 SCAR=6.9
Pod documented
228  
229 =head2 update_fields($package)
230  
231 Returns an array of fields that you can update when an object is already created.
232  
233 =cut
234  
235 0 ▶sub update_fields { return qw(); }
Subroutine update_fields: not called CC=1 SCAR=6.9
Pod documented
236  
237 =head2 update_filled($package)
238  
239 Returns an array of fields that will be changed/filled when an object is updated.
240  
241 =cut
242  
243 0 ▶sub update_filled { return qw(); }
Subroutine update_filled: not called CC=1 SCAR=6.9
Pod documented
244  
245  
246 =head2 throws_webhooks($package), throws_create_webhooks($package), throws_update_webhooks($package), throws_delete_webhooks($package)
247  
248 Tells you whether or not the object throws webhooks for a living.
249  
250 =cut
251  
252 0 ▶sub throws_webhooks { return undef; }
Subroutine throws_webhooks: not called CC=1 SCAR=6.9
Pod documented
253 0 ▶sub throws_create_webhooks { return $_[0]->throws_webhooks; }
Subroutine throws_create_webhooks: not called CC=1 SCAR=6.9
Pod documented
254 0 ▶sub throws_update_webhooks { return $_[0]->throws_webhooks; }
Subroutine throws_update_webhooks: not called CC=1 SCAR=6.9
Pod documented
255 0 ▶sub throws_delete_webhooks { return $_[0]->throws_webhooks; }
Subroutine throws_delete_webhooks: not called CC=1 SCAR=6.9
Pod documented
256  
257 # Oh fucking WOW. WHAT THE FUCK. Variants, of course, delete directly with their id, and modify with it.
258 # Metafields delete with their id, but modify through their parent. They also get through their parents.
259 # Variants of course, get through their id directly. I'm at a loss for words. Why!? Article are different, yet again.
260  
261 =head2 get_all_through_parent($package), get_through_parent($package), create_through_parent($package), update_through_parent($package), delete_through_parent($package)
262  
263 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
264  
265 These tell you which objects have to go through their parent, and which don't. It tends to be rather arbitrary.
266  
267 =cut
268  
269 0 ▶sub get_all_through_parent { return defined $_[0]->parent; }
Subroutine get_all_through_parent: not called CC=1 SCAR=6.9
Pod documented
270 0 ▶sub get_through_parent { return defined $_[0]->parent; }
Subroutine get_through_parent: not called CC=1 SCAR=6.9
Pod documented
271 0 ▶sub count_through_parent { return $_[0]->get_all_through_parent; }
Subroutine count_through_parent: not called CC=1 SCAR=6.9
Pod undocumented
272 0 ▶sub create_through_parent { return defined $_[0]->parent; }
Subroutine create_through_parent: not called CC=1 SCAR=6.9
Pod documented
273 0 ▶sub update_through_parent { return defined $_[0]->parent; }
Subroutine update_through_parent: not called CC=1 SCAR=6.9
Pod documented
274 0 ▶sub delete_through_parent { return defined $_[0]->parent; }
Subroutine delete_through_parent: not called CC=1 SCAR=6.9
Pod documented
275 0 ▶sub activate_through_parent { return defined $_[0]->parent; }
Subroutine activate_through_parent: not called CC=1 SCAR=6.9
Pod undocumented
276 0 ▶sub deactivate_through_parent { return defined $_[0]->parent; }
Subroutine deactivate_through_parent: not called CC=1 SCAR=6.9
Pod undocumented
277 0 ▶sub open_through_parent { return defined $_[0]->parent; }
Subroutine open_through_parent: not called CC=1 SCAR=6.9
Pod undocumented
278 0 ▶sub close_through_parent { return defined $_[0]->parent; }
Subroutine close_through_parent: not called CC=1 SCAR=6.9
Pod undocumented
279 0 ▶sub cancel_through_parent { return defined $_[0]->parent; }
Subroutine cancel_through_parent: not called CC=1 SCAR=6.9
Pod undocumented
280 0 ▶sub enable_through_parent { return defined $_[0]->parent; }
Subroutine enable_through_parent: not called CC=1 SCAR=6.9
Pod undocumented
281 0 ▶sub disable_through_parent { return defined $_[0]->parent; }
Subroutine disable_through_parent: not called CC=1 SCAR=6.9
Pod undocumented
282  
283 =head2 included_in_parent
284  
285 Tells you whether a sub-object is included by defualt when you get an object as a sub-object. Most cases is 1.
286  
287 =cut
288  
289 0 ▶sub included_in_parent { return 1; }
Subroutine included_in_parent: not called CC=1 SCAR=6.9
Pod documented
290  
291 =head2 field($package, $name), fields($package)
292  
293 Returns a WWW::Shopify::Field::... which describes the specified field, or returns a hash containing all fields.
294  
295 =cut
296  
297 ▶sub fields { }
Subroutine fields: not called CC=1 SCAR=0.0
Pod documented
298 1406 ▶sub field { my ($package, $name) = @_; return $package->fields->{$name}; }
Subroutine field: called CC=1 SCAR=0.0
Pod documented
299  
300 =head2 associate($self)
301  
302 Returns the WWW::Shopify object that created/updated/deleted this object last.
303  
304 =cut
305  
306 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
307  
308 =head2 associate_parent($self)
309  
310 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.
311  
312 =cut
313  
314  
315 1 ▶use Scalar::Util qw(weaken);
Subroutine BEGIN: called CC=1 SCAR=0.0
316 sub associated_parent {
317 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
318 0 $_[0]->{associated_parent} = $_[1];
319 0 weaken($_[0]->{associated_parent});
320 }
321 0 return $_[0]->{associated_parent};
322 }
323  
324 0 ▶sub has_metafields { return defined $_[0]->field('metafields'); }
Subroutine has_metafields: not called CC=1 SCAR=6.9
Pod undocumented
325  
326 =head2 metafields($self)
327  
328 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.
329  
330 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.
331  
332 If this is the case, call the method below.
333  
334 =cut
335  
336 1 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
337 sub metafields {
338 0 ▶ my $sa = $_[0]->associate;
Subroutine metafields: not called CC=4 SCAR=30.0
Pod documented
339 0 ▶ if (!defined $_[0]->{metafields}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $_[0]{"metafields"}) 0 0 0
340 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
341 0 $_[0]->{metafields} = [$sa->get_all('Metafield', { parent => $_[0] })];
342 }
343 0 ▶ return $_[0]->{metafields} unless wantarray;
Branch: 1 branch
BranchTrueFalseTotal
unless wantarray 0 0 0
344 0 return @{$_[0]->{metafields}};
345 }
346  
347 =head2 refesh_metafields($self)
348  
349 Returns the metafields associated with item, but ALWAYS performs a get request.
350  
351 =cut
352  
353 sub refresh_metafields {
354 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
355 0 return $_[0]->metafields;
356 }
357  
358 =head2 add_metafield($self, $metafield)
359  
360 Takes a WWW::Shopify::Model::Metafield object.
361  
362 =cut
363  
364 sub add_metafield {
365 0 ▶ my ($self, $metafield) = @_;
Subroutine add_metafield: not called CC=2 SCAR=17.9
Pod documented
366 0 my $sa = $_[0]->associate;
367 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
368 0 $metafield->associated_parent($self);
369 0 return $sa->create($metafield);
370 }
371  
372 =head2 from_json($package, $json_hash, [$associated])
373  
374 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.
375  
376 =cut
377  
378 sub from_json {
379 0 ▶ my ($package, $json, $associated) = @_;
Subroutine from_json: not called CC=3 SCAR=24.8
Pod documented
380  
381 sub decodeForRef {
382 0 ▶ my ($self, $json, $ref, $associated) = @_;
Subroutine decodeForRef: not called CC=12 SCAR=50.5
Pod undocumented
383 0 for (keys(%{$ref})) {
384 0 ▶ if ($ref->{$_}->is_relation()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ref->{$_}->is_relation) { } 0 0 0
385 0 my $package = $ref->{$_}->relation();
386 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
387 0 ▶ next unless exists $json->{$package->plural()};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $$json{$package->plural} 0 0 0
388 $self->{$_} = [map {
389 0 my $o = $package->from_json($_, $associated);
390 0 $o->associated_parent($self);
391 0 $o;
392 0 } @{$json->{$package->plural()}}];
393 }
394 ▶ 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
395 0 ▶ next unless (exists $json->{$_});
Branch: 1 branch
BranchTrueFalseTotal
unless exists $json->{$_} 0 0 0
396 0 $self->{$_} = $package->from_json($json->{$_}, $associated);
397 0 ▶ $self->{$_}->associated_parent($self) if $self->{$_};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{$_} 0 0 0
398 }
399 elsif ($ref->{$_}->is_one) {
400 0 ▶ $self->{$_} = $json->{$_} if exists $json->{$_};
Branch: 1 branch
BranchTrueFalseTotal
if exists $json->{$_} 0 0 0
401 }
402 else {
403 0 die "Relationship specified must be either many, or one in $package.";
404 }
405 }
406 else {
407 0 ▶ $self->{$_} = $ref->{$_}->from_shopify($json->{$_}) if exists $json->{$_};
Branch: 1 branch
BranchTrueFalseTotal
if exists $json->{$_} 0 0 0
408 }
409 }
410 }
411 0 ▶ return undef unless $json;
Branch: 1 branch
BranchTrueFalseTotal
unless $json 0 0 0
412  
413 0 my $self = $package->new();
414 0 ▶ $self->associate($associated) if ($associated);
Branch: 1 branch
BranchTrueFalseTotal
if $associated 0 0 0
415  
416 0 $self->decodeForRef($json, $self->fields, $associated);
417 0 return $self;
418 }
419  
420 =head2 to_json($self)
421  
422 Returns a hash that's ready to be converted in to a JSON string using encode_json from the JSON package.
423  
424 =cut
425  
426 sub to_json($) {
427 0 ▶ my ($self) = @_;
Subroutine to_json: not called CC=19 SCAR=59.4
Pod documented
428 0 my $fields = $self->fields();
429 0 my $final = {};
430 0 foreach my $key (keys(%$self)) {
431 0 ▶ next unless exists $fields->{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $fields->{$key} 0 0 0
432 0 ▶ if ($fields->{$key}->is_relation()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fields->{$key}->is_relation) { } 0 0 0
433 0 ▶ if ($fields->{$key}->is_many()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fields->{$key}->is_many) 0 0 0
434 # Since metafields don't come prepackaged, we don't get them. Unless we've already got them.
435 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
436 0 my @results = $self->$key();
437 0 ▶ if (int(@results)) {
Branch: 1 branch
BranchTrueFalseTotal
if (int @results) { } 0 0 0
438 0 $final->{$key} = [map { $_->to_json() } @results];
439 }
440 else {
441 0 ▶ $final->{$key} = [] if exists $self->{$key};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{$key} 0 0 0
442 }
443 }
444 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
445 0 ▶ if (defined $self->$key()) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->$key) { } 0 0 0
446 # This is inconsistent; this if is a stop-gap measure.
447 # Getting directly from teh database seems to make this automatically an id.
448 0 ▶ if (ref($self->$key())) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self->$key) { } 0 0 0
449 0 $final->{$key} = $self->$key()->id();
450 }
451 else {
452 0 $final->{$key} = $self->$key();
453 }
454 }
455 else {
456 0 ▶ $final->{$key} = undef if exists $self->{$key};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{$key} 0 0 0
457 }
458 }
459 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
460 }
461 else {
462 0 ▶ $final->{$key} = $fields->{$key}->to_shopify($self->$key) if exists $self->{$key};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{$key} 0 0 0
463 }
464 }
465 0 return $final;
466 }
467  
468 sub generate_accessors {
469 return join("\n",
470 66 (map { "__PACKAGE__->queries->{$_}->name('$_');" } keys(%{$_[0]->queries})),
471 644 (map { "__PACKAGE__->fields->{$_}->name('$_');" } keys(%{$_[0]->fields})),
472 30 ▶ (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 69 575 644
Condition100%3/3
$_ ne "metafields" and $_[0]->field($_)->is_relation
!ll&&!rl&&r
1056569
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
473 604 ▶ (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 634 10 644
Condition100%6/6
$_ ne 'metafields' && !($_[0]->field($_)->is_relation && $_[0]->field($_)->is_many)
!ll&&!rl&&r
1030604
$_[0]->field($_)->is_relation && $_[0]->field($_)->is_many
!ll&&!rl&&r
5653930
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
474 64 ▶ (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
475 );
476 }
477  
478 0 ▶sub read_scope { return undef; }
Subroutine read_scope: not called CC=1 SCAR=6.9
Pod undocumented
479 0 ▶sub write_scope { return undef; }
Subroutine write_scope: not called CC=1 SCAR=6.9
Pod undocumented
480  
481 =head1 SEE ALSO
482  
483 L<WWW::Shopify>
484  
485 =head1 AUTHOR
486  
487 Adam Harrison
488  
489 =head1 LICENSE
490  
491 See LICENSE in the main directory.
492  
493 =cut
494  
495 1 1
« blib/lib/WWW/Shopify/Test.pm ↑ Summary blib/lib/WWW/Shopify.pm »