Schema/Base/Attribute.pm

filter: Statementstmt 100% 88 / 88 Branchbran 100% 28 / 28 Conditioncond 100% 3 / 3 MC/DCmcdc 100% 2 / 2 Subroutinesub 100% 11 / 11 Podpod 100% 8 / 8 totaltotal 100% 140 / 140 CC 16 SCAR 14.1
CC
16
Cov
100%
find_attribute_value
7.0
find_or_create_attribute
5.0
search_attribute_values
4.0
CRAP 4.1
« Schema/ResultSet/Session.pm ↑ Summary Test/Role/MySQL.pm »
1 2 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 package Interchange6::Schema::Base::Attribute;
3  
4 =head1 NAME
5  
6 Interchange6::Schema::Base::Attribute
7  
8 =cut
9  
10 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 =head1 DESCRIPTION
14  
15 The Attribute base class is consumed by classes with attribute
16 relationships like L<Interchange6::Schema::Result::User>,
17 L<Interchange6::Schema::Result::Navigation>
18 and L<Interchange6::Schema::Result::Product>.
19  
20 =over 4
21  
22 =item B<Assumptions>
23  
24 This module assumes that your using standardized class naming.
25  
26 example: User in this example is the $base class so UserAttribute,
27 UserAttributeValue class naming would be used. These would
28 also use user_attributes_id and user_attributes_values_id as primary
29 keys. In general follow the example classes listed in description.
30  
31 =back
32  
33 =cut
34  
35 =head1 SYNOPSIS
36  
37 $navigation_object->add_attribute('meta_title','My very seductive title here!');
38  
39 =head1 METHODS
40  
41 =head2 add_attribute
42  
43 Add attribute.
44  
45 $base->add_attribute('hair_color', 'blond');
46  
47 Where 'hair_color' is Attribute and 'blond' is AttributeValue
48  
49 =cut
50  
51 sub add_attribute {
52 85 ▶ my ($self, $attr, $attr_value) = @_;
Subroutine add_attribute: called CC=1 SCAR=0.0
Pod documented
53 85 my $base = $self->result_source->source_name;
54  
55 # find or create attributes
56 85 my ($attribute, $attribute_value) = $self->find_or_create_attribute($attr, $attr_value);
57  
58 # create base_attribute object
59 85 my $base_attribute = $self->find_or_create_related(lc($base) . '_attributes',
60 {attributes_id => $attribute->id});
61 # create base_attribute_value
62 85 $base_attribute->create_related(lc($base) . '_attribute_values',
63 {attribute_values_id => $attribute_value->id});
64  
65 85 return $self;
66 }
67  
68 =head2 update_attribute_value
69  
70 Update base attribute value
71  
72 $base->update_attribute('hair_color', 'brown');
73  
74 =cut
75  
76 sub update_attribute_value {
77 2 ▶ my ($self, $attr, $attr_value) = @_;
Subroutine update_attribute_value: called CC=1 SCAR=0.0
Pod documented
78 2 my $base = $self->result_source->source_name;
79  
80 2 my ($attribute, $attribute_value) = $self->find_or_create_attribute($attr, $attr_value);
81  
82 2 my (undef, $base_attribute_value) = $self->find_base_attribute_value($attribute, $base);
83  
84 2 $base_attribute_value->update({attribute_values_id => $attribute_value->id});
85  
86 2 return $self;
87 }
88  
89 =head2 delete_attribute
90  
91 Delete $base attribute
92  
93 $base->delete_attribute('hair_color', 'purple');
94  
95 =cut
96  
97 sub delete_attribute {
98 2 ▶ my ($self, $attr, $attr_value) = @_;
Subroutine delete_attribute: called CC=1 SCAR=0.0
Pod documented
99 2 my $base = $self->result_source->source_name;
100  
101 2 my ($attribute) = $self->find_or_create_attribute($attr, $attr_value);
102  
103 2 my ($base_attribute, $base_attribute_value) = $self->find_base_attribute_value($attribute, $base);
104  
105 #delete
106 2 $base_attribute_value->delete;
107 2 $base_attribute->delete;
108  
109 2 return $self;
110 }
111  
112 =head2 search_attributes
113  
114 Returns attributes resultset for a $base object
115  
116 $rs = $base->search_attributes;
117  
118 You can pass conditions and attributes to the search like for
119 any L<DBIx::Class::ResultSet>, e.g.:
120  
121 $rs = $base->search_attributes(
122 undef, { order_by => 'priority desc' });
123  
124 =cut
125  
126 sub search_attributes {
127 5 ▶ my ($self, $condition, $search_atts) = @_;
Subroutine search_attributes: called CC=1 SCAR=0.0
Pod documented
128  
129 5 my $base = $self->result_source->source_name;
130  
131 5 my $base_attributes = $self->search_related(lc($base) . '_attributes');
132  
133 5 my $attributes = $base_attributes->search_related('attribute',
134 $condition, $search_atts);
135  
136 5 return $attributes;
137 }
138  
139 =head2 find_attribute_value
140  
141 Finds the attribute value for the current object or a defined object value.
142 If $object is passed the entire attribute_value object will be returned. $args can
143 accept both scalar and hash inputs.
144  
145 $base->find_attribute_value({name => $attr_name, priority => $attr_priority}, {object => 1});
146  
147 =cut
148  
149 sub find_attribute_value {
150 12 ▶ my ($self, $args, $object) = @_;
Subroutine find_attribute_value: called CC=7 SCAR=19.5
Pod documented
151 12 my $base = $self->result_source->source_name;
152 12 my $lc_base = lc($base);
153  
154 # attribute must be set
155 12 ▶ unless ($args) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($args) 1 11 12
156 1 die "find_attribute_value input requires at least a valid attribute value";
157 };
158  
159 11 ▶ my %attr = ref($args) eq 'HASH' ? %{$args} : (name => $args);
Branch: 1 branch
BranchTrueFalseTotal
ref $args eq 'HASH' ? : 1 10 11
160  
161 11 my $attribute = $self->result_source->schema->resultset('Attribute')->find( \%attr );
162  
163 11 ▶ unless ($attribute) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($attribute) 1 10 11
164 1 return undef;
165 }
166  
167 # find records
168 10 my $base_attribute = $self->find_related($lc_base . '_attributes',
169 {attributes_id => $attribute->id});
170  
171 10 ▶ unless ($base_attribute) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($base_attribute) 1 9 10
172 1 return undef;
173 }
174  
175 9 my $base_attribute_value = $base_attribute->find_related($lc_base .'_attribute_values',
176 {$lc_base . '_attributes_id' => $base_attribute->id});
177 9 ▶ unless ($base_attribute_value) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($base_attribute_value) 1 8 9
178 1 return undef;
179 }
180  
181 8 my $attribute_value = $base_attribute_value->find_related('attribute_value',
182 {lc($base) .'_attribute_values_id' => $base_attribute_value->id});
183 8 ▶ if ($object) {
Branch: 1 branch
BranchTrueFalseTotal
if ($object) { } 1 7 8
184 1 return $attribute_value;
185 }
186 else {
187 7 return $attribute_value->value;
188 }
189 };
190  
191 =head2 search_attribute_values
192  
193 =over 4
194  
195 =item Arguments: L<$cond|DBIx::Class::SQLMaker> | undef, L<\%attrs|DBIx::Class::ResultSet/ATTRIBUTES> | undef, L<\%av_attrs|DBIx::Class::ResultSet/ATTRIBUTES>
196  
197 Where $cond and %attrs are passed to the Attribute search and %av_attrs is passed to the AttributeValue search.
198  
199 =item Return Value: Array (or arrayref in scalar context) of attributes and values for for the $base object input.
200  
201 =back
202  
203 my $product = $schema->resultset('Product')->find({ sku = '123' });
204 my $av = $product->search_attribute_values(
205 undef, { order_by => 'priority' }, { order_by => 'priority' });
206  
207 =cut
208  
209 sub search_attribute_values {
210 2 ▶ my ($self, $condition, $search_atts, $av_search_atts) = @_;
Subroutine search_attribute_values: called CC=4 SCAR=13.9
Pod documented
211 2 my $base = $self->result_source->source_name;
212 2 my (%base_data, %attr_values, @data);
213  
214 2 my $base_attributes = $self->search_related(lc($base) . '_attributes');
215  
216 2 my $attributes_rs = $base_attributes->search_related('attribute',
217 $condition, $search_atts);
218  
219 2 ▶ while (my $attribute = $attributes_rs->next) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $attribute = $attributes_rs->next) 4 2 6
220 4 my @values;
221 4 my $attribute_value_rs = $attribute->search_related('attribute_values',
222 undef, $av_search_atts);
223 4 ▶ while (my $attribute_value = $attribute_value_rs->next) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $attribute_value = $attribute_value_rs->next) 6 4 10
224  
225 # get key value pairs
226 6 my %attr_values = $attribute_value->get_columns;
227 6 push( @values, { %attr_values });
228 }
229 4 my %base_data = $attribute->get_columns;
230  
231 # populate values
232 4 $base_data{attribute_values} = \@values;
233 4 push( @data, { %base_data });
234 }
235 2 ▶ return wantarray ? @data : \@data;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 1 1 2
236 };
237  
238 =head2 find_or_create_attribute
239  
240 Find or create attribute and attribute_value.
241  
242 =cut
243  
244 sub find_or_create_attribute {
245 93 ▶ my ( $self, $attr, $value ) = @_;
Subroutine find_or_create_attribute: called CC=5 SCAR=16.1
Pod documented
246  
247 93 ▶ unless ( defined($attr) && defined($value) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $attr and defined $value) 3 90 93
Condition100%3/3
defined $attr and defined $value
!ll&&!rl&&r
2190
MC/DC100%2/2
defined $attr and defined $value
defined $attr defined $value
Truth table100%3/3
A and B
A= defined $attrB= defined $value
ABresult
0X0
100
111
248 3 die "Both attribute and attribute value are required for find_or_create_attribute";
249 }
250  
251 # check if $attr is a HASH if not set as name
252 90 ▶ my %attr = ref($attr) eq 'HASH' ? %{$attr} : (name => $attr);
Branch: 1 branch
BranchTrueFalseTotal
ref $attr eq 'HASH' ? : 52 38 90
253  
254 # check if $value is a HASH if not set as value
255 90 ▶ my %attr_value = ref($value) eq 'HASH' ? %{$value} : (value => $value);
Branch: 1 branch
BranchTrueFalseTotal
ref $value eq 'HASH' ? : 3 87 90
256  
257 90 my $attribute = $self->result_source->schema->resultset('Attribute')->find_or_create( %attr );
258  
259 # create attribute_values
260 90 my $attribute_value = $attribute->find_or_create_related('attribute_values', \%attr_value );
261  
262 90 return ($attribute, $attribute_value);
263 };
264  
265 =head2 find_base_attribute_value
266  
267 From a $base->attribute input $base_attribute, $base_attribute_value is returned.
268  
269 =cut
270  
271 sub find_base_attribute_value {
272 7 ▶ my ($self, $attribute, $base) = @_;
Subroutine find_base_attribute_value: called CC=3 SCAR=11.0
Pod documented
273  
274 7 ▶ unless($base) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($base) 2 5 7
275 2 die "Missing base name for find_base_attribute_value";
276 }
277  
278 5 ▶ unless($attribute) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($attribute) 1 4 5
279 1 die "Missing attribute object for find_base_attribute_value";
280 }
281  
282 4 my $lc_base = lc($base);
283  
284 4 my $base_attribute = $self->find_related($lc_base . '_attributes',
285 {attributes_id => $attribute->id});
286  
287 4 my $base_attribute_value = $base_attribute->find_related($lc_base . '_attribute_values',
288 {$lc_base . '_attributes_id' => $base_attribute->id});
289  
290 4 return ($base_attribute, $base_attribute_value);
291 }
292  
293  
294 2 1;
« Schema/ResultSet/Session.pm ↑ Summary Test/Role/MySQL.pm »