Bolts/Meta/Class/Trait/Bag.pm

filter: Statementstmt 76.4% 39 / 51 Branchbran 66.6% 12 / 18 Conditioncond 58.3% 7 / 12 MC/DCmcdc 28.5% 2 / 7 Subroutinesub 73.3% 11 / 15 Podpod 100% 3 / 3 totaltotal 69.8% 74 / 106 SCAR 29.2
CC
14
Cov
72%
_wrap_method_in_such_that_check
12.0
__ANON__
12.0
add_artifact
10.0
CRAP 18.5
« Bolts/Artifact.pm ↑ Summary Bolts/Bag.pm »
1 package Bolts::Meta::Class::Trait::Bag;
2 10 $Bolts::Meta::Class::Trait::Bag::VERSION = '0.143170';
3 # ABSTRACT: Metaclass role for Bolts-built bags
4  
5 10 ▶use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 10 ▶use Safe::Isa;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 10 ▶use Scalar::Util qw( reftype );
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10  
11 has artifacts => (
12 is => 'ro',
13 required => 1,
14 41 ▶ default => sub { +{} },
Subroutine __ANON__: called CC=1 SCAR=0.0
15 10 traits => [ 'Hash' ],
16 handles => {
17 '_add_artifact' => 'set',
18 'list_artifacts' => 'elements',
19 },
20 );
21  
22  
23 10 has such_that_isa => (
24 is => 'rw',
25 isa => 'Moose::Meta::TypeConstraint',
26 predicate => 'has_such_that_isa',
27 );
28  
29  
30 10 has such_that_does => (
31 is => 'rw',
32 isa => 'Moose::Meta::TypeConstraint',
33 predicate => 'has_such_that_does',
34 );
35  
36  
37 sub is_finished_bag {
38 51 ▶ my $meta = shift;
Subroutine is_finished_bag: called CC=1 SCAR=0.0
Pod documented
39 51 return $meta->is_immutable;
40 }
41  
42  
43 sub add_artifact {
44 200 ▶ my ($meta, $name, $value, $such_that) = @_;
Subroutine add_artifact: called CC=10 SCAR=23.1
Pod documented
45
46 200 ▶ if (!defined $such_that and ($meta->has_such_that_isa
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $such_that and $meta->has_such_that_isa || $meta->has_such_that_does) 157 43 200
Condition83%5/6
not defined $such_that and $meta->has_such_that_isa || $meta->has_such_that_does
!ll&&!rl&&r
046154
$meta->has_such_that_isa or $meta->has_such_that_does
l!l&&r!l&&!r
315443
MC/DC33%1/3
not defined $such_that and $meta->has_such_that_isa || $meta->has_such_that_does
not defined $such_that $meta->has_such_that_isa $meta->has_such_that_does
Truth table50%2/4
not(A) and B or C
A= not defined $such_thatB= $meta->has_such_that_isaC= $meta->has_such_that_does
ABCresult
0XX0
1000
11X1
1011
47 or $meta->has_such_that_does)) {
48 157 $such_that = {};
49 157 ▶ $such_that->{isa} = $meta->such_that_isa
Branch: 1 branch
BranchTrueFalseTotal
if $meta->has_such_that_isa 3 154 157
50 if $meta->has_such_that_isa;
51 157 ▶ $such_that->{does} = $meta->such_that_does
Branch: 1 branch
BranchTrueFalseTotal
if $meta->has_such_that_does 154 3 157
52 if $meta->has_such_that_does;
53 }
54  
55 200 ▶ if ($value->$_does('Bolts::Role::Artifact')) {
Branch: 2 branches
BranchTrueFalseTotal
if ($value->$Bolts::Meta::Class::Trait::Bag::_does('Bolts::Role::Artifact')) { } 198 2 200
elsif (defined builtin::reftype($value) and builtin::reftype($value) eq 'CODE') { } 1 1 2
56 198 ▶ $value->such_that($such_that) if $such_that;
Branch: 1 branch
BranchTrueFalseTotal
if $such_that 157 41 198
57 198 $meta->_add_artifact($name => $value);
58 198 ▶ $meta->add_method($name => sub { $value });
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine down: called CC=1 SCAR=0.0
59 }
60  
61 ▶ elsif (defined reftype($value) and reftype($value) eq 'CODE') {
Condition66%2/3
defined builtin::reftype($value) and builtin::reftype($value) eq "CODE"
!ll&&!rl&&r
101
MC/DC50%1/2
defined builtin::reftype($value) and builtin::reftype($value) eq "CODE"
defined builtin::reftype($value) builtin::reftype($value) eq "CODE"
Truth table66%2/3
A and B
A= defined builtin::reftype($value)B= builtin::reftype($value) eq "CODE"
ABresult
0X0
100
111
62 1 my $thunk = Bolts::Artifact::Thunk->new(
63 %$such_that,
64 thunk => $value,
65 );
66  
67 1 $meta->_add_artifact($name => $thunk);
68 1 ▶ $meta->add_method($name => sub { $thunk });
Subroutine __ANON__: not called CC=1 SCAR=1.2
69 }
70  
71 else {
72 # TODO It would be better to assert the validity of the checks on
73 # the value immediately.
74  
75 my $thunk = Bolts::Artifact::Thunk->new(
76 %$such_that,
77 2 ▶ thunk => sub { $value },
Subroutine __ANON__: called CC=1 SCAR=0.0
78 1 );
79  
80 1 $meta->_add_artifact($name => $thunk);
81 1 ▶ $meta->add_method($name => sub { $thunk });
Subroutine __ANON__: called CC=1 SCAR=0.0
82 }
83  
84 }
85  
86  
87 sub finish_bag {
88 23 ▶ my ($meta) = @_;
Subroutine finish_bag: called CC=1 SCAR=0.0
Pod documented
89  
90 23 $meta->make_immutable(
91 replace_constructor => 1,
92 replace_destructor => 1,
93 );
94  
95 23 return $meta;
96 }
97  
98 sub _wrap_method_in_such_that_check {
99 0 ▶ my ($meta, $code, $such_that) = @_;
Subroutine _wrap_method_in_such_that_check: not called CC=3 SCAR=24.8
100  
101 0 my $wrapped;
102 0 ▶ if (defined $such_that->{isa} or defined $such_that->{does}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $such_that->{'isa'} or defined $such_that->{'does'}) { } 0 0 0
Condition0%0/3
defined $such_that->{"isa"} or defined $such_that->{"does"}
l!l&&r!l&&!r
000
MC/DC0%0/2
defined $such_that->{"isa"} or defined $such_that->{"does"}
defined $such_that->{"isa"} defined $such_that->{"does"}
Truth table0%0/3
A or B
A= defined $such_that->{"isa"}B= defined $such_that->{"does"}
ABresult
1X1
011
000
103 $wrapped = sub {
104 0 ▶ my $result = $code->(@_);
Subroutine __ANON__: not called CC=3 SCAR=24.8
105  
106 $such_that->{isa}->assert_valid($result)
107 0 ▶ if defined $such_that->{isa};
Branch: 1 branch
BranchTrueFalseTotal
if defined $such_that->{"isa"} 0 0 0
108  
109 $such_that->{does}->assert_valid($result)
110 0 ▶ if defined $such_that->{does};
Branch: 1 branch
BranchTrueFalseTotal
if defined $such_that->{"does"} 0 0 0
111  
112 0 return $result;
113 0 };
114 }
115 else {
116 $wrapped = sub {
117 0 ▶ return scalar $code->(@_);
Subroutine __ANON__: not called CC=1 SCAR=6.9
118 0 };
119 }
120  
121 0 return $wrapped;
122 }
123  
124  
125 10 1;
126  
127 __END__
128  
129 =pod
130  
131 =encoding UTF-8
132  
133 =head1 NAME
134  
135 Bolts::Meta::Class::Trait::Bag - Metaclass role for Bolts-built bags
136  
137 =head1 VERSION
138  
139 version 0.143170
140  
141 =head1 DESCRIPTION
142  
143 While a bag may be any kind of object, this metaclass role on a bag provides some helpful utilities for creating and managing bags.
144  
145 =head1 ATTRIBUTES
146  
147 =head2 artifacts
148  
149 These are the artifacts that have been added to this bag. It is saved as a hash. You can get the hash of artifacts as a list using C<list_artifacts>. You add artifacts to this list using L</add_artifact>.
150  
151 =head2 such_that_isa
152  
153 This is a L<Moose::Meta::TypeConstraint> to apply to the L<Bolts::Artifact/isa_type> of all contained artifacts.
154  
155 =head2 such_that_does
156  
157 This is a L<Moose::Meta::TypeConstraint> to apply to the L<Bolts::Artifact/does_type> of all contained artifacts.
158  
159 =head1 METHODS
160  
161 =head2 is_finished_bag
162  
163 my $finished = $meta->is_finished_bag;
164  
165 This is used to determine if a bag's definition has already been performed and completed. At this time, it's just a synonym for L<Class::MOP::Class/is_immutable>.
166  
167 =head2 add_artifact
168  
169 $meta->add_artifact(name => $artifact, {
170 isa => $isa_type,
171 does => $does_type,
172 });
173  
174 Adds an artifact method to the bag with the given C<name>. The C<$artifact> may be an instance of L<Bolts::Role::Artifact>, a code reference to used to define a L<Bolts::Artifact::Thunk> or just another value, which will be wrapped in an anonymous sub and turned into a L<Bolts::Artifact::Thunk>.
175  
176 The C<isa> and C<does> will be applied to the artifact as appropriate.
177  
178 =head2 finish_bag
179  
180 $meta->finish_bag;
181  
182 This completes the bag building process and marks the Moose object as immutable. Aft this is called, L</is_finished_bag> returns true.
183  
184 =head1 AUTHOR
185  
186 Andrew Sterling Hanenkamp <hanenkamp@cpan.org>
187  
188 =head1 COPYRIGHT AND LICENSE
189  
190 This software is copyright (c) 2014 by Qubling Software LLC.
191  
192 This is free software; you can redistribute it and/or modify it under
193 the same terms as the Perl 5 programming language system itself.
194  
195 =cut
« Bolts/Artifact.pm ↑ Summary Bolts/Bag.pm »