KiokuDB/TypeMap/Composite.pm

filter: Statementstmt 96.8% 62 / 64 Branchbran 66.6% 8 / 12 Conditioncond 50.0% 3 / 6 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 100% 16 / 16 Podpod n/a totaltotal 88.1% 89 / 101 CC 10 SCAR 8.5
CC
10
Cov
89%
_create_entry
4.2
_build_includes
4.1
_create_typemap
3.0
CRAP 2.3
« KiokuDB/Backend/Serialize/JSON.pm ↑ Summary KiokuDB/Backend/Role/TXN/Memory/Scan.pm »
1 #!/usr/bin/perl
2  
3 package KiokuDB::TypeMap::Composite;
4 23 ▶use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 23 ▶use KiokuDB::TypeMap;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 23 ▶use namespace::clean -except => 'meta';
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 {
11 package KiokuDB::TypeMap::Composite::TypeMapAttr;
12 23 ▶ use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 23 ▶ use namespace::clean -except => 'meta';
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 23 ▶ sub Moose::Meta::Attribute::Custom::Trait::KiokuDB::TypeMap::register_implementation { __PACKAGE__ }
Subroutine register_implementation: called CC=1 SCAR=0.0
17 }
18  
19 23 has override => (
20 isa => "HashRef[HashRef]",
21 is => "ro",
22 71 ▶ default => sub { +{} },
Subroutine __ANON__: called CC=1 SCAR=0.0
23 23 );
24  
25 has exclude => (
26 isa => "ArrayRef[Str]",
27 is => "ro",
28 71 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
29 23 );
30  
31 23 has _exclude => (
32 is => "ro",
33 lazy_build => 1,
34 );
35  
36 sub _build__exclude {
37 71 ▶ my $self = shift;
Subroutine _build__exclude: called CC=1 SCAR=0.2
38 71 return { map { $_ => undef } @{ $self->exclude } };
39 }
40  
41 sub _build_includes {
42 71 ▶ my $self = shift;
Subroutine _build_includes: called CC=4 SCAR=14.1
43  
44 71 my @attrs = $self->meta->get_all_attributes;
45  
46 71 my $exclude = $self->_exclude;
47  
48 my @typemap_attrs = grep {
49 71 ( my $short_name = $_->name ) =~ s/_typemap$//;
50  
51 $_->does("KiokuDB::TypeMap::Composite::TypeMapAttr")
52 and
53 ( !$short_name or !exists($exclude->{$short_name}) )
54 and
55 1109 ▶ !exists($exclude->{$_->name})
Branch: 1 branch
BranchTrueFalseTotal
$_->does("KiokuDB::TypeMap::Composite::TypeMapAttr") and not $short_name && exists $exclude->{$short_name} and not exists $$exclude{$_->name} 410 699 1109
Condition50%3/6
$_->does("KiokuDB::TypeMap::Composite::TypeMapAttr") and not $short_name && exists $exclude->{$short_name}
!ll&&!rl&&r
6990410
$short_name && exists $exclude->{$short_name}
!ll&&!rl&&r
04100
MC/DC0%0/3
$_->does("KiokuDB::TypeMap::Composite::TypeMapAttr") and not $short_name && exists $exclude->{$short_name}
$_->does("KiokuDB::TypeMap::Composite::TypeMapAttr") $short_name exists $exclude->{$short_name}
Truth table0%0/4
A and not(B && C)
A= $_->does("KiokuDB::TypeMap::Composite::TypeMapAttr")B= $short_nameC= exists $exclude->{$short_name}
ABCresult
0XX0
1110
10X1
1101
56 } @attrs;
57  
58 71 return [ map { $_->get_value($self) } @typemap_attrs ];
59 }
60  
61 sub _construct_entry {
62 950 ▶ my ( $self, @args ) = @_;
Subroutine _construct_entry: called CC=1 SCAR=0.0
63  
64 950 my $args = $self->_entry_options(@args);
65  
66 950 my $type = delete $args->{type};
67 950 Class::MOP::load_class($type);
68  
69 950 $type->new($args);
70 }
71  
72 sub _entry_options {
73 950 ▶ my ( $self, %args ) = @_;
Subroutine _entry_options: called CC=2 SCAR=7.0
74  
75 950 my $class = delete $args{class};
76  
77 950 ▶ return { %args, %{ $self->override->{$class} || {} }, };
Branch: 1 branch
BranchTrueFalseTotal
$self->override->{$class} or {} 950 0 950
78 }
79  
80 sub _create_entry {
81 950 ▶ my ( $self, $class, $entry ) = @_;
Subroutine _create_entry: called CC=4 SCAR=14.5
82  
83 950 ▶ return if exists $self->_exclude->{$class};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->_exclude->{$class} 0 950 950
84  
85 950 ▶ if ( blessed $entry ) {
Branch: 2 branches
BranchTrueFalseTotal
if (builtin::blessed($entry)) { } 0 950 950
elsif (ref $entry) { } 590 360 950
86 0 return ( $class => $entry );
87 } elsif ( ref $entry ) {
88 590 return ( $class => $self->_construct_entry( %$entry, class => $class ) );
89 } else {
90 360 return ( $class => $self->_construct_entry( type => $entry, class => $class ) );
91 }
92 }
93  
94 sub _create_entries {
95 590 ▶ my ( $self, $entries ) = @_;
Subroutine _create_entries: called CC=1 SCAR=0.0
96  
97 590 my $excl;
98  
99 return {
100 map {
101 590 my $class = $_;
102 950 my $entry = $entries->{$class};
103  
104 950 $self->_create_entry($class, $entry);
105 } keys %$entries
106 };
107 }
108  
109 sub _create_typemap {
110 410 ▶ my ( $self, %args ) = @_;
Subroutine _create_typemap: called CC=3 SCAR=11.0
111  
112 410 foreach my $entries ( @args{grep { exists $args{$_} } qw(entries isa_entries does_entries)} ) {
113 590 ▶ next unless $entries;
Branch: 1 branch
BranchTrueFalseTotal
unless $entries 0 590 590
114 590 $entries = $self->_create_entries($entries);
115 }
116  
117 410 KiokuDB::TypeMap->new(%args);
118 }
119  
120 sub _naive_isa_typemap {
121 22 ▶ my ( $self, $class, @args ) = @_;
Subroutine _naive_isa_typemap: called CC=1 SCAR=0.0
122  
123 22 $self->_create_typemap(
124 isa_entries => {
125 $class => {
126 type => "KiokuDB::TypeMap::Entry::Naive",
127 @args,
128 },
129 },
130 );
131 }
132  
133 23 __PACKAGE__
134  
135 __END__
136  
137 =pod
138  
139 =head1 NAME
140  
141 KiokuDB::TypeMap::Composite - A role for L<KiokuDB::TypeMaps> created out of
142 many smaller typemaps
143  
144 =head1 SYNOPSIS
145  
146 package MyTypeMap;
147 use Moose;
148  
149 extends qw(KiokuDB::TypeMap);
150  
151 with qw(KiokuDB::TypeMap::Composite);
152  
153  
154 # declare typemaps to inherit from using the KiokuDB::TypeMap trait
155 # the 'includes' attribute will be built by collecting these attrs:
156  
157 has foo_typemap => (
158 traits => [qw(KiokuDB::TypeMap)], # register for inclusion
159 does => "KiokUDB::Role::TypeMap",
160 is => "ro",
161 lazy_build => 1,
162 );
163  
164  
165 # this role also provides convenience methods for creating typemap objects
166 # easily:
167 sub _build_foo_typemap {
168 my $self = shift;
169  
170 $self->_create_typemap(
171 isa_entries => {
172 $class => {
173 type => 'KiokuDB::TypeMap::Entry::Callback',
174 intrinsic => 1,
175 collapse => "collapse",
176 expand => "new",
177 },
178 },
179 );
180 }
181  
182 sub _build_bar_typemap {
183 my $self = shift;
184  
185 # create a typemap with one naive isa entry
186 $self->_naive_isa_typemap("Class::Foo", @entry_args);
187 }
188  
189  
190  
191  
192  
193 # you also get some construction time customization:
194  
195 MyTypeMap->new(
196 exclude => [qw(Class::Blort foo)],
197 override => {
198 "Class::Blah", => $alternate_entry,
199 },
200 );
201  
202 =head1 DESCRIPTION
203  
204 This role provides a declarative, customizable way to set values for
205 L<KiokuDB::TypeMap>'s C<includes> attribute.
206  
207 Any class consuming this role can declare attributes with the trait
208 C<KiokuDB::TypeMap>.
209  
210 The result is a typemap instance that inherits from the specified typemap in a
211 way that is composable for the author and flexible for the user.
212  
213 L<KiokuDB::TypeMap::Default> is created using this role.
214  
215 =head1 ATTRIBUTES
216  
217 =over 4
218  
219 =item exclude
220  
221 An array reference containing typemap attribute names (e.g. C<path_class> in
222 the default typemap) or class name to exclude.
223  
224 Class exclusions are handled by C<_create_typemap> and do not apply to already
225 constructed typemaps.
226  
227 =item override
228  
229 A hash reference of classes to L<KiokuDB::TypeMap::Entry> objects.
230  
231 Class overrides are handled by C<_create_typemap> and do not apply to already
232 constructed typemaps.
233  
234 Classes which don't have a definition will not be merged into the resulting
235 typemap, simply create a typemap of your own and inherit if that's what you
236 want.
237  
238 =back
239  
240 =head1 METHODS
241  
242 =over 4
243  
244 =item _create_typemap %args
245  
246 Creates a new typemap.
247  
248 The entry arguments are converted before passing to L<KiokuDB::TypeMap/new>:
249  
250 $self->_create_typemap(
251 entries => {
252 Foo => {
253 type => "KiokuDB::TypeMap::Entry::Naive",
254 intrinsic => 1,
255 },
256 },
257 );
258  
259 The nested hashref will be used as arguments to
260 L<KiokuDB::TypeMap::Entry::Naive/new> in this example.
261  
262 C<exclude> and C<override> are taken into account by the hashref conversion
263 code.
264  
265 =item _naive_isa_typemap $class, %entry_args
266  
267 A convenience method to create a one entry typemap with a single inherited
268 entry for C<$class> of the type L<KiokuDB::TypeMap::Entry::Naive>.
269  
270 This is useful for when you have a base class that you'd like KiokuDB to
271 persist automatically:
272  
273 sub _build_my_class_typemap {
274 shift->_naive_isa_typemap( "My::Class::Base" );
275 }
276  
277 =back
278  
279 =cut
280  
« KiokuDB/Backend/Serialize/JSON.pm ↑ Summary KiokuDB/Backend/Role/TXN/Memory/Scan.pm »