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