Set/Storage/Split.pm

filter: Statementstmt 97.2% 35 / 36 Branchbran 100% 2 / 2 Conditioncond 50.0% 1 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 100% 8 / 8 Podpod 100% 5 / 5 totaltotal 94.4% 51 / 54 CC 5 SCAR 7.0
CC
5
Cov
95%
_matching_storage
3.0
BUILDARGS
3.0
BEGIN
1.0
CRAP 2.0
« Set.pm ↑ Summary Set/Restriction.pm »
1 package Context::Set::Storage::Split;
2 2 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 extends qw/Context::Set::Storage/;
4  
5 2 ▶use Context::Set::Storage::Split::Rule;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 =head1 NAME
8  
9 Context::Set::Storage::Split - Split storage of Context::Set accross different L<Context::Set::Storage>'s
10  
11 =head1 MANUAL
12  
13 Contexts are dispatched accross different storage according to a set of rules.
14  
15 A rule should have:
16  
17 - name : This is free and mandatory.
18 - test: A Code Ref that test a given context (see example).
19 - storage: Where to store the contexts that pass the test. Should be an instance of any subclass of L<Context::Set::Storage>
20  
21 Rules are tested in the order of the definition, so the default one should
22 go at the end of the list. If you don't specify a default, this storage will die if
23 no rule matches the context you make it manage.
24  
25 For example:
26  
27 $users_store and $general_store are instances of L<Context::Set::Storage>
28  
29 ## Store the contexts unders 'users' in a special store, all the rest in a general one.
30 my $split_store = Context::Set::Storage::Split->new({
31 rules => [{
32 name => 'users_specific',
33 test => sub{ shift->is_inside('users'); },
34 storage => $users_store
35 },
36 {
37 name => 'default',
38 test => sub{ 1; },
39 storage => $general_store
40 }]
41 });
42  
43 =cut
44  
45  
46 2 has 'rules' => ( is => 'ro', isa => 'ArrayRef[Context::Set::Storage::Split::Rule]', required => 1);
47 2 has '_rules_idx' => ( is => 'ro' , isa => 'HashRef[Context::Set::Storage::Split::Rule]', required => 1);
48  
49  
50 =head2 BUILDARGS
51  
52 See L<Moose>
53  
54 In moose, we override BUILDARGS, not new.
55  
56 =cut
57  
58 1 ▶sub BUILDARGS{
Subroutine BUILDARGS: called CC=3 SCAR=11.0
Pod documented
59 1 my ($class, $args) = @_;
60  
61 ## Replace rules by an array of real rules.
62 1 my @new_rules = ();
63 1 my %rules_idx;
64 1 ▶ foreach my $rule ( @{ $args->{rules} // confess "Missing rules in args" } ){
Condition50%1/2
$args->{"rules"} // confess("Missing rules in args")
l!l
10
MC/DC0%0/1
$args->{"rules"} // confess("Missing rules in args")
$args->{"rules"}
Truth table50%1/2
A
A= $args->{"rules"}
Aresult
11
00
65 3 my $new_rule = Context::Set::Storage::Split::Rule->new($rule);
66 3 push @new_rules , $new_rule;
67 3 $rules_idx{$new_rule->name()} = $new_rule;
68 }
69  
70 1 $args->{_rules_idx} = \%rules_idx;
71 1 $args->{rules} = \@new_rules;
72 1 return $args;
73 }
74  
75 =head2 rule
76  
77 Returns a rule by name.
78  
79 Usage:
80  
81 $self->rule('myrule')->...
82  
83 =cut
84  
85 1 ▶sub rule{
Subroutine rule: called CC=1 SCAR=0.0
Pod documented
86 1 my ($self, $name) = @_;
87 1 return $self->_rules_idx->{$name};
88 }
89  
90 =head2 populate_context
91  
92 See super class L<Context::Set::Storage>
93  
94 =cut
95  
96 15 ▶sub populate_context{
Subroutine populate_context: called CC=1 SCAR=0.0
Pod documented
97 15 my ($self,$context) = @_;
98 15 return $self->_matching_storage($context)->populate_context($context);
99 }
100  
101 =head2 set_context_property
102  
103 See superclass L<Context::Set::Storage>
104  
105 =cut
106  
107 10 ▶sub set_context_property{
Subroutine set_context_property: called CC=1 SCAR=0.0
Pod documented
108 10 my ($self, $context, $prop , $v , $after ) = @_;
109 10 return $self->_matching_storage($context)->set_context_property($context,$prop,$v,$after);
110 }
111  
112 =head2 delete_context_property
113  
114 See superclass L<Context::Set::Storage>
115  
116 =cut
117  
118 1 ▶sub delete_context_property{
Subroutine delete_context_property: called CC=1 SCAR=0.0
Pod documented
119 1 my ($self, $context , $prop , $after) = @_;
120 1 return $self->_matching_storage($context)->delete_context_property($context,$prop,$after);
121 }
122  
123  
124 ## Return the storage for the given context according to
125 ## the rules.
126 26 ▶sub _matching_storage{
Subroutine _matching_storage: called CC=3 SCAR=11.0
127 26 my ($self, $context) = @_;
128  
129 ## Scan the rules and return the first matching one.
130 26 foreach my $rule ( @{$self->rules() } ){
131 57 ▶ if( $rule->test->($context) ){
Branch: 1 branch
BranchTrueFalseTotal
if ($rule->test->($context)) 26 31 57
132 26 return $rule->storage();
133 }
134 }
135 0 confess("Could NOT find any matching rule for context ".$context->fullname());
136 }
137  
138 2 __PACKAGE__->meta->make_immutable();
139 2 1;
« Set.pm ↑ Summary Set/Restriction.pm »