annotate lib/IMPL/Web/View/TTControl.pm @ 425:c27434cdd611 ref20150831

sync
author cin
date Tue, 03 Apr 2018 19:30:01 +0300
parents c6e90e02dd17
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
407
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
1 package IMPL::Web::View::TTControl;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
2 use strict;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
3
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
4 use IMPL::Const qw(:prop);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
5 use IMPL::lang qw(:hash :base);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
6 use IMPL::declare {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
7 require => {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
8 Exception => 'IMPL::Exception',
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
9 ArgException => '-IMPL::InvalidArgumentException'
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
10 },
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
11 base => [
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
12 'IMPL::Object' => undef
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
13 ],
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
14 props => [
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
15 context => PROP_RO,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
16 template => PROP_RO
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
17 ]
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
18 };
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
19
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
20 our $AUTOLOAD_REGEX = qr/^[a-z]/;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
21
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
22 sub CTOR {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
23 my ($this,$context,$template,$args) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
24
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
25 $this->context($context)
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
26 or die ArgException->new(context => 'A context is required');
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
27 $this->template($template)
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
28 or die ArgException->new(template => 'A template is required');
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
29
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
30 if (ref $args eq 'HASH') {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
31 while(my ($key, $value) = each %$args) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
32 next if grep $_ eq $key, qw(context template);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
33 $this->$key($value);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
34 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
35 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
36 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
37
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
38 sub _PopulateMethods {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
39 my ($this,@methods) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
40
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
41 $this->_stash->update({
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
42 map {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
43 my $name = $_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
44 $name,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
45 sub {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
46 $this->$name(@_);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
47 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
48 } @methods
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
49 });
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
50 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
51
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
52 sub _stash {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
53 $_[0]->context->stash;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
54 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
55
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
56 sub Render {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
57 my ($this,$args) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
58 return $this->context->include($this->template,$args);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
59 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
60
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
61 our $AUTOLOAD;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
62 sub AUTOLOAD {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
63 my ($prop) = ($AUTOLOAD =~ m/(\w+)$/);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
64
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
65 die Exception->new("Method not found: $AUTOLOAD") unless $prop=~/$AUTOLOAD_REGEX/ and $_[0];
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
66
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
67 no strict 'refs';
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
68
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
69 my $method = sub {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
70 my $that = shift;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
71 if (@_ == 0) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
72 return $that->_stash->get($prop);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
73 } elsif (@_ == 1) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
74 return $that->_stash->set($prop,shift);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
75 } else {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
76 return $that->_stash->get([$prop,[@_]]);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
77 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
78 };
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
79
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
80 *{$AUTOLOAD} = $method;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
81
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
82 goto &$method;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
83 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
84
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
85
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
86 1;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
87
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
88 __END__
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
89
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
90 =pod
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
91
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
92 =head1 NAME
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
93
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
94 C<IMPL::Web::View::TTControl> расширяет функциональность шаблонов
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
95
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
96 =head1 SYNPOSIS
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
97
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
98 =begin code
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
99
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
100 package My::View::Menu;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
101 use IMPL::declare {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
102 base => [
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
103 'IMPL::Web::View::TTControl' => '@_'
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
104 ]
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
105 };
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
106
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
107 sub Render {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
108 my ($this,$args) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
109
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
110 $this->PrepareItems($args);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
111
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
112 return $this->next::method($args);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
113 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
114
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
115 sub PrepareItems
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
116
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
117 =end code
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
118
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
119 =head1 DESCRIPTION
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
120
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
121
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
122 =cut