Set/Storage/DBIC.pm

filter: Statementstmt 100% 50 / 50 Branchbran 100% 6 / 6 Conditioncond 100% 6 / 6 MC/DCmcdc 100% 3 / 3 Subroutinesub 100% 7 / 7 Podpod 100% 3 / 3 totaltotal 100% 75 / 75 CC 8 SCAR 11.9
CC
8
Cov
100%
__ANON__
5.0
populate_context
4.0
BEGIN
1.0
CRAP 3.3
« Set/Manager.pm ↑ Summary Set.pm »
1 package Context::Set::Storage::DBIC;
2 2 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 extends qw/Context::Set::Storage/;
4  
5 2 ▶use Log::Log4perl;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 my $LOGGER = Log::Log4perl->get_logger();
7  
8 =head1 NAME
9  
10 Context::Set::Storage::DBIC - Manage context persistence in a L<DBIx::Class::ResultSet>
11  
12 =head1 MANUAL
13  
14 This storage allows you to store your contexts and their values in a DBIC Resultset.
15  
16 This resultset MUST have the following columns:
17  
18 id: A unique numeric ID. Be generous (BIG NUM) as this will be incremented each time there's
19 a new value for a property. This is the only unique key.
20  
21 context_name: NOT NULL - A long enough VARCHAR. 512 is a good size. It must be able to contain
22 the longest context fullname possible for your application. No default.
23  
24 key : NOT NULL - A long enough VARCHAR. Must be able to contain the longest possible
25 property name for your application. No default.
26  
27 is_array : NOT NULL - A boolean. No default.
28  
29 value: CAN BE NULL. Something capable of holding any Perl string or number. VARCHAR(512) Is a good starting point.
30  
31  
32 Additionaly you may want to consider adding the following indices:
33  
34 (context_name) and (context_name, key)
35  
36  
37 Usage:
38  
39 my $storage = Context::Set::Storage::DBIC->new({ resultset => $schema->resultset('Context::SetValues') });
40 my $cm = Context::Set::Manager->new({ storage => $storage });
41 ...
42  
43  
44 =cut
45  
46 2 has 'resultset' => ( is => 'rw', isa => 'DBIx::Class::ResultSet' , required => 1 );
47  
48 =head2 populate_context
49  
50 See super class L<Context::Set::Storage>
51  
52 =cut
53  
54 30 ▶sub populate_context{
Subroutine populate_context: called CC=4 SCAR=13.9
Pod documented
55 30 my ($self,$context) = @_;
56  
57 30 my $rs = $self->resultset();
58 30 my $fullname = $context->fullname();
59  
60 30 $LOGGER->debug("LOADING ".$fullname." from DBIC Rs ".$rs->result_source->name());
61  
62 30 my $kvs = $self->resultset->search_rs({ context_name => $context->fullname() },
63 { order_by => [ 'key' , 'id' ] }
64 );
65 30 my $properties = {};
66 30 ▶ while( my $kv = $kvs->next() ){
Branch: 1 branch
BranchTrueFalseTotal
if (my $kv = $kvs->next) 28 30 58
67 28 my ($k ,$v) = ( $kv->key() , $kv->value() );
68 28 ▶ $properties->{$k} //= [];
Condition100%2/2
$properties->{$k} //= []
l!l
424
MC/DC100%1/1
$properties->{$k} //= []
$properties->{$k}
Truth table100%2/2
A
A= $properties->{$k}
Aresult
11
00
69 28 ▶ if( $kv->is_array() ){
Branch: 1 branch
BranchTrueFalseTotal
if ($kv->is_array) { } 10 18 28
70 10 push @{$properties->{$k}} , $v;
71 }else{
72 18 $properties->{$k} = $v;
73 }
74 }
75  
76 ## Inject all of that in the context.
77 30 $context->properties($properties);
78 }
79  
80 =head2 set_context_property
81  
82 See superclass L<Context::Set::Storage>
83  
84 =cut
85  
86 20 ▶sub set_context_property{
Subroutine set_context_property: called CC=1 SCAR=0.0
Pod documented
87 20 my ($self, $context, $prop , $v , $after ) = @_;
88  
89 20 ▶ my $stuff = sub{
Subroutine __ANON__: called CC=5 SCAR=16.1
90 20 my $is_array = 1;
91 ## Normalize v
92 20 ▶ unless( ref($v // 'nothing') eq 'ARRAY' ){
Branch: 1 branch
BranchTrueFalseTotal
unless (ref($v // "nothing") eq "ARRAY") 16 4 20
Condition100%2/2
$v // "nothing"
l!l
182
MC/DC100%1/1
$v // "nothing"
$v
Truth table100%2/2
A
A= $v
Aresult
11
00
93 16 $v = [ $v ];
94 16 $is_array = 0;
95 }
96  
97 20 my $fullname = $context->fullname();
98  
99 20 my $rs = $self->resultset();
100  
101 20 ▶ $LOGGER->debug("SETTING On Rs:'".$rs->result_source->name()."' context:'".$fullname."' key:'".$prop."' value:[".join(',', map{ $_ // 'UNDEF' } @$v).']');
Condition100%2/2
$_ // "UNDEF"
l!l
202
MC/DC100%1/1
$_ // "UNDEF"
$_
Truth table100%2/2
A
A= $_
Aresult
11
00
102  
103 ## Blat the key
104 20 $rs->search_rs({ context_name => $fullname,
105 key => $prop
106 })->delete();
107 ## And record each value (can be an array)
108 20 foreach my $value ( @$v ){
109 22 $rs->create({ context_name => $fullname,
110 key => $prop,
111 value => $value,
112 is_array => $is_array
113 });
114 }
115 20 return &{$after}();
116 20 };
117 20 return $self->resultset->result_source->schema()->txn_do($stuff);
118 }
119  
120 =head2 delete_context_property
121  
122 See superclass L<Context::Set::Storage>
123  
124 =cut
125  
126 2 ▶sub delete_context_property{
Subroutine delete_context_property: called CC=1 SCAR=0.0
Pod documented
127 2 my ($self, $context, $prop, $after) = @_;
128 2 my $rs = $self->resultset();
129 2 ▶ my $stuff = sub{
Subroutine __ANON__: called CC=1 SCAR=0.0
130 2 my $fullname = $context->fullname();
131 2 $LOGGER->debug("DELETING On RS '".$rs->result_source->name()."' context:'".$fullname."' key:'".$prop."'");
132 2 $rs->search_rs({ context_name => $fullname,
133 key => $prop
134 })->delete();
135 2 return &{$after}();
136 2 };
137 2 return $rs->result_source->schema()->txn_do($stuff);
138 }
139  
140 2 __PACKAGE__->meta->make_immutable();
141 2 1;
« Set/Manager.pm ↑ Summary Set.pm »