File Coverage

blib/lib/Template/Lace/Factory.pm
Criterion Covered Total %
statement 45 49 91.8
branch 5 8 62.5
condition n/a
subroutine 15 16 93.7
pod 0 9 0.0
total 65 82 79.2


line stmt bran cond sub pod time code
1             package Template::Lace::Factory;
2              
3 1     1   580 use Moo;
  1         2  
  1         8  
4 1     1   336 use Module::Runtime 'use_module';
  1         2  
  1         6  
5              
6 6     6 0 22 sub DOM_CLASS { use_module 'Template::Lace::DOM' }
7 6     6 0 33 sub RENDERER_CLASS { use_module 'Template::Lace::Renderer' }
8 6     6 0 22 sub COMPONENTS_CLASS { use_module 'Template::Lace::Components' }
9              
10             has 'model_class' => (
11             is=>'ro',
12             required=>1);
13              
14             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             default=>sub { RENDERER_CLASS } );
23              
24             has 'dom' => (
25             is=>'ro',
26             required=>1);
27              
28             has 'init_args' => (
29             is=>'ro',
30             required=>1,
31             default=>sub { +{} });
32              
33             has 'components' => (
34             is=>'ro',
35             required=>1);
36              
37             around BUILDARGS => sub {
38             my ($orig, $class, @args) = @_;
39             my $args = $class->$orig(@args);
40             my $model_class = $class->_get_model_class($args);
41             my $dom_class = $class->_get_dom_class($args);
42             my $components_class = $class->_get_components_class($args);
43             my $dom = $args->{dom} = $class->_build_dom($dom_class, $model_class);
44             my $component_handlers = $args->{component_handlers};
45             $args->{components} = $class->_build_components(
46             $components_class,
47             $model_class,
48             $dom,
49             $component_handlers);
50             return $args;
51             };
52              
53             sub _get_model_class {
54 6     6   25 my ($class, $args) = @_;
55 6         38 my $model_class = use_module($args->{model_class});
56 6         323 return $model_class;
57             }
58              
59             sub _get_dom_class {
60 6     6   21 my ($class, $args) = @_;
61             my $dom_class = exists $args->{dom_class} ?
62 6 50       40 use_module($args->{dom_class}) :
63             DOM_CLASS;
64 6         223 return $dom_class;
65             }
66              
67             sub _get_components_class {
68 6     6   20 my ($class, $args) = @_;
69             my $components_class = exists $args->{components_class} ?
70 6 50       33 use_module($args->{components_class}) :
71             COMPONENTS_CLASS;
72 6         203 return $components_class;
73             }
74              
75             sub _build_dom {
76 6     6   26 my ($class, $dom_class, $model_class) = @_;
77 6         81 my $template = $model_class->template;
78 6         72 my $dom = $dom_class->new($template);
79 6 100       7398 $model_class->prepare_dom($dom) if $model_class->can('prepare_dom');
80 6         51 return $dom;
81             }
82              
83             sub _build_components {
84 6     6   27 my ($class, $components_class, $model_class, $dom, $component_handlers) = @_;
85 6         196 my $components = $components_class->new(
86             dom => $dom,
87             model_class => $model_class,
88             component_handlers => $component_handlers);
89 6         178 return $components;
90             }
91              
92             sub create {
93 7     7 0 803 my ($self, @args) = @_;
94 7         34 my %args = $self->prepare_args(@args);
95 7         37 my $dom = $self->create_dom(%args);
96 7         38 my $model = $self->create_model(%args);
97 7         46 my $renderer = $self->create_renderer(
98             $model,
99             $dom,
100             $self->components);
101 7         43 return $renderer;
102             }
103              
104             sub render {
105 0     0 0 0 my ($self, @args) = @_;
106 0         0 my $renderer = $self->create(@args);
107 0         0 my $response = $renderer->render;
108 0         0 return $response;
109             }
110              
111             sub prepare_args {
112 7     7 0 29 my ($self, @args) = @_;
113 7         18 my %args = (%{$self->init_args}, @args);
  7         55  
114 7         48 return %args;
115             }
116              
117             sub create_dom {
118 7     7 0 32 my ($self, %args) = @_;
119 7         49 my $dom = $self->dom->clone;
120 7         39 return $dom;
121             }
122              
123             sub create_model {
124 7     7 0 38 my ($self, %args) = @_;
125 7 50       109 my $model = $self->has_model_constructor ?
126             $self->model_constructor->($self->model_class, %args) :
127             $self->model_class->new(%args);
128 7         8628 return $model;
129             }
130              
131              
132             sub create_renderer {
133 7     7 0 28 my ($self, $model, $dom, $components) = @_;
134 7         177 my $renderer = $self->renderer_class->new(
135             model=>$model,
136             components=>$components,
137             dom=>$dom);
138 7         1828 return $renderer;
139             }
140              
141             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