Set/Manager.pm

filter: Statementstmt 94.2% 66 / 70 Branchbran 80.0% 16 / 20 Conditioncond 66.6% 4 / 6 MC/DCmcdc 33.3% 1 / 3 Subroutinesub 100% 15 / 15 Podpod 100% 4 / 4 totaltotal 89.8% 106 / 118 CC 14 SCAR 12.1
CC
14
Cov
90%
restrict
6.6
manage
5.0
unite
3.2
CRAP 3.4
« Set/Union.pm ↑ Summary Set/Storage/DBIC.pm »
1 package Context::Set::Manager;
2 3 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 3 ▶use Moose::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 3 ▶use Context::Set;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use Context::Set::Restriction;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use Context::Set::Union;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 3 ▶use Context::Set::Storage::BlackHole;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 6 ▶has '_localidx' => ( is => 'ro' , isa => 'HashRef[ArrayRef[Context::Set]]', default => sub{ {}; });
Subroutine __ANON__: called CC=1 SCAR=0.0
12 6 ▶has '_fullidx' => ( is => 'ro' , isa => 'HashRef' , default => sub{ {}; } );
Subroutine __ANON__: called CC=1 SCAR=0.0
13  
14 3 has 'universe' => ( is => 'ro' , isa => 'Context::Set' , required => 1 ,
15 lazy_build => 1 );
16  
17 has 'storage' => ( is => 'ro', isa => 'Context::Set::Storage' , required => 1,
18 2 ▶ default => sub{ return Context::Set::Storage::BlackHole->new() } );
Subroutine __ANON__: called CC=1 SCAR=0.0
19 3 has 'autoreload' => ( is => 'ro', isa => 'Bool', default => 0 );
20  
21 =head1 NAME
22  
23 Context::Set::Manager - A manager for your Context::Sets
24  
25 =head1 SYNOPSIS
26  
27 my $cm = Context::Set::Manager->new();
28  
29 my $users = $cm->restrict('users');
30 $users->set_property('page.color' , 'blue');
31  
32 my $user1 = $cm->restrict('users' , 1 );
33  
34 ## OR
35  
36 $user1 = $users->restrict(1);
37  
38 $user1->set_property('page.color' , 'pink');
39  
40 $user1->get_property('page.color'); # pink.
41  
42 $cm->restrict('users' , 2)->get_property('page.color'); # blue
43  
44 ## OR
45  
46 $users->restrict(2)->get_property('page.color'); # blue
47  
48 =head2 PERSISTENCE
49  
50 Give your manager a L<Context::Set::Storage> subclass at build time. So all managed context persist using this storage.
51  
52 For example:
53  
54 my $cm = Context::Set::Manager->new({ storage => an instance of Context::Set::Storage::DBIC });
55 ...
56  
57  
58 =head2 CONCURRENCY
59  
60 If your manager lives in a process and a stored value is changed by another process,
61 you can set this to autoreload the managed contexts on access. Use the option autoreload for that
62 (Note that it only makes sense with a Persistent storage (see PERSISTENCE):
63  
64 my $cm = Context::Set::Manager->new({ storage => an instance of Context::Set::Storage::DBIC,
65 autoreload => 1 });
66  
67 Note: Performance might be impacted. This will be solved in the future by implementing cachable
68 storages.
69  
70 =cut
71  
72 6 ▶sub _build_universe{
Subroutine _build_universe: called CC=1 SCAR=0.0
73 6 my ($self) = @_;
74  
75 6 my $universe = Context::Set->new();
76 6 return $self->manage($universe);
77 }
78  
79  
80 =head2 manage
81  
82 Adds the given Context::Set to this manager (in case it was built outside).
83  
84 Note that if a context with an identical fullname is already there, it
85 will return it. This is to ensure the unicity of contexts within the manager.
86  
87 Usage:
88  
89 $context = $cm->manage($context);
90  
91 =cut
92  
93 52 ▶sub manage{
Subroutine manage: called CC=5 SCAR=16.1
Pod documented
94 52 my ($self , $context) = @_;
95  
96 52 ▶ if( my $there = $self->_fullidx()->{$context->fullname()} ){
Branch: 1 branch
BranchTrueFalseTotal
if (my $there = $self->_fullidx->{$context->fullname}) 3 49 52
97 3 ▶ if( $self->autoreload() ){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->autoreload) 0 3 3
98 0 $self->storage->populate_context($there);
99 }
100 3 return $there;
101 }
102  
103 49 ▶ if( my $localname = $context->name() ){
Branch: 1 branch
BranchTrueFalseTotal
if (my $localname = $context->name) 37 12 49
104 37 ▶ $self->_localidx->{$localname} //= [];
Condition100%2/2
$self->_localidx->{$localname} //= []
l!l
730
MC/DC100%1/1
$self->_localidx->{$localname} //= []
$self->_localidx->{$localname}
Truth table100%2/2
A
A= $self->_localidx->{$localname}
Aresult
11
00
105 37 push @{$self->_localidx->{$localname}}, $context;
106 }
107 49 $self->_fullidx->{$context->fullname()} = $context;
108  
109 ## Let the storage fill up the full context.
110 49 $self->storage()->populate_context($context);
111  
112 ## Apply the managed role to this new context so this manager is contagious.
113 ## and it interact with the storage.
114 49 Moose::Util::ensure_all_roles($context,
115 'Context::Set::Role::Managed',
116 'Context::Set::Role::Stored',
117 );
118 ## Dont forget to inject myself.
119 49 $context->manager($self);
120 ## and the storage
121 49 $context->storage($self->storage());
122 49 return $context;
123 }
124  
125 =head2 restrict
126  
127 Builds a restriction of the universe or of the given context.
128  
129 Usage:
130  
131 my $users = $cm->restrict('users'); ## This restricts the UNIVERSE
132 my $user1 = $cm->restrict($users, 1); ## This restricts the users.
133 my $user1 = $cm->restrict('users' , 1); ## Same thing
134 my $user1 = $cm->restruct('UNIVERSE/users' , 1); ## Same thing.
135  
136 =cut
137  
138 32 ▶sub restrict{
Subroutine restrict: called CC=6 SCAR=18.8
Pod documented
139 32 my ($self, $c1, $new_name) = @_;
140 32 ▶ unless( $new_name ){
Branch: 1 branch
BranchTrueFalseTotal
unless ($new_name) 11 21 32
141 11 ▶ unless( $c1 ){
Branch: 1 branch
BranchTrueFalseTotal
unless ($c1) 0 11 11
142 0 confess("Missing restriction name");
143 }
144 11 return $self->_restrict_context($self->universe(), $c1);
145 }
146  
147 21 ▶ if( my $context = $self->find($c1) ){
Branch: 1 branch
BranchTrueFalseTotal
if (my $context = $self->find($c1)) 20 1 21
148 20 ▶ unless( $new_name ){
Branch: 1 branch
BranchTrueFalseTotal
unless ($new_name) 0 20 20
149 0 confess("Missing restriction name");
150 }
151 20 return $self->_restrict_context($self->find($c1) , $new_name);
152 }
153 1 ▶ confess("Cannot find context '".( $c1 // 'UNDEFINED' )."' to restrict");
Condition50%1/2
$c1 // "UNDEFINED"
l!l
10
MC/DC0%0/1
$c1 // "UNDEFINED"
$c1
Truth table50%1/2
A
A= $c1
Aresult
11
00
154 }
155  
156 =head2 unite
157  
158 Returns the union of the given Context::Sets. You need to give at least two contexts.
159  
160 Context::Sets can be given by name or by references.
161  
162 Usage:
163  
164 my $ctx = $this->unite('context1' , $context2);
165 my $ctx = $this->unite($context1, 'context2', $context3);
166  
167 =cut
168  
169 12 ▶sub unite{
Subroutine unite: called CC=3 SCAR=11.8
Pod documented
170 12 my ($self , @contexts ) = @_;
171 12 ▶ unless( scalar(@contexts) >= 2 ){
Branch: 1 branch
BranchTrueFalseTotal
unless (scalar @contexts >= 2) 0 12 12
172 0 confess("You need to unite at least 2 Context::Sets");
173 }
174  
175 12 ▶ @contexts = map{ $self->find($_) or die "Cannot find Context::Set to unite for '$_'" } @contexts;
Condition50%1/2
$self->find($_) || die("Cannot find Context::Set to unite for '${_}'")
l!l
240
MC/DC0%0/1
$self->find($_) || die("Cannot find Context::Set to unite for '${_}'")
$self->find($_)
Truth table50%1/2
A
A= $self->find($_)
Aresult
11
00
176 12 return $self->manage(Context::Set::Union->new({ contexts => \@contexts }));
177 }
178  
179  
180 31 ▶sub _restrict_context{
Subroutine _restrict_context: called CC=1 SCAR=0.0
181 31 my ($self, $c1 , $new_name) = @_;
182 31 return $self->manage(Context::Set::Restriction->new({ name => $new_name,
183 restricted => $c1 }));
184 }
185  
186 =head2 find
187  
188 Finds one context by the given name (local or full). Returns undef if nothing is found.
189  
190 If the name only match a local name and there's more that one Context::Set with this name, the latest one will be returned.
191  
192 Usage:
193  
194 if( my $context = $this->find('a_name') ){
195  
196 $this->find('UNIVERSE/name1/name2');
197  
198 if( $this->find($a_context) ){ ## Is this context in this manager
199  
200 =cut
201  
202 109 ▶sub find{
Subroutine find: called CC=3 SCAR=11.0
Pod documented
203 109 my ($self ,$name) = @_;
204  
205 ## Dereference if its a reference. Will not work with anything
206 ## else but Context::Sets.
207 109 ▶ if( ref($name) ){ return $self->find($name->fullname()); }
Branch: 1 branch
BranchTrueFalseTotal
if (ref $name) 35 74 109
208  
209 ## Case of fullname match
210 74 ▶ if( my $c = $self->_fullidx()->{$name} ){ return $c;}
Branch: 1 branch
BranchTrueFalseTotal
if (my $c = $self->_fullidx->{$name}) 39 35 74
211  
212 ## Case of local name match.
213 35 return $self->_localidx()->{$name}->[-1];
214 }
215  
216  
217 3 __PACKAGE__->meta->make_immutable();
218 3 1;
« Set/Union.pm ↑ Summary Set/Storage/DBIC.pm »