Helper/Row/StorageValues.pm

filter: Statementstmt 80.4% 33 / 41 Branchbran 100% 2 / 2 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 81.8% 9 / 11 Podpod 85.7% 6 / 7 totaltotal 77.2% 51 / 66 CC 3 SCAR 3.8
CC
3
Cov
78%
store_storage_values
2.0
storage_value_columns
2.0
new
2.0
CRAP 1.5
« Helper/Schema/DidYouMean.pm ↑ Summary Helper/ResultSet/Shortcut/Distinct.pm »
1 package DBIx::Class::Helper::Row::StorageValues;
2 54 $DBIx::Class::Helper::Row::StorageValues::VERSION = '2.032002';
3 # ABSTRACT: Keep track of stored vs in-memory row values
4  
5 54 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 54 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 54 ▶use parent 'DBIx::Class::Row';
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 54 __PACKAGE__->mk_group_accessors(inherited => '_storage_value_columns');
11 54 __PACKAGE__->mk_group_accessors(inherited => '_storage_values');
12  
13 512 ▶sub _has_storage_value { $_[0]->column_info($_[1])->{keep_storage_value} }
Subroutine _has_storage_value: called CC=1 SCAR=0.0
14  
15 sub storage_value_columns {
16 248 ▶ my $self = shift;
Subroutine storage_value_columns: called CC=2 SCAR=6.9
Pod documented
17 248 ▶ if (!$self->_storage_value_columns) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_storage_value_columns) 241 7 248
18 241 $self->_storage_value_columns([
19 grep $self->_has_storage_value($_),
20 $self->result_source->columns
21 ]);
22 }
23 248 return $self->_storage_value_columns;
24 }
25  
26 sub store_storage_values {
27 248 ▶ my $self = shift;
Subroutine store_storage_values: called CC=2 SCAR=7.1
Pod undocumented
28 $self->_storage_values({
29 map {
30 70 ▶ my $acc = ($self->column_info($_)->{accessor} || $_);
Condition33%1/3
$self->column_info($_)->{'accessor'} || $_
l!l&&r!l&&!r
0700
MC/DC0%0/2
$self->column_info($_)->{'accessor'} || $_
$self->column_info($_)->{'accessor'} $_
Truth table33%1/3
A || B
A= $self->column_info($_)->{'accessor'}B= $_
ABresult
1X1
011
000
31 70 $_ => $self->$acc
32 248 } @{$self->storage_value_columns}
33 });
34 248 $self->_storage_values;
35 }
36  
37 15 ▶sub get_storage_value { $_[0]->_storage_values->{$_[1]} }
Subroutine get_storage_value: called CC=1 SCAR=0.0
Pod documented
38  
39 sub new {
40 0 ▶ my $class = shift;
Subroutine new: not called CC=1 SCAR=6.9
Pod documented
41 0 my $ret = $class->next::method(@_);
42 0 $ret->store_storage_values;
43 0 $ret;
44 }
45  
46 sub inflate_result {
47 241 ▶ my $class = shift;
Subroutine inflate_result: called CC=1 SCAR=0.0
Pod documented
48 241 my $ret = $class->next::method(@_);
49 241 $ret->store_storage_values;
50 241 $ret;
51 }
52  
53 sub insert {
54 0 ▶ my $self = shift;
Subroutine insert: not called CC=1 SCAR=6.9
Pod documented
55 0 my $ret = $self->next::method(@_);
56 0 $ret->store_storage_values;
57 0 $ret;
58 }
59  
60 sub update {
61 7 ▶ my $self = shift;
Subroutine update: called CC=1 SCAR=0.0
Pod documented
62 7 my $ret = $self->next::method(@_);
63 7 $ret->store_storage_values;
64 7 $ret;
65 }
66  
67 54 1;
68  
69 __END__
70  
71 =pod
72  
73 =head1 NAME
74  
75 DBIx::Class::Helper::Row::StorageValues - Keep track of stored vs in-memory row values
76  
77 =head1 SYNOPSIS
78  
79 package MyApp::Schema::Result::BlogPost;
80  
81 use parent 'DBIx::Class::Core';
82  
83 __PACKAGE__->load_components(qw(Helper::Row::StorageValues));
84  
85 __PACKAGE__->table('BlogPost');
86 __PACKAGE__->add_columns(
87 id => {
88 data_type => 'integer',
89 is_auto_increment => 1,
90 },
91 title => {
92 data_type => 'varchar',
93 length => 32,
94 keep_storage_value => 1,
95 },
96 body => {
97 data_type => 'text',
98 },
99 );
100  
101 1;
102  
103 # elsewhere:
104  
105 my $post = $blog_rs->create({
106 title => 'Components for fun and profit',
107 body => '...',
108 });
109  
110 $post->title('Components for fun');
111  
112 warn sprintf 'Changing title from %s to %s',
113 $post->storage_value('title'), $post->title;
114  
115 $post->update;
116  
117 =head1 DESCRIPTION
118  
119 This component keeps track of the value for a given column in the database. If
120 you change the column's value and do not call C<update>, the C<storage_value>
121 will be different; once C<update> is called the C<storage_value> will be set
122 to the value of the accessor. Note that the fact that it uses the accessor is
123 an important distinction. If you are using L<DBIx::Class::FilterColumn> or
124 L<DBIx::Class::InflateColumn> it will get the non-storage or inflated values,
125 respectively.
126  
127 =head1 METHODS
128  
129 =head2 _has_storage_value
130  
131 $self->_has_storage_value('colname')
132  
133 returns true if we should store the storage value from the database. Override
134 this if you'd like to enable storage on all integers or something like that:
135  
136 sub _has_storage_value {
137 my ( $self, $column ) = @_;
138  
139 my $info = $self->column_info($column);
140  
141 return defined $info->{data_type} && $info->{data_type} eq 'integer';
142 }
143  
144 =head2 storage_value_columns
145  
146 $self->storage_value_columns
147  
148 returns a list of columns to store
149  
150 =head2 get_storage_value
151  
152 $self->get_storage_value('colname')
153  
154 returns the value for that column which is in storage
155  
156 =head1 AUTHOR
157  
158 Arthur Axel "fREW" Schmidt <frioux+cpan@gmail.com>
159  
160 =head1 COPYRIGHT AND LICENSE
161  
162 This software is copyright (c) 2016 by Arthur Axel "fREW" Schmidt.
163  
164 This is free software; you can redistribute it and/or modify it under
165 the same terms as the Perl 5 programming language system itself.
166  
167 =cut
« Helper/Schema/DidYouMean.pm ↑ Summary Helper/ResultSet/Shortcut/Distinct.pm »