Lace/Factory.pm

filter: Statementstmt 94.1% 64 / 68 Branchbran 62.5% 5 / 8 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 94.7% 18 / 19 Podpod 0.0% 0 / 9 totaltotal 83.6% 87 / 104 SCAR 16.1
CC
5
Cov
91%
_get_dom_class
2.0
_get_components_class
2.0
create_model
2.0
CRAP 5.0
« Lace/Factory/InferInitArgsRole.pm ↑ Summary Lace/Model/AutoTemplate.pm »
1 package Template::Lace::Factory;
2  
3 1 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use Module::Runtime 'use_module';
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 6 ▶sub DOM_CLASS { use_module 'Template::Lace::DOM' }
Subroutine DOM_CLASS: called CC=1 SCAR=0.0
Pod undocumented
7 6 ▶sub RENDERER_CLASS { use_module 'Template::Lace::Renderer' }
Subroutine RENDERER_CLASS: called CC=1 SCAR=0.0
Pod undocumented
8 6 ▶sub COMPONENTS_CLASS { use_module 'Template::Lace::Components' }
Subroutine COMPONENTS_CLASS: called CC=1 SCAR=0.0
Pod undocumented
9  
10 1 has 'model_class' => (
11 is=>'ro',
12 required=>1);
13  
14 1 has 'model_constructor' => (
15 is=>'ro',
16 required=>0,
17 predicate=>'has_model_constructor');
18  
19 has 'renderer_class' => (
20 is=>'ro',
21 required=>1,
22 6 ▶ default=>sub { RENDERER_CLASS } );
Subroutine __ANON__: called CC=1 SCAR=0.0
23  
24 1 has 'dom' => (
25 is=>'ro',
26 required=>1);
27  
28 has 'init_args' => (
29 is=>'ro',
30 required=>1,
31 6 ▶ default=>sub { +{} });
Subroutine __ANON__: called CC=1 SCAR=0.0
32  
33 1 has 'components' => (
34 is=>'ro',
35 required=>1);
36  
37 around BUILDARGS => sub {
38 6 ▶ my ($orig, $class, @args) = @_;
Subroutine __ANON__: called CC=1 SCAR=0.0
39 6 my $args = $class->$orig(@args);
40 6 my $model_class = $class->_get_model_class($args);
41 6 my $dom_class = $class->_get_dom_class($args);
42 6 my $components_class = $class->_get_components_class($args);
43 6 my $dom = $args->{dom} = $class->_build_dom($dom_class, $model_class);
44 6 my $component_handlers = $args->{component_handlers};
45 6 $args->{components} = $class->_build_components(
46 $components_class,
47 $model_class,
48 $dom,
49 $component_handlers);
50 6 return $args;
51 1 };
52  
53 sub _get_model_class {
54 6 ▶ my ($class, $args) = @_;
Subroutine _get_model_class: called CC=1 SCAR=0.0
55 6 my $model_class = use_module($args->{model_class});
56 6 return $model_class;
57 }
58  
59 sub _get_dom_class {
60 6 ▶ my ($class, $args) = @_;
Subroutine _get_dom_class: called CC=2 SCAR=7.1
61 my $dom_class = exists $args->{dom_class} ?
62 6 ▶ use_module($args->{dom_class}) :
Branch: 1 branch
BranchTrueFalseTotal
exists $args->{'dom_class'} ? : 0 6 6
63 DOM_CLASS;
64 6 return $dom_class;
65 }
66  
67 sub _get_components_class {
68 6 ▶ my ($class, $args) = @_;
Subroutine _get_components_class: called CC=2 SCAR=7.1
69 my $components_class = exists $args->{components_class} ?
70 6 ▶ use_module($args->{components_class}) :
Branch: 1 branch
BranchTrueFalseTotal
exists $args->{'components_class'} ? : 0 6 6
71 COMPONENTS_CLASS;
72 6 return $components_class;
73 }
74  
75 sub _build_dom {
76 6 ▶ my ($class, $dom_class, $model_class) = @_;
Subroutine _build_dom: called CC=2 SCAR=6.9
77 6 my $template = $model_class->template;
78 6 my $dom = $dom_class->new($template);
79 6 ▶ $model_class->prepare_dom($dom) if $model_class->can('prepare_dom');
Branch: 1 branch
BranchTrueFalseTotal
if $model_class->can("prepare_dom") 2 4 6
80 6 return $dom;
81 }
82  
83 sub _build_components {
84 6 ▶ my ($class, $components_class, $model_class, $dom, $component_handlers) = @_;
Subroutine _build_components: called CC=1 SCAR=0.0
85 6 my $components = $components_class->new(
86 dom => $dom,
87 model_class => $model_class,
88 component_handlers => $component_handlers);
89 6 return $components;
90 }
91  
92 sub create {
93 7 ▶ my ($self, @args) = @_;
Subroutine create: called CC=1 SCAR=0.0
Pod undocumented
94 7 my %args = $self->prepare_args(@args);
95 7 my $dom = $self->create_dom(%args);
96 7 my $model = $self->create_model(%args);
97 7 my $renderer = $self->create_renderer(
98 $model,
99 $dom,
100 $self->components);
101 7 return $renderer;
102 }
103  
104 sub render {
105 0 ▶ my ($self, @args) = @_;
Subroutine render: not called CC=1 SCAR=6.9
Pod undocumented
106 0 my $renderer = $self->create(@args);
107 0 my $response = $renderer->render;
108 0 return $response;
109 }
110  
111 sub prepare_args {
112 7 ▶ my ($self, @args) = @_;
Subroutine prepare_args: called CC=1 SCAR=0.0
Pod undocumented
113 7 my %args = (%{$self->init_args}, @args);
114 7 return %args;
115 }
116  
117 sub create_dom {
118 7 ▶ my ($self, %args) = @_;
Subroutine create_dom: called CC=1 SCAR=0.0
Pod undocumented
119 7 my $dom = $self->dom->clone;
120 7 return $dom;
121 }
122  
123 sub create_model {
124 7 ▶ my ($self, %args) = @_;
Subroutine create_model: called CC=2 SCAR=7.1
Pod undocumented
125 7 ▶ my $model = $self->has_model_constructor ?
Branch: 1 branch
BranchTrueFalseTotal
$self->has_model_constructor ? : 0 7 7
126 $self->model_constructor->($self->model_class, %args) :
127 $self->model_class->new(%args);
128 7 return $model;
129 }
130  
131  
132 sub create_renderer {
133 7 ▶ my ($self, $model, $dom, $components) = @_;
Subroutine create_renderer: called CC=1 SCAR=0.0
Pod undocumented
134 7 my $renderer = $self->renderer_class->new(
135 model=>$model,
136 components=>$components,
137 dom=>$dom);
138 7 return $renderer;
139 }
140  
141 1 1;
142  
143 =head1 NAME
144  
145 Template::Lace::Factory - Create templates
146  
147 =head1 SYNOPSIS
148  
149 TBD
150  
151 =head1 DESCRIPTION
152  
153 Produces Templates from a model.
154  
155 =head1 INITIALIZATION ARGUMENTS
156  
157 This class defines the following initialization arguments
158  
159 =head2 model_class
160  
161 The class that is providing the view model and does the interface
162 defined by L<Template::Lace::ModelRole>
163  
164 =head2 model_constructor
165  
166 An optional codereference that allows you to specify how the C<model_class>
167 it turned into an instance. By default we call a method called C<new>
168 on the C<model_class>. If you have special needs in creating the model
169 you can define this coderef which gets the C<model_class> and initialization
170 arguments and should return an instance. For example:
171  
172 model_constructor => sub {
173 my ($model_class, %args) = @_:
174 return $model_class->new( version=>1, %args);
175 },
176  
177 =head2 renderer_class
178  
179 The name of the Render class. useful if you need to subclass to provide
180 special feeatures.
181  
182 =head2 init_args
183  
184 A hashref of arguments that are fixed and are always passed to the model
185 constructor at create time. Useful if your model class has some attributes
186 which only need to be defined once.
187  
188 =head2 component_mappings
189  
190 A Hashref of component information. For now see the core documentation at
191 L<Template::Lace> for details about components.
192  
193 =head1 SEE ALSO
194
195 L<Template::Lace>.
196  
197 =head1 AUTHOR
198  
199 Please See L<Template::Lace> for authorship and contributor information.
200
201 =head1 COPYRIGHT & LICENSE
202
203 Please see L<Template::Lace> for copyright and license information.
204  
205 =cut
« Lace/Factory/InferInitArgsRole.pm ↑ Summary Lace/Model/AutoTemplate.pm »