File Coverage

blib/lib/Catalyst/ComponentRole/InjectionHelpers.pm
Criterion Covered Total %
statement 22 28 78.5
branch 6 12 50.0
condition 4 4 100.0
subroutine 5 5 100.0
pod 3 3 100.0
total 40 52 76.9


line stmt bran cond sub pod time code
1             package Catalyst::ComponentRole::InjectionHelpers;
2              
3 2     2   1153 use Moose::Role;
  2         5  
  2         12  
4 2     2   9607 use Moose::Util;
  2         5  
  2         16  
5              
6             has _version => (is=>'ro', required=>1);
7             has application => (is=>'ro', required=>1);
8             has from => (is=>'ro', isa=>'ClassName|CodeRef', required=>1);
9             has method => (is=>'ro', required=>1, default=>'new');
10             has injected_component_name => (is=>'ro', isa=>'Str', required=>1);
11             has injection_parameters => (is=>'ro', isa=>'HashRef', required=>1);
12             has get_config => (is=>'ro', isa=>'CodeRef', required=>1, default=>sub {sub { +{} }});
13             has roles => (is=>'ro', isa=>'ArrayRef', required=>1, default=>sub { +[] });
14             has transform_args => (is=>'ro', isa=>'CodeRef', predicate=>'has_transform_args');
15             has composed_class => (
16             is=>'ro',
17             init_arg=>undef,
18             required=>1,
19             lazy=>1,
20             default=>sub { Moose::Util::with_traits($_[0]->from, @{$_[0]->roles}) });
21              
22             sub merge_args {
23 12     12 1 29 my ($self, $app_or_c, @args) = @_;
24 12         22 my %global_config_args = %{ $self->get_config->($app_or_c) };
  12         408  
25              
26             # Ok, so if @args are a hash, it just gets combined, no harm at
27             # all as long as you expect a hash. But is @args is an array,
28             # we want it FIRST, because we will assume the @args are intended to
29             # be positional.
30              
31             # Remember @args only comes from $c->model($model, @args).
32             # So here you can override global args from the call to model, and for
33             # now we just do the dumbest possible merge type.
34 12         183 return (%global_config_args, @args);
35             }
36              
37             sub transform_args_if_needed {
38 12     12 1 40 my ($self, $composed_class, $app_or_c, @merged_args) = @_;
39 12 50       445 if($self->has_transform_args) {
40 0 0       0 if($self->_version == 2) {
41 0         0 @merged_args = $self->transform_args->(@merged_args);
42             } else {
43 0         0 @merged_args = $self->transform_args->($self, $composed_class, $app_or_c, @merged_args);
44             }
45             }
46 12         38 return @merged_args;
47             }
48              
49             sub build_new_instance {
50 12     12 1 679 my ($self, $app_or_c, @args) = @_;
51 12         41 my @merged_args = $self->merge_args($app_or_c, @args);
52 12         442 my $method = $self->method;
53 12 100 100     383 my $composed_class = ref($self->from)||'' eq "CODE" ?
54             $self->from : $self->composed_class;
55              
56 12         42 @merged_args = $self->transform_args_if_needed($composed_class, $app_or_c, @merged_args);
57              
58 12 100 100     55 if((ref($method)||'') eq 'CODE') {
59 3 50       97 if($self->_version == 2) {
60 0         0 my @init_args = ($app_or_c, @merged_args);
61 0 0       0 unshift @init_args, $composed_class unless ref($composed_class);
62 0         0 return $method->(@init_args);
63             } else {
64 3         15 return $self->$method($composed_class, $app_or_c, @merged_args);
65             }
66             } else {
67 9         53 return $composed_class->$method(@merged_args);
68             }
69             }
70              
71             =head1 NAME
72              
73             Catalyst::ComponentRole::InjectionHelpers; - Common role for adaptors
74              
75             =head1 SYNOPSIS
76              
77             package MyApp::MySpecialAdaptor
78              
79             use Moose;
80             with 'Catalyst::ComponentRole::InjectionHelpers';
81              
82             sub ACCEPT_CONTEXT { ... }
83              
84             =head1 DESCRIPTION
85              
86             Common functionality and interface inforcement for injection helper adaptors.
87             You should see L<Catalyst::Plugin::InjectionHelpers> for more.
88              
89             =head1 ATTRIBUTES
90              
91             This role defines the following attributes
92              
93             =head2 application
94              
95             Your L<Catalyst> application
96              
97             =head2 from
98              
99             A class name or coderef that is being adapted to run under L<Catalyst>
100              
101             =head2 method
102              
103             The name of the method in your 'from' class that is used to create a new
104             instance OR a coderef that is used to return an instance. Defaults to 'new'.
105              
106             =head2 roles
107              
108             A list of L<Moose::Role>s to be composed into your class
109              
110             =head2 transform_args
111              
112             A coderef that you can use to transform configuration arguments into something
113             more suitable for your class. For example, the configuration args is typically
114             a hash, but your object class may require some positional arguments.
115              
116             MyApp->inject_components(
117             'Model::Foo' => {
118             from_class = 'Foo',
119             transform_args => sub {
120             my ($adaptor_instance, $coderef, $app, %args) = @_;
121             my $path = delete $args{path},
122             return ($path, %args);
123             },
124             },
125             );
126              
127             Should return the args as they as used by the initialization method of the
128             'from_class'.
129              
130             =head2 get_config
131              
132             =head2 injection_parameters
133              
134             =head2 injected_component_name
135              
136             TBD
137              
138             =head1 METHODS
139              
140             This role exposes the following public methods
141              
142             =head2 merge_args
143              
144             Responsible for merging global configuration and anything passed in at call
145             time
146              
147             =head2 transform_args_if_needed
148              
149             Perform any programmatic argument transformation
150              
151             =head2 build_new_instance
152              
153             Responsible for returning a new instance of the component.
154              
155             =head1 AUTHOR
156              
157             John Napiorkowski L<email:jjnapiork@cpan.org>
158            
159             =head1 SEE ALSO
160            
161             L<Catalyst::Plugin::InjectionHelpers>
162             L<Catalyst>, L<Catalyst::Model::InjectionHelpers::Application>,
163             L<Catalyst::Model::InjectionHelpers::Factory>, L<Catalyst::Model::InjectionHelpers::PerRequest>
164             L<Catalyst::ModelRole::InjectionHelpers>
165              
166             =head1 COPYRIGHT & LICENSE
167            
168             Copyright 2016, John Napiorkowski L<email:jjnapiork@cpan.org>
169            
170             This library is free software; you can redistribute it and/or modify it under
171             the same terms as Perl itself.
172            
173             =cut
174             1;