Lace/Components.pm

filter: Statementstmt 93.4% 129 / 138 Branchbran 78.5% 33 / 42 Conditioncond 83.3% 5 / 6 MC/DCmcdc 66.6% 2 / 3 Subroutinesub 92.3% 24 / 26 Podpod 0.0% 0 / 15 totaltotal 83.9% 193 / 230 SCAR 33.2
CC
27
Cov
90%
find_component
6.0
attr_value_handler_factory
5.0
is_a_component
4.2
CRAP 27.8
« Lace/Renderer.pm ↑ Summary Lace/Factory/InferInitArgsRole.pm »
1 package Template::Lace::Components;
2  
3 1 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use UUID::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use JSON::MaybeXS ();
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 has [qw(handlers component_info ordered_component_keys)] => (is=>'ro', required=>1);
8  
9 around BUILDARGS => sub {
10 6 ▶ my ($orig, $class, @args) = @_;
Subroutine __ANON__: called CC=1 SCAR=0.0
11 6 my $args = $class->$orig(@args);
12 6 my %component_info = $class->get_component_info($args);
13 6 my @ordered_component_keys = $class->get_component_ordered_keys(%component_info);
14 6 my %handlers = $class->get_handlers($args, \%component_info, @ordered_component_keys);
15  
16 6 $args->{handlers} = \%handlers;
17 6 $args->{component_info} = \%component_info;
18 6 $args->{ordered_component_keys} = \@ordered_component_keys;
19  
20 6 return $args;
21 1 };
22  
23 sub get_component_info {
24 6 ▶ my ($class, $args) = @_;
Subroutine get_component_info: called CC=1 SCAR=0.0
Pod undocumented
25 6 my %component_info = $class->find_components($args->{dom}, $args->{component_handlers});
26 6 return %component_info;
27 }
28  
29 sub get_handlers {
30 6 ▶ my ($class, $args, $component_info, @ordered_component_keys) = @_;
Subroutine get_handlers: called CC=2 SCAR=6.9
Pod undocumented
31 6 my %handlers = ();
32 6 foreach my $key(@ordered_component_keys) {
33 6 my $handler = $class->get_handler($args, %{$component_info->{$key}});
34 6 $handlers{$key} = $handler;
35 }
36 6 return %handlers;
37 }
38  
39 sub get_handler {
40 6 ▶ my ($class, $args, %component_info) = @_;
Subroutine get_handler: called CC=4 SCAR=13.9
Pod undocumented
41 6 my $prefix = $component_info{prefix};
42 6 my $name = $component_info{name};
43 6 my $handler = '';
44 6 ▶ if(ref($args->{component_handlers}{$prefix}) eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $args->{'component_handlers'}{$prefix} eq 'CODE') { } 1 5 6
45 1 $handler = $args->{component_handlers}{$prefix}->($name, $args, %{$component_info{attrs}});
46 } else {
47 5 $handler = $args->{component_handlers}{$prefix}{$name};
48 5 ▶ $handler = (ref($handler) eq 'CODE') ? $handler->($args, %{$component_info{attrs}}): $handler;
Branch: 1 branch
BranchTrueFalseTotal
ref $handler eq 'CODE' ? : 0 5 5
49 }
50  
51 # TODO should this be in the renderer?
52 6 ▶ if($handler->model_class->can('on_component_add')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($handler->model_class->can("on_component_add")) 1 5 6
53 my %attrs = (
54 $handler->renderer_class
55 ->process_attrs(
56 $handler->model_class,
57 $args->{dom}, # DOM of containing template
58 1 %{$component_info{attrs}}),
59 content=>$args->{dom}->content,
60 # model=>$self->model
61 1 );
62 1 my $renderer = $handler->create(%attrs);
63 1 $renderer->model->on_component_add($renderer->dom, $args->{dom});
64  
65 1 $args->{dom}->at("[uuid='$component_info{key}']")->replace($renderer->dom);
66 }
67  
68 6 return $handler;
69 }
70  
71 sub find_components {
72 147 ▶ my ($class, $dom, $handlers, $current_container_id, %components) = @_;
Subroutine find_components: called CC=1 SCAR=0.0
Pod undocumented
73 $dom->child_nodes->each(sub {
74 135 ▶ %components = $class->find_component(@_,
Subroutine __ANON__: called CC=1 SCAR=0.0
75 $handlers,
76 $current_container_id,
77 %components);
78 147 });
79 147 return %components;
80 }
81  
82 sub find_component {
83 135 ▶ my ($class, $child_dom, $num, $handlers, $current_container_id, %components) = @_;
Subroutine find_component: called CC=6 SCAR=17.9
Pod undocumented
84 135 ▶ if(my ($prefix, $component_name) = (($child_dom->tag||'') =~m/^(.+?)\-(.+)?/)) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = ($child_dom->tag || "") =~ /^(.+?)\-(.+)?/) 13 122 135
Condition100%2/2
$child_dom->tag || ''
l!l
4986
MC/DC100%1/1
$child_dom->tag || ''
$child_dom->tag
Truth table100%2/2
A
A= $child_dom->tag
Aresult
11
00
85 ## if uuid exists, that means we already processed it.
86 13 ▶ if($class->is_a_component($handlers, $prefix, $component_name)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class->is_a_component($handlers, $prefix, $component_name)) 13 0 13
87 13 ▶ unless($child_dom->attr('uuid')) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($child_dom->attr("uuid")) 6 7 13
88 6 my $uuid = $class->generate_component_uuid($prefix);
89 6 $child_dom->attr({'uuid',$uuid});
90 6 $components{$uuid} = +{
91 order => (scalar(keys %components)),
92 key => $uuid,
93 $class->setup_component_info($prefix,
94 $current_container_id,
95 $component_name,
96 $child_dom),
97 };
98  
99 6 ▶ push @{$components{$current_container_id}{children_ids}}, $uuid
Branch: 1 branch
BranchTrueFalseTotal
if $current_container_id 5 1 6
100 if $current_container_id;
101  
102 6 my $old_current_container_id = $current_container_id;
103 6 $current_container_id = $uuid;
104
105 6 %components = $class->find_components(
106 $child_dom,
107 $handlers,
108 $current_container_id,
109 %components);
110  
111 6 $current_container_id = $old_current_container_id;
112 }
113 }
114 }
115 135 %components = $class->find_components(
116 $child_dom,
117 $handlers,
118 $current_container_id,
119 %components);
120 135 return %components;
121 }
122  
123 sub is_a_component {
124 13 ▶ my ($class, $handlers, $prefix, $name) = @_;
Subroutine is_a_component: called CC=4 SCAR=14.5
Pod undocumented
125 13 ▶ if($handlers->{$prefix}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($handlers->{$prefix}) { } 13 0 13
126 13 ▶ if(ref($handlers->{$prefix}) eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $handlers->{$prefix} eq 'CODE') { } 1 12 13
127 1 return 1;
128 } else {
129 12 ▶ return $handlers->{$prefix}{$name} ? 1:0;
Branch: 1 branch
BranchTrueFalseTotal
$handlers->{$prefix}{$name} ? : 12 0 12
130 }
131 } else {
132 0 return 0;
133 }
134 }
135  
136 sub generate_component_uuid {
137 6 ▶ my ($class, $prefix) = @_;
Subroutine generate_component_uuid: called CC=1 SCAR=0.0
Pod undocumented
138 6 my $uuid = UUID::Tiny::create_uuid_as_string;
139 6 $uuid=~s/\-//g;
140 6 return $uuid;
141 }
142  
143 sub setup_component_info {
144 6 ▶ my ($class, $prefix, $current_container_id, $name, $dom) = @_;
Subroutine setup_component_info: called CC=2 SCAR=6.9
Pod undocumented
145 6 my %attrs = $class->setup_component_attr($dom);
146 6 ▶ return prefix => $prefix,
Condition100%2/2
$current_container_id || ''
l!l
51
MC/DC100%1/1
$current_container_id || ''
$current_container_id
Truth table100%2/2
A
A= $current_container_id
Aresult
11
00
147 name => $name,
148 current_container_id => $current_container_id||'',
149 attrs => \%attrs;
150 }
151  
152 sub setup_component_attr {
153 6 ▶ my ($class, $dom) = @_;
Subroutine setup_component_attr: called CC=2 SCAR=7.0
Pod undocumented
154 return map {
155 23 $_ => $class->attr_value_handler_factory($dom->attr->{$_});
156 6 ▶ } keys %{$dom->attr||+{}};
Branch: 1 branch
BranchTrueFalseTotal
$dom->attr or {} 0 6 6
157 }
158  
159 sub attr_value_handler_factory {
160 26 ▶ my ($class, $value) = @_;
Subroutine attr_value_handler_factory: called CC=5 SCAR=16.1
Pod undocumented
161  
162  
163 26 ▶ if(my ($node, $css) = ($value=~m/^\\['"](\@?)(.+)['"]$/)) {
Branch: 4 branches
BranchTrueFalseTotal
if (() = $value =~ /^\\['"](\@?)(.+)['"]$/) { } 2 24 26
elsif (my $path = ($value =~ /^\$\.(.+)$/)[0]) { } 3 21 24
elsif ($value =~ /^\{/) { } 1 20 21
elsif ($value =~ /^\[/) { } 1 19 20
164 2 return $class->setup_css_match_handler($node, $css); # CSS match to content DOM
165 } elsif(my $path = ($value=~m/^\$\.(.+)$/)[0]) {
166 3 return $class->setup_data_path_hander($path); # is path to data
167 } elsif($value=~/^\{/) {
168 1 return $class->setup_hashrefdata_hander($value);
169 }elsif($value=~/^\[/) {
170 1 return $class->setup_arrayrefdata_hander($value);
171 } else {
172 19 return $value; # is literal or 'passthru' value
173 }
174 }
175  
176 sub setup_arrayrefdata_hander {
177 1 ▶ my ($class, $value) = @_;
Subroutine setup_arrayrefdata_hander: called CC=1 SCAR=0.0
Pod undocumented
178 1 my $ref = JSON::MaybeXS::decode_json($value);
179 my @array = map {
180 1 my $v = $_; $v =~s/^\$\.//;
181 2 $class->attr_value_handler_factory($v);
182 } @$ref;
183 return sub {
184 1 ▶ my ($ctx, $dom) = @_;
Subroutine __ANON__: called CC=3 SCAR=11.7
185 1 ▶ return [ map { (ref($_)||'') eq 'CODE' ? $_->($ctx,$dom) : $_ } @array ];
Branch: 1 branch
BranchTrueFalseTotal
(ref $_ || '') eq 'CODE' ? : 0 2 2
Condition50%1/2
ref $_ || ''
l!l
02
MC/DC0%0/1
ref $_ || ''
ref $_
Truth table50%1/2
A
A= ref $_
Aresult
11
00
186 1 };
187 }
188  
189  
190 sub setup_hashrefdata_hander {
191 1 ▶ my ($class, $value) = @_;
Subroutine setup_hashrefdata_hander: called CC=1 SCAR=0.0
Pod undocumented
192 1 my $ref = JSON::MaybeXS::decode_json($value);
193 my %hash = map {
194 1 my $v = $ref->{$_};
195 1 $_ => $class->attr_value_handler_factory($v);
196 } keys %$ref;
197 return sub {
198 1 ▶ my ($ctx, $dom) = @_;
Subroutine __ANON__: called CC=1 SCAR=0.0
199 1 my %unrolled = map { $_ => $hash{$_}->($ctx,$dom) } keys(%hash);
200 1 return \%unrolled;
201 1 };
202 }
203  
204 sub setup_css_match_handler {
205 2 ▶ my ($class, $node, $css) = @_;
Subroutine setup_css_match_handler: called CC=3 SCAR=14.2
Pod undocumented
206 2 ▶ if($node) {
Branch: 1 branch
BranchTrueFalseTotal
if ($node) { } 0 2 2
207 0 ▶ return sub { my ($view, $dom) = @_; $dom->find($css) };
Subroutine __ANON__: not called CC=1 SCAR=6.9
208 } else {
209 2 ▶ if(my $content = $css=~s/\:content$//) { # hack to CSS to allow match on content
Branch: 1 branch
BranchTrueFalseTotal
if (my $content = $css =~ s/\:content$//) { } 2 0 2
210 2 ▶ return sub { my ($view, $dom) = @_; $dom->at($css)->content };
Subroutine __ANON__: called CC=1 SCAR=0.0
211 } else {
212 0 ▶ return sub { my ($view, $dom) = @_; $dom->at($css) };
Subroutine __ANON__: not called CC=1 SCAR=6.9
213 }
214 }
215 }
216  
217 sub setup_data_path_hander {
218 3 ▶ my ($class, $path) = @_;
Subroutine setup_data_path_hander: called CC=2 SCAR=7.0
Pod undocumented
219 3 ▶ my @parts = $path=~m/\./ ? (split('\.', $path)) : ($path);
Branch: 1 branch
BranchTrueFalseTotal
$path =~ /\./ ? : 2 1 3
220 return sub {
221 3 ▶ my ($ctx, $dom) = @_;
Subroutine __ANON__: called CC=4 SCAR=14.1
222 3 foreach my $part(@parts) {
223 9 ▶ if(Scalar::Util::blessed $ctx) {
Branch: 2 branches
BranchTrueFalseTotal
if (builtin::blessed($ctx)) { } 3 6 9
elsif (ref $ctx eq 'HASH') { } 6 0 6
224 3 $ctx = $ctx->$part;
225 } elsif(ref($ctx) eq 'HASH') {
226 6 $ctx = $ctx->{$part};
227 } else {
228 0 die "No '$part' in path '$path' for this view";
229 }
230 }
231 3 return $ctx;
232 3 };
233 }
234  
235 sub get_component_ordered_keys {
236 6 ▶ my ($class, %component_info) = @_;
Subroutine get_component_ordered_keys: called CC=1 SCAR=0.0
Pod undocumented
237 return map {
238 $_->{key}
239 6 } sort {
240 $a->{order} <=> $b->{order}
241 10 } map {
242 6 $component_info{$_}
243 } keys %component_info;
244 }
245  
246 1 1;
247  
248 =head1 NAME
249  
250 Template::Lace::Components - Prepares a Component Hierarchy from a DOM
251  
252 =head1 SYNOPSIS
253  
254 TBD
255  
256 =head1 DESCRIPTION
257  
258 Use by L<Template::Lace::Factory> to create a component hierarchy for a DOM
259 and from the defined component_mappings. Not really end user bits aimed here
260 but you can subclass if you want customized component features. See the main
261 docs in L<Template::Lace> for detailed discussion of Components.
262  
263 =head1 SEE ALSO
264
265 L<Template::Lace>.
266  
267 =head1 AUTHOR
268  
269 Please See L<Template::Lace> for authorship and contributor information.
270
271 =head1 COPYRIGHT & LICENSE
272
273 Please see L<Template::Lace> for copyright and license information.
274  
275 =cut
« Lace/Renderer.pm ↑ Summary Lace/Factory/InferInitArgsRole.pm »