File Coverage

blib/lib/DBIx/Class/Helper/Row/StorageValues.pm
Criterion Covered Total %
statement 37 37 100.0
branch 2 2 100.0
condition 1 3 33.3
subroutine 11 11 100.0
pod 6 7 85.7
total 57 60 95.0


line stmt bran cond sub pod time code
1             package DBIx::Class::Helper::Row::StorageValues;
2             $DBIx::Class::Helper::Row::StorageValues::VERSION = '2.035000';
3             # ABSTRACT: Keep track of stored vs in-memory row values
4              
5 56     56   27779 use strict;
  56         132  
  56         1641  
6 56     56   322 use warnings;
  56         115  
  56         1504  
7              
8 56     56   331 use parent 'DBIx::Class::Row';
  56         141  
  56         396  
9              
10             __PACKAGE__->mk_group_accessors(inherited => '_storage_value_columns');
11             __PACKAGE__->mk_group_accessors(inherited => '_storage_values');
12              
13 513     513   27388 sub _has_storage_value { $_[0]->column_info($_[1])->{keep_storage_value} }
14              
15             sub storage_value_columns {
16 248     248 1 432 my $self = shift;
17 248 100       5280 if (!$self->_storage_value_columns) {
18 241         40370 $self->_storage_value_columns([
19             grep $self->_has_storage_value($_),
20             $self->result_source->columns
21             ]);
22             }
23 248         22416 return $self->_storage_value_columns;
24             }
25              
26             sub store_storage_values {
27 248     248 0 503 my $self = shift;
28             $self->_storage_values({
29             map {
30 72   33     2152 my $acc = ($self->column_info($_)->{accessor} || $_);
31 72         5470 $_ => $self->$acc
32 248         445 } @{$self->storage_value_columns}
  248         624  
33             });
34 248         13431 $self->_storage_values;
35             }
36              
37 18     18 1 4899 sub get_storage_value { $_[0]->_storage_values->{$_[1]} }
38              
39             sub new {
40 1     1 1 474 my $class = shift;
41 1         75 my $ret = $class->next::method(@_);
42 1         133 $ret->_storage_values({});
43 1         15 $ret;
44             }
45              
46             sub inflate_result {
47 240     240 1 184686 my $class = shift;
48 240         1023 my $ret = $class->next::method(@_);
49 240         12518 $ret->store_storage_values;
50 240         6580 $ret;
51             }
52              
53             sub insert {
54 1     1 1 471 my $self = shift;
55 1         7 my $ret = $self->next::method(@_);
56 1         2008 $ret->store_storage_values;
57 1         13 $ret;
58             }
59              
60             sub update {
61 7     7 1 17 my $self = shift;
62 7         29 my $ret = $self->next::method(@_);
63 7         11985 $ret->store_storage_values;
64 7         95 $ret;
65             }
66              
67             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) 2020 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