Mercurial > pub > Impl
annotate Lib/IMPL/Web/Application/CustomResource.pm @ 332:04a093f0a5a6
IMPL::Web::Application refactoring: resources are created per client request
author | cin |
---|---|
date | Sun, 09 Jun 2013 21:48:57 +0400 |
parents | fe725fad2d90 |
children | 71221d79e6b4 |
rev | line source |
---|---|
229
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
1 package IMPL::Web::Application::CustomResource; |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
2 use strict; |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
3 |
230 | 4 use IMPL::Const qw(:prop); |
229
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
5 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
6 use IMPL::declare { |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
7 require => { |
248 | 8 Factory => 'IMPL::Object::Factory', |
229
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
9 CustomResourceContract => 'IMPL::Web::Application::CustomResourceContract' |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
10 }, |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
11 base => [ |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
12 'IMPL::Web::Application::Resource' => '@_' |
330 | 13 ], |
14 props => [ | |
15 accessCheck => PROP_RW | |
229
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
16 ] |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
17 }; |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
18 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
19 __PACKAGE__->static_accessor(contractFactory => CustomResourceContract ); |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
20 __PACKAGE__->static_accessor_own(_contractInstance => undef); |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
21 |
330 | 22 sub CTOR { |
23 my ($this,%args) = @_; | |
24 | |
25 $this->accessCheck($args{accessCheck}) | |
26 if $args{accessCheck}; | |
27 } | |
28 | |
229
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
29 sub contractInstance { |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
30 my ($self) = @_; |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
31 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
32 $self = ref $self || $self; |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
33 $self->_contractInstance ? $self->_contractInstance : $self->InitContract(); |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
34 } |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
35 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
36 sub InitContract { |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
37 my ($self) = @_; |
230 | 38 $self->_contractInstance( |
39 $self->contractFactory->new( | |
40 resourceFactory => $self, | |
41 resources => [ $self->GetChildResources() ] | |
42 ) | |
43 ); | |
229
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
44 } |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
45 |
248 | 46 sub CreateContract { |
47 my ($self, %args) = @_; | |
48 | |
49 $self->contractFactory->new( | |
50 resourceFactory => Factory->new( | |
51 $self, | |
52 \%args | |
53 ), | |
54 resources => [ $self->GetChildResources() ] | |
55 ) | |
56 } | |
57 | |
295 | 58 sub CreateResource { |
59 my ($self,%args) = @_; | |
60 | |
61 $self->CreateContract()->CreateResource(%args); | |
62 } | |
63 | |
330 | 64 sub AccessCheck { |
65 my ($this,$verb) = @_; | |
66 | |
67 my $handler = $this->accessCheck; | |
68 | |
69 if(ref($handler) eq 'CODE') { | |
70 &$handler($this,$verb); | |
71 } | |
72 } | |
73 | |
229
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
74 sub GetChildResources { |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
75 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
76 } |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
77 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
78 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
79 1; |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
80 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
81 __END__ |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
82 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
83 =pod |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
84 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
85 =head1 NAME |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
86 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
87 C<IMPL::Web::Application::CustomResource> - базовый класс для ресурсов, |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
88 реальзуемых в коде. |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
89 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
90 =head1 SYNOPSIS |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
91 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
92 =begin code |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
93 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
94 package MyApp::Web::Resources::ProfileResource; |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
95 use IMPL::declare { |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
96 base => [ |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
97 'IMPL::Web::Application::CustomResource' => '@_' |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
98 ] |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
99 } |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
100 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
101 sub HttpGet { |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
102 my ($this) = @_; |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
103 return $this->model; |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
104 } |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
105 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
106 sub HttpPut { |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
107 my ($this,$action) = @_; |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
108 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
109 my $form = MyApp::Web::Schema::UpdateUser->new(); |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
110 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
111 $this->model->update( $form->Bind($action) ); |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
112 } |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
113 |
332
04a093f0a5a6
IMPL::Web::Application refactoring: resources are created per client request
cin
parents:
330
diff
changeset
|
114 our %COMPONENTS = ( |
04a093f0a5a6
IMPL::Web::Application refactoring: resources are created per client request
cin
parents:
330
diff
changeset
|
115 item => { |
04a093f0a5a6
IMPL::Web::Application refactoring: resources are created per client request
cin
parents:
330
diff
changeset
|
116 verbs => { |
04a093f0a5a6
IMPL::Web::Application refactoring: resources are created per client request
cin
parents:
330
diff
changeset
|
117 get => sub { |
04a093f0a5a6
IMPL::Web::Application refactoring: resources are created per client request
cin
parents:
330
diff
changeset
|
118 shift->model; |
04a093f0a5a6
IMPL::Web::Application refactoring: resources are created per client request
cin
parents:
330
diff
changeset
|
119 } |
04a093f0a5a6
IMPL::Web::Application refactoring: resources are created per client request
cin
parents:
330
diff
changeset
|
120 }, |
04a093f0a5a6
IMPL::Web::Application refactoring: resources are created per client request
cin
parents:
330
diff
changeset
|
121 resources => [ |
04a093f0a5a6
IMPL::Web::Application refactoring: resources are created per client request
cin
parents:
330
diff
changeset
|
122 edit => |
04a093f0a5a6
IMPL::Web::Application refactoring: resources are created per client request
cin
parents:
330
diff
changeset
|
123 ] |
04a093f0a5a6
IMPL::Web::Application refactoring: resources are created per client request
cin
parents:
330
diff
changeset
|
124 } |
04a093f0a5a6
IMPL::Web::Application refactoring: resources are created per client request
cin
parents:
330
diff
changeset
|
125 ); |
04a093f0a5a6
IMPL::Web::Application refactoring: resources are created per client request
cin
parents:
330
diff
changeset
|
126 |
229
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
127 =end code |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
128 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
129 =head1 MEMBERS |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
130 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
131 =head2 C<[static]contractFactory> |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
132 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
133 Фабрика, используемая для получения контракта ресурса. По умолчанию |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
134 C<IMPL::Web::Application::CustomResourceContract>. |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
135 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
136 =head2 C<[static]contractInstance> |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
137 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
138 Экземпляр контракта для ресурса. Создается при первом обращении при помощи |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
139 метода C<InitContract()>. |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
140 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
141 =head2 C<[static]InitContract()> |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
142 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
143 Создает новый экземпляр контракта, используя фабрику из свойства C<contractFactory>. |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
144 |
248 | 145 =head2 C<[static]CreateContract(%args)> |
146 | |
147 Создает новый контракт, который при создании ресурсов будет передавать им в | |
148 конструкторе параметры C<%args>. Реализуется при помощи C<IMPL::Object::Factory> | |
149 которой задается параметр ссылка на C<%args>, т.о. при создании ресурса, ему в | |
150 конструкторе будет передан список из ключей и значений хеша C<%args>, а затем | |
151 остальные аргументы. | |
152 | |
295 | 153 =head2 C<[static]CreateResource(%args)> |
154 | |
155 Создает контракт по-умолчанию и вызывает у него метод C<CreateResource(%args)>. | |
156 | |
229
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
157 =head2 C<[static]GetChildResources()> |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
158 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
159 Статический метод, который должны переопределять новые классы ресурсов, у |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
160 которых есть дочерние ресурсы. |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
161 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
162 =begin code |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
163 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
164 package MyApp::Web::MyResource |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
165 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
166 sub GetChildResources { |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
167 my $self = shift; |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
168 return |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
169 $self->SUPER::GetChildResources(), |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
170 { |
230 | 171 name => 'info', |
172 contract => $contractInfo | |
229
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
173 }; |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
174 } |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
175 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
176 =end code |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
177 |
230 | 178 Метод возвращает список из хешей, которые будут переданы в качестве параметра |
179 C<resources> контракту данного ресурса. | |
229
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
180 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
181 =cut |