Board/Service/WithParameters.pm

filter: Statementstmt 100% 44 / 44 Branchbran 100% 4 / 4 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 10 / 10 Podpod 100% 3 / 3 totaltotal 100% 61 / 61 SCAR 11.0
CC
3
Cov
100%
__ANON__
2.0
check_parameters
2.0
BEGIN
1.0
CRAP 3.0
« Board/LifeCycle/Singleton.pm ↑ Summary Board/Service/Alias.pm »
1 package Bread::Board::Service::WithParameters;
2 69 our $AUTHORITY = 'cpan:STEVAN';
3 # ABSTRACT: Services with parameters
4 69 $Bread::Board::Service::WithParameters::VERSION = '0.35';
5 69 ▶use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 69 ▶use MooseX::Params::Validate qw(validated_hash);
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 69 ▶use Bread::Board::Types;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 69 with 'Bread::Board::Service';
11  
12 69 has 'parameters' => (
13 traits => [ 'Hash', 'Copy' ],
14 is => 'ro',
15 isa => 'Bread::Board::Service::Parameters',
16 lazy => 1,
17 coerce => 1,
18 builder => '_build_parameters',
19 handles => {
20 'has_parameters' => 'count'
21 }
22 );
23  
24 69 has '_parameter_keys_to_remove' => (
25 is => 'rw',
26 isa => 'ArrayRef',
27 clearer => '_clear_parameter_keys_to_remove',
28 predicate => '_has_parameter_keys_to_remove',
29 );
30  
31 before 'get' => sub {
32 238 ▶ my $self = shift;
Subroutine __ANON__: called CC=1 SCAR=0.0
33 238 my %params = $self->check_parameters(@_);
34 227 $self->_parameter_keys_to_remove( [ keys %params ] );
35 227 $self->params({ %{ $self->params }, %params });
36 69 };
37  
38 after 'get' => sub {
39 227 ▶ my $self = shift;
Subroutine __ANON__: called CC=2 SCAR=6.9
40 227 ▶ return unless $self->_has_parameter_keys_to_remove;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_has_parameter_keys_to_remove 5 222 227
41 222 map { $self->_clear_param( $_ ) } @{ $self->_parameter_keys_to_remove };
42 222 $self->_clear_parameter_keys_to_remove;
43 69 };
44  
45 109 ▶sub _build_parameters { +{} }
Subroutine _build_parameters: called CC=1 SCAR=0.0
46  
47 sub check_parameters {
48 238 ▶ my $self = shift;
Subroutine check_parameters: called CC=2 SCAR=6.9
Pod documented
49 return validated_hash(\@_, (
50 238 ▶ %{ $self->parameters },
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_parameters 58 180 238
51 # NOTE:
52 # cache the parameters in a per-service
53 # basis, this should be more than adequate
54 # since each service can only have one set
55 # of parameters at a time. If this does end
56 # up breaking then we can give it a better
57 # key at that point.
58 # - SL
59 (MX_PARAMS_VALIDATE_CACHE_KEY => Scalar::Util::refaddr($self))
60 )) if $self->has_parameters;
61 180 return ();
62 }
63  
64 sub has_required_parameters {
65 102 ▶ my $self = shift;
Subroutine has_required_parameters: called CC=1 SCAR=0.0
Pod documented
66 102 scalar grep { ! $_->{optional} } values %{ $self->parameters };
67 }
68  
69 sub has_parameter_defaults {
70 5 ▶ my $self = shift;
Subroutine has_parameter_defaults: called CC=1 SCAR=0.0
Pod documented
71 5 scalar grep { $_->{default} } values %{ $self->parameters };
72 }
73  
74 69 ▶no Moose::Role; 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
75  
76 __END__
77  
78 =pod
79  
80 =encoding UTF-8
81  
82 =head1 NAME
83  
84 Bread::Board::Service::WithParameters - Services with parameters
85  
86 =head1 VERSION
87  
88 version 0.35
89  
90 =head1 DESCRIPTION
91  
92 This is a sub-role of L<Bread::Board::Service>, for parameterized
93 services. These are services that will instantiate different values
94 depending on parameters that are passed to the C<get> method. You can
95 pass those parameters via the L<< C<service_params> attribute of
96 C<Bread::Board::Dependency>|Bread::Board::Dependency/service_params
97 >>, or via the L<< C<inflate> method of
98 C<Bread::Board::Service::Deferred::Thunk>|Bread::Board::Service::Deferred::Thunk/inflate
99 >>.
100  
101 =head1 ATTRIBUTES
102  
103 =head2 C<parameters>
104  
105 Read-only hashref, will be passed as-is to L<<
106 C<MooseX::Params::Validate>'s
107 C<validated_hash>|MooseX::Params::Validate/validated_hash >>, so you
108 can use things like C<optional> and C<default> in addition to type
109 constraints:
110  
111 service something => (
112 class => 'Thing',
113 parameters => {
114 type => { isa => 'Str', default => 'text' },
115 },
116 );
117  
118 This attribute uses coercions on L<<
119 C<Bread::Board::Service::Parameters>|Bread::Board::Types/Bread::Board::Service::Parameters
120 >> so that you can also say:
121  
122 service something => (
123 class => 'Thing',
124 parameters => ['type'],
125 );
126  
127 and it will be equivalent to:
128  
129 service something => (
130 class => 'Thing',
131 parameters => {
132 type => { optional => 0 },
133 },
134 );
135  
136 =head1 METHODS
137  
138 =head2 C<has_parameters>
139  
140 Predicate for the L</parameters> attribute.
141  
142 =head2 C<has_parameter_defaults>
143  
144 Returns true if any of the L</parameters> have a C<default> value.
145  
146 =head2 C<has_required_parameters>
147  
148 Returns true if any of the L</parameters> does I<not> have C<optional>
149 set to true.
150  
151 =head2 C<check_parameters>
152  
153 my %parameters = $service->check_parameters(name1=>$value1,name2=>$value2);
154 my %parameters = $service->check_parameters({name1=>$value1,name2=>$value2});
155  
156 If any L</parameters> are defined, this function validates its
157 arguments against the parameters' definitions (using
158 L<MooseX::Params::Validate>). It will die if the validation fails, or
159 return the validated parameters (including default value) if it
160 succeeds.
161  
162 =head2 C<get>
163  
164 I<Before> the C<get> method, arguments to C<get> are passed through
165 L</check_parameters> and added to the L<<
166 C<params>|Bread::Board::Service/params >> hashref. I<After> the C<get>
167 method, those keys/values will be removed. In practice, this makes all
168 parameters available to the actual C<get> method body.
169  
170 =head1 AUTHOR
171  
172 Stevan Little <stevan@iinteractive.com>
173  
174 =head1 BUGS
175  
176 Please report any bugs or feature requests on the bugtracker website
177 https://github.com/stevan/BreadBoard/issues
178  
179 When submitting a bug or request, please include a test-file or a
180 patch to an existing test-file that illustrates the bug or desired
181 feature.
182  
183 =head1 COPYRIGHT AND LICENSE
184  
185 This software is copyright (c) 2017, 2016, 2015, 2014, 2013, 2011, 2009 by Infinity Interactive.
186  
187 This is free software; you can redistribute it and/or modify it under
188 the same terms as the Perl 5 programming language system itself.
189  
190 =cut
« Board/LifeCycle/Singleton.pm ↑ Summary Board/Service/Alias.pm »