Bolts/Meta/Attribute/Trait/Initializer.pm

filter: Statementstmt 80.4% 37 / 46 Branchbran 70.0% 7 / 10 Conditioncond 50.0% 2 / 4 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 87.5% 7 / 8 Podpod n/a totaltotal 75.7% 53 / 70 SCAR 23.1
CC
9
Cov
77%
__ANON__
12.0
__ANON__
6.6
__ANON__
2.0
CRAP 10.0
« Bolts/Bag.pm ↑ Summary Bolts/Injector.pm »
1 package Bolts::Meta::Attribute::Traits::Initializer;
2 10 $Bolts::Meta::Attribute::Traits::Initializer::VERSION = '0.143170';
3 # ABSTRACT: Build an attribute with an initializer
4  
5 10 ▶use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 10 ▶use Safe::Isa;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 10 Moose::Util::meta_attribute_alias('Bolts::Initializer');
9  
10  
11 # TODO Make this into a helper class so that other kinds of init can be added
12 # and customized later.
13 10 ▶use Moose::Util::TypeConstraints;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 10 has init_type => (
15 is => 'ro',
16 isa => enum([qw( Array Scalar )]),
17 required => 1,
18 default => 'Scalar',
19 );
20 10 ▶no Moose::Util::TypeConstraints;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22  
23 10 has special_initializer => (
24 is => 'ro',
25 isa => 'Str',
26 required => 1,
27 );
28  
29 10 has _original_default => (
30 is => 'rw',
31 predicate => '_has_original_default',
32 );
33  
34 before install_accessors => sub {
35 48 ▶ my $self = shift;
Subroutine __ANON__: called CC=2 SCAR=6.9
36 48 my $meta = $self->associated_class;
37  
38 48 ▶ $meta->add_attribute($self->special_initializer => (
Branch: 1 branch
BranchTrueFalseTotal
$self->_has_original_default ? : 10 38 48
39 is => 'ro',
40 required => $self->is_required,
41 init_arg => $self->name,
42 ($self->_has_original_default ? (
43 default => $self->_original_default
44 ) : ()),
45 ));
46 10 };
47  
48 before _process_options => sub {
49 48 ▶ my ($self, $name, $options) = @_;
Subroutine __ANON__: called CC=5 SCAR=18.9
50  
51 # Having these here is probably a sign that we're doing this wrong.
52 # Should probably just have the default call some predefined subroutine
53 # instead and skip these bits here.
54 48 ▶ $options->{special_initializer} //= '_' . $name . '_initializer';
Condition50%1/2
$options->{"special_initializer"} //= '_' . $name . '_initializer'
l!l
048
MC/DC0%0/1
$options->{"special_initializer"} //= '_' . $name . '_initializer'
$options->{"special_initializer"}
Truth table50%1/2
A
A= $options->{"special_initializer"}
Aresult
11
00
55 48 ▶ $options->{init_type} //= 'Scalar';
Condition50%1/2
$options->{"init_type"} //= "Scalar"
l!l
048
MC/DC0%0/1
$options->{"init_type"} //= "Scalar"
$options->{"init_type"}
Truth table50%1/2
A
A= $options->{"init_type"}
Aresult
11
00
56  
57 48 my $_initializer = $options->{special_initializer};
58  
59 $options->{_original_default} = delete $options->{default}
60 48 ▶ if exists $options->{default};
Branch: 1 branch
BranchTrueFalseTotal
if exists $options->{"default"} 10 38 48
61  
62 48 $options->{init_arg} = undef;
63 48 $options->{lazy} = 1;
64  
65 48 ▶ if ($options->{init_type} eq 'Scalar') {
Branch: 1 branch
BranchTrueFalseTotal
if ($options->{'init_type'} eq 'Scalar') { } 48 0 48
66 $options->{default} = sub {
67 376 ▶ my $self = shift;
Subroutine __ANON__: called CC=2 SCAR=6.9
68  
69 376 my $init = $self->$_initializer;
70 376 ▶ if ($init->$_isa('Bolts::Meta::Initializer')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($init->$Bolts::Meta::Attribute::Traits::Initializer::_isa('Bolts::Meta::Initializer')) { } 2 374 376
71 2 return $self->initialize_value($init->get);
72 }
73 else {
74 374 return $init;
75 }
76 48 };
77 }
78 else {
79 $options->{default} = sub {
80 0 ▶ my $self = shift;
Subroutine __ANON__: not called CC=3 SCAR=24.8
81  
82 0 my @values;
83 0 my $init_array = $self->$_initializer;
84 0 for my $init (@$init_array) {
85 0 ▶ if ($init->$_isa('Bolts::Meta::Initializer')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($init->$Bolts::Meta::Attribute::Traits::Initializer::_isa('Bolts::Meta::Initializer')) { } 0 0 0
86 0 push @values, $self->initialize_value($init->get);
87 }
88 else {
89 0 push @values, $init;
90 }
91 }
92  
93 0 return \@values;
94 0 };
95 }
96 10 };
97  
98 10 1;
99  
100 __END__
101  
102 =pod
103  
104 =encoding UTF-8
105  
106 =head1 NAME
107  
108 Bolts::Meta::Attribute::Traits::Initializer - Build an attribute with an initializer
109  
110 =head1 VERSION
111  
112 version 0.143170
113  
114 =head1 DESCRIPTION
115  
116 Sometimes it can be handy to partially break inversion of control to allow an object some control over it's own destiny. This attribute, given the short alias L<Bolts::Initializer>, can help you do that.
117  
118 See L<Bolts::Role::Initializer> for details and a synopsis.
119  
120 =head1 ATTRIBUTES
121  
122 =head2 init_type
123  
124 This is the type of initialization to perform on the intializer. It may be set to either "Array" or "Scalar" and defaults to "Scalar".
125  
126 =over
127  
128 =item Scalar
129  
130 The initializer is given as a single value. Either the actual value to be passed through or a L<Bolts::Meta::Initializer> object.
131  
132 =item Array
133  
134 The initializer is given as an array reference of values. Each element of the array may be a L<Bolts::Meta::Initializer> object or a real object to place in the array as is.
135  
136 =back
137  
138 =head2 special_initializer
139  
140 This is the name of the secondary attribute to use as the hidden initializer attribute. It defaults to C<<"_${name}_initializer">>, where C<<${name}>> is the name of this attribute.
141  
142 =head1 AUTHOR
143  
144 Andrew Sterling Hanenkamp <hanenkamp@cpan.org>
145  
146 =head1 COPYRIGHT AND LICENSE
147  
148 This software is copyright (c) 2014 by Qubling Software LLC.
149  
150 This is free software; you can redistribute it and/or modify it under
151 the same terms as the Perl 5 programming language system itself.
152  
153 =cut
« Bolts/Bag.pm ↑ Summary Bolts/Injector.pm »