DBI/Column.pm

filter: Statementstmt 84.6% 66 / 78 Branchbran 72.2% 26 / 36 Conditioncond 77.7% 14 / 18 MC/DCmcdc 45.4% 5 / 11 Subroutinesub 76.4% 13 / 17 Podpod 100% 10 / 10 totaltotal 78.8% 134 / 170 CC 26 SCAR 16.5
CC
26
Cov
80%
is_boolean
12.0
serialize_metadata2
12.0
active
7.0
CRAP 5.2
« DBI/Filter/Truncate.pm ↑ Summary DBI/Filter/Boolean.pm »
1 30 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 30 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 package Jifty::DBI::Column;
5  
6 30 our $VERSION = '0.01';
7 30 ▶use base qw/Class::Accessor::Fast Jifty::DBI::HasFilters/;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 30 ▶use UNIVERSAL::require;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 30 ▶use version;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 30 my @attrs = qw/
12 name
13 type
14 default
15 readable writable
16 max_length
17 mandatory
18 virtual
19 computed
20 distinct
21 sort_order
22 refers_to by
23 alias_for_column
24 aliased_as
25 since till
26 indexed
27 _validator
28 _checked_for_validate_sub
29 record_class
30 attributes
31 case_sensitive
32 private
33 protected
34 encode_on_select
35 /;
36  
37 # these actually live in the attributes hash
38 30 my @handy_attrs = qw/
39 container
40 label hints render_as
41 display_length
42 documentation
43 valid_values
44 available_values
45 autocompleted
46 no_placeholder
47 /;
48  
49 # compat: this should probably never exist and be deprecated
50 30 our @ATTRS = (@attrs, @handy_attrs);
51  
52 30 __PACKAGE__->mk_accessors(@attrs);
53  
54 30 for my $attr (@handy_attrs) {
55 30 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
56 *$attr = sub {
57 139 ▶ my $self = shift;
Subroutine __ANON__: called CC=4 SCAR=14.2
58 139 ▶ $self->attributes({}) unless $self->attributes;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->attributes 0 139 139
59 139 ▶ return $self->attributes->{$attr} unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 132 7 139
60 7 ▶ $self->attributes->{$attr} = (@_ == 1 ? $_[0] : [@_]);
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 ? : 7 0 7
61 }
62 300 }
63  
64 =head1 NAME
65  
66 Jifty::DBI::Column - Encapsulates a single column in a Jifty::DBI::Record table
67  
68 =head1 DESCRIPTION
69  
70  
71 This class encapsulates a single column in a Jifty::DBI::Record table
72 description. It replaces the _accessible method in
73 L<Jifty::DBI::Record>.
74  
75 It has the following accessors: C<name type default validator boolean
76 refers_to readable writable length>.
77  
78 =cut
79  
80 =head2 new
81  
82 =cut
83  
84 sub new {
85 143 ▶ my ($class, $args) = @_;
Subroutine new: called CC=3 SCAR=11.0
Pod documented
86 143 my $self = $class->SUPER::new({});
87  
88 # run through accessors, push unknown keys into the attributes hash
89  
90 # XXX: we might want to construct the proper hash (lifting things
91 # not in @attrs into attributes and just pass the whole hash
92 143 $self->attributes({});
93 143 for (keys %$args) {
94 271 ▶ if ($self->can($_)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->can($_)) { } 264 7 271
95 264 $self->$_($args->{$_});
96 } else {
97 7 $self->attributes->{$_} = $args->{$_};
98 }
99 }
100  
101 143 return $self;
102 }
103  
104 =head2 is_numeric
105  
106 Returns true if the column is of some numeric type, otherwise returns false.
107  
108 =cut
109  
110 sub is_numeric {
111 7 ▶ my $self = shift;
Subroutine is_numeric: called CC=2 SCAR=6.9
Pod documented
112 7 ▶ if ( $self->type =~ /INT|NUMERIC|DECIMAL|REAL|DOUBLE|FLOAT/i ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->type =~ /INT|NUMERIC|DECIMAL|REAL|DOUBLE|FLOAT/i) 3 4 7
113 3 return 1;
114 }
115 4 return 0;
116 }
117  
118  
119  
120 =head2 is_string
121  
122 Returns true if this column is a text field
123  
124 =cut
125  
126  
127 sub is_string {
128 241 ▶ my $self = shift;
Subroutine is_string: called CC=2 SCAR=6.9
Pod documented
129 241 ▶ if ( $self->type =~ /CHAR|TEXT/i ){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->type =~ /CHAR|TEXT/i) 84 157 241
130 84 return 1;
131 }
132 157 return 0;
133 }
134  
135 =head2 is_boolean
136  
137 Returns true if this column is a boolean
138  
139 =cut
140  
141 sub is_boolean {
142 0 ▶ my $self = shift;
Subroutine is_boolean: not called CC=3 SCAR=24.8
Pod documented
143 0 ▶ return 1 if grep { $_->isa('Jifty::DBI::Filter::Boolean') } $self->output_filters;
Branch: 1 branch
BranchTrueFalseTotal
if grep {$_->isa("Jifty::DBI::Filter::Boolean");} $self->output_filters 0 0 0
144 0 ▶ return 1 if $self->type =~ /BOOL/i;
Branch: 1 branch
BranchTrueFalseTotal
if $self->type =~ /BOOL/i 0 0 0
145 0 return 0;
146 }
147  
148 =head2 serialize_metadata
149  
150 Returns a hash describing this column object with enough detail to
151 fully describe it in the database. Intentionally skips
152 C<record_class>, all column attributes starting with C<_>, and all
153 column attributes which are undefined. The "known" attributes in the
154 C<attributes> hash are flattened and returned as well. The list of
155 known attributes are:
156  
157 =over
158  
159 =item container
160  
161 =item label hints render_as
162  
163 =item display_length
164  
165 =item valid_values
166  
167 =item available_values
168  
169 =item autocompleted
170  
171 =item documentation
172  
173 =item no_placeholder
174  
175 Setting this to a true value causes L<Jifty::DBI::record/load_by_cols>
176 to not use a placeholder when loading the column. This can allow the
177 database to come up with better query plans in some cases.
178  
179 =back
180  
181 =cut
182  
183 sub serialize_metadata {
184 1 ▶ my $self = shift;
Subroutine serialize_metadata: called CC=4 SCAR=13.9
Pod documented
185 1 ▶ return {map { $_ => $self->$_() } grep { $_ ne 'attributes' && $_ ne 'record_class' && $_ !~ /^_/ && defined $self->$_}
Branch: 1 branch
BranchTrueFalseTotal
$_ ne "attributes" and $_ ne "record_class" and not $_ =~ /^_/ and defined $self->$_ 32 4 36
Condition100%6/6
$_ ne "attributes" and $_ ne "record_class" and not $_ =~ /^_/
!ll&&!rl&&r
2232
$_ ne "attributes" and $_ ne "record_class"
!ll&&!rl&&r
1134
MC/DC0%0/3
$_ ne "attributes" and $_ ne "record_class" and not $_ =~ /^_/
$_ ne "attributes" $_ ne "record_class" not $_ =~ /^_/
Truth table0%0/4
A and B and not(C)
A= $_ ne "attributes"B= $_ ne "record_class"C= not $_ =~ /^_/
ABCresult
0XX0
10X0
1100
1111
186 @ATTRS};
187 }
188  
189 =head2 serialize_metadata2
190  
191 Returns a hash describing this column object with enough detail to
192 fully describe it in the database. Intentionally skips C<record_class>,
193 all column attributes starting with C<_>, and all column attributes
194 which are undefined.
195  
196 =cut
197  
198 sub serialize_metadata2 {
199 0 ▶ my $self = shift;
Subroutine serialize_metadata2: not called CC=3 SCAR=24.8
Pod documented
200 0 ▶ return {map { $_ => $self->$_() } grep { $_ ne 'record_class' && $_ !~ /^_/ && defined $self->$_} @attrs};
Branch: 1 branch
BranchTrueFalseTotal
$_ ne "record_class" and not $_ =~ /^_/ and defined $self->$_ 0 0 0
Condition0%0/3
$_ ne "record_class" and not $_ =~ /^_/
!ll&&!rl&&r
000
MC/DC0%0/2
$_ ne "record_class" and not $_ =~ /^_/
$_ ne "record_class" not $_ =~ /^_/
Truth table0%0/3
A and not(B)
A= $_ ne "record_class"B= not $_ =~ /^_/
ABresult
0X0
100
111
201 }
202  
203 =head2 validator
204  
205 Gets/sets the validator coderef for the column.
206  
207 =cut
208  
209 sub validator {
210 59 ▶ my $self = shift;
Subroutine validator: called CC=6 SCAR=18.5
Pod documented
211  
212 59 ▶ if ( @_ ) {
Branch: 2 branches
BranchTrueFalseTotal
if (@_) { } 0 59 59
elsif (not $self->_checked_for_validate_sub || $self->_validator) { } 21 38 59
213 0 $self->_validator( shift );
214 }
215 ▶ elsif ( not $self->_checked_for_validate_sub and not $self->_validator ) {
Condition66%2/3
$self->_checked_for_validate_sub || $self->_validator
l!l&&r!l&&!r
38021
MC/DC50%1/2
$self->_checked_for_validate_sub || $self->_validator
$self->_checked_for_validate_sub $self->_validator
Truth table66%2/3
A || B
A= $self->_checked_for_validate_subB= $self->_validator
ABresult
1X1
011
000
216 21 ▶ my $name = ( $self->aliased_as ? $self->aliased_as : $self->name );
Branch: 1 branch
BranchTrueFalseTotal
$self->aliased_as ? : 0 21 21
217 21 my $can = $self->record_class->can( "validate_" . $name );
218  
219 21 ▶ $self->_validator( $can ) if $can;
Branch: 1 branch
BranchTrueFalseTotal
if $can 2 19 21
220 21 $self->_checked_for_validate_sub( 1 );
221 }
222  
223 59 return $self->_validator;
224 }
225  
226 # Aliases for compatibility with searchbuilder code
227 30 *read = \&readable;
228 30 *write = \&writable;
229  
230 =head2 read
231  
232 DEPRECATED. Use C<< $column->readable >> instead.
233  
234 =head2 write
235  
236 DEPRECATED. Use C<< $column->writable >> instead.
237  
238 =head2 length
239  
240 DEPRECATED. Use C<< $column->max_length >> instead.
241  
242 =head2 until
243  
244 DEPRECATED. Use C<< $column->till >> instead.
245  
246 =cut
247  
248 0 ▶sub length { Carp::croak('$column->length is no longer supported; use $column->max_length instead') }
Subroutine length: not called CC=1 SCAR=6.9
Pod documented
249 0 ▶sub until { Carp::croak('$column->until is no longer supported; use $column->till instead') }
Subroutine until: not called CC=1 SCAR=6.9
Pod documented
250  
251 =head2 active
252  
253 Returns the a true value if the column method exists for the current application
254 version. The current application version is determined by checking the L<Jifty::DBI::Record/schema_version> of the column's L</record_class>. This method returns a false value if the column is not yet been added or has been dropped.
255  
256 This method returns a false value under these circumstances:
257  
258 =over
259  
260 =item *
261  
262 Both the C<since> trait and C<schema_version> method are defined and C<schema_version> is less than the version set on C<since>.
263  
264 =item *
265  
266 Both the C<till> trait and C<schema_version> method are defined and C<schema_version> is greater than or equal to the version set on C<till>.
267  
268 =back
269  
270 Otherwise, this method returns true.
271  
272 =cut
273  
274 sub active {
275 448 ▶ my $self = shift;
Subroutine active: called CC=7 SCAR=19.5
Pod documented
276  
277 448 ▶ return 1 unless $self->record_class->can('schema_version');
Branch: 1 branch
BranchTrueFalseTotal
unless $self->record_class->can("schema_version") 369 79 448
278 79 ▶ return 1 unless defined $self->record_class->schema_version;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->record_class->schema_version 9 70 79
279  
280 70 my $version = version->new($self->record_class->schema_version);
281  
282 # The application hasn't yet started using this column
283 70 ▶ return 0 if defined $self->since
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->since and $version < "version"->new($self->since) 3 67 70
Condition100%3/3
defined $self->since and $version < "version"->new($self->since)
!ll&&!rl&&r
55123
MC/DC100%2/2
defined $self->since and $version < "version"->new($self->since)
defined $self->since $version < "version"->new($self->since)
Truth table100%3/3
A and B
A= defined $self->sinceB= $version < "version"->new($self->since)
ABresult
0X0
100
111
284 and $version < version->new($self->since);
285  
286 # The application stopped using this column
287 67 ▶ return 0 if defined $self->till
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->till and $version >= "version"->new($self->till) 6 61 67
Condition100%3/3
defined $self->till and $version >= "version"->new($self->till)
!ll&&!rl&&r
5566
MC/DC100%2/2
defined $self->till and $version >= "version"->new($self->till)
defined $self->till $version >= "version"->new($self->till)
Truth table100%3/3
A and B
A= defined $self->tillB= $version >= "version"->new($self->till)
ABresult
0X0
100
111
288 and $version >= version->new($self->till);
289  
290 # The application currently uses this column
291 61 return 1;
292 }
293  
294 30 1;
« DBI/Filter/Truncate.pm ↑ Summary DBI/Filter/Boolean.pm »