comparison Lib/IMPL/Web/View/TTContext.pm @ 371:d5c8b955bf8d

refactoring
author cin
date Fri, 13 Dec 2013 16:49:47 +0400
parents cbf4febf0930
children 1eca08048ba9
comparison
equal deleted inserted replaced
370:cbf4febf0930 371:d5c8b955bf8d
12 require => { 12 require => {
13 Document => '-Template::Document', 13 Document => '-Template::Document',
14 TypeKeyedCollection => 'IMPL::TypeKeyedCollection', 14 TypeKeyedCollection => 'IMPL::TypeKeyedCollection',
15 ArgException => '-IMPL::InvalidArgumentException', 15 ArgException => '-IMPL::InvalidArgumentException',
16 Resources => 'IMPL::Resources', 16 Resources => 'IMPL::Resources',
17 Loader => 'IMPL::Code::Loader' 17 Loader => 'IMPL::Code::Loader',
18 MetadataBase => '-IMPL::Web::View::Metadata::BaseMeta',
19 Metadata => 'IMPL::Web::View::Metadata::ObjectMeta'
18 }, 20 },
19 base => [ 21 base => [
20 'Template::Context' => '@_' 22 'Template::Context' => '@_'
21 ] 23 ]
22 }; 24 };
152 } 154 }
153 155
154 my $prefix = $this->prefix; 156 my $prefix = $this->prefix;
155 157
156 my $info; 158 my $info;
157 my $meta = $this->resolve_model($path,$args); 159 my $meta = $this->resolve_model($path,$args)
158 160 or return "[not found '$path']";
159 $info->{prefix} = $prefix ? $prefix . '.' . $path : $path; 161
162 $info->{prefix} = join('.', grep($_, $prefix, $path));
160 $info->{model} = $meta->model; 163 $info->{model} = $meta->model;
161 $info->{metadata} = $meta; 164 $info->{metadata} = $meta;
162 165
163 $template ||= $info->{template}; 166 $template ||= $info->{template};
164 $template = $template ? $this->find_template($template) : $this->find_template_for($info->{model}); 167 $template = $template ? $this->find_template($template) : $this->find_template_for($info->{metadata});
165 168
166 return $this->render( 169 return $this->render(
167 $template, 170 $template,
168 hashApply( 171 hashApply(
169 $info, 172 $info,
182 } else { 185 } else {
183 $template = shift; 186 $template = shift;
184 $args = shift; 187 $args = shift;
185 } 188 }
186 189
187 $args ||= {}; 190 #copy
188 191 $args = { %{$args || {}} };
189 my $prefix = delete $args->{prefix} || $this->prefix; 192
190 193 $args->{prefix} = join('.',grep($_,$this->prefix,$args->{path}))
191 if (my $rel = delete $args->{rel}) { 194 unless defined $args->{prefix};
192 $prefix = $prefix ? "${prefix}.${rel}" : $rel; 195
196 if (is($model,MetadataBase)) {
197 $args->{model} = $model->model;
198 $args->{metadata} = $model;
199 } else {
200 $args->{model} = $model;
201 $args->{metadata} = Metadata->GetMetadataForModel($model);
193 } 202 }
194 203
195 $template = $template ? $this->find_template($template) : $this->find_template_for($model); 204 $template = $template ? $this->find_template($template) : $this->find_template_for($args->{metadata});
196 205
197 return $this->render( 206 return $this->render(
198 $template, 207 $template,
199 hashApply( 208 $args
200 {
201 prefix => $prefix,
202 model => $model,
203 },
204 $args
205 )
206 ); 209 );
207 } 210 }
208 211
209 # обеспечивает необходимый уровень изоляции между контекстами 212 # обеспечивает необходимый уровень изоляции между контекстами
210 # $code - код, который нужно выполнить в новом контексте 213 # $code - код, который нужно выполнить в новом контексте
323 326
324 die ArgException->new(prefix => "the prefix must be specified") 327 die ArgException->new(prefix => "the prefix must be specified")
325 unless defined $prefix; 328 unless defined $prefix;
326 329
327 my $meta = $this->metadata; 330 my $meta = $this->metadata;
331 unless($meta) {
332 $meta = Metadata->GetMetadataForModel($this->model);
333 $this->metadata($meta);
334 }
335
328 foreach my $part (grep length($_), split(/\.|\[(\d+)\]/, $prefix)) { 336 foreach my $part (grep length($_), split(/\.|\[(\d+)\]/, $prefix)) {
329 last unless $meta; 337 last unless $meta;
330 if ($part =~ /^\d+$/) { 338 if ($part =~ /^\d+$/) {
331 $meta = $meta->GetItem($part); 339 $meta = $meta->GetItem($part);
332 } else { 340 } else {
337 return $meta; 345 return $meta;
338 } 346 }
339 347
340 sub find_template_for { 348 sub find_template_for {
341 my ($this,$meta, $nothrow) = @_; 349 my ($this,$meta, $nothrow) = @_;
350
351 die ArgException->new(meta => 'An invalid metadata is supplied')
352 unless is($meta,MetadataBase);
342 353
343 return $this->find_template($meta->template) 354 return $this->find_template($meta->template)
344 if ($meta->template); 355 if ($meta->template);
345 356
346 my $type = $meta->modelType; 357 my $type = $meta->modelType;