Mercurial > pub > Impl
annotate Lib/IMPL/Web/Application/CustomResource.pm @ 334:71221d79e6b4
removing web resources contracts
author | cin |
---|---|
date | Thu, 13 Jun 2013 02:24:57 +0400 |
parents | 04a093f0a5a6 |
children | e8be9062ecf2 |
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 => { |
334 | 8 NotAllowedException => 'IMPL::Web::NotAllowedException', |
9 HttpResponse => 'IMPL::Web::HttpResponse' | |
229
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 |
334 | 19 our %RESOURCE_BINDINGS = ( |
20 GET => 'HttpGet', | |
21 POST => 'HttpPost', | |
22 PUT => 'HttpPut', | |
23 DELETE => 'HttpDelete', | |
24 HEAD => 'HttpHead' | |
25 ); | |
26 | |
27 __PACKAGE__->static_accessor(_rxResourcesMap => undef, 'own'); | |
28 __PACKAGE__->static_accessor(_nameResourcesMap => undef, 'own'); | |
29 | |
30 sub namedResources { | |
31 shift->_nameResourcesMap; | |
32 } | |
33 | |
34 sub regexResources { | |
35 shift->_rxResourcesMap; | |
36 } | |
229
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
37 |
330 | 38 sub CTOR { |
39 my ($this,%args) = @_; | |
40 | |
41 $this->accessCheck($args{accessCheck}) | |
42 if $args{accessCheck}; | |
334 | 43 |
44 $this->verbs->{options} ||= \&_HttpOptionsBinding; | |
45 | |
46 while(my ($verb,$methodName) = each %RESOURCE_BINDINGS) { | |
47 $this->verbs->{lc($verb)} ||= sub { | |
48 my ($resource,$action) = @_; | |
49 | |
50 if (eval { $resource->can($methodName) }) { | |
51 return $resource->$methodName($action); | |
52 } else { | |
53 die NotAllowedException->new(allow => join(',', _GetAllowedHttpMethods($resource))); | |
54 } | |
55 } | |
56 } | |
330 | 57 } |
58 | |
334 | 59 sub FindChildResourceInfo { |
60 my ( $this, $name ) = @_; | |
229
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
61 |
334 | 62 $this->_PrepareResourcesCache() |
63 unless($this->_nameResourcesMap); | |
64 | |
65 return $this->next::method($name); | |
229
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
66 } |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
67 |
334 | 68 sub PrepareResourcesCache { |
69 # suppress default caching mechanisn | |
229
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
70 } |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
71 |
334 | 72 sub _PrepareResourcesCache { |
73 # a little bit wired | |
74 my ($self) = @_; | |
75 my %nameMap; | |
76 my @rxMap; | |
248 | 77 |
334 | 78 foreach my $res ($self->GetChildResources()) { |
79 #skip resources without contract | |
80 next unless $res->{contract}; | |
81 | |
82 if ( my $name = $res->{name} ) { | |
83 $nameMap{$name} = $res; | |
84 } | |
85 if ( $res->{match} ) { | |
86 push @rxMap,$res; | |
87 } | |
88 } | |
89 | |
90 $self->_rxResourcesMap(\@rxMap); | |
91 $self->_nameResourcesMap(\%nameMap); | |
295 | 92 } |
93 | |
330 | 94 sub AccessCheck { |
95 my ($this,$verb) = @_; | |
96 | |
97 my $handler = $this->accessCheck; | |
98 | |
99 if(ref($handler) eq 'CODE') { | |
334 | 100 return &$handler($this,$verb); |
330 | 101 } |
102 } | |
103 | |
229
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
104 sub GetChildResources { |
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 } |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
107 |
334 | 108 sub _HttpOptionsBinding { |
109 my ($this) = @_; | |
110 | |
111 my @allow = $this->_GetAllowedHttpMethods(); | |
112 return HttpResponse->new( | |
113 status => '200 OK', | |
114 headers => { | |
115 allow => join ( ',', @allow ) | |
116 } | |
117 ); | |
118 } | |
119 | |
120 sub _GetAllowedHttpMethods { | |
121 my ($this) = @_; | |
122 return grep $this->can($RESOURCE_BINDINGS{$_}), keys %RESOURCE_BINDINGS; | |
123 } | |
124 | |
229
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
125 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
126 1; |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
127 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
128 __END__ |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
129 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
130 =pod |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
131 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
132 =head1 NAME |
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::CustomResource> - базовый класс для ресурсов, |
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 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
137 =head1 SYNOPSIS |
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 =begin code |
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 package MyApp::Web::Resources::ProfileResource; |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
142 use IMPL::declare { |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
143 base => [ |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
144 'IMPL::Web::Application::CustomResource' => '@_' |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
145 ] |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
146 } |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
147 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
148 sub HttpGet { |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
149 my ($this) = @_; |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
150 return $this->model; |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
151 } |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
152 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
153 sub HttpPut { |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
154 my ($this,$action) = @_; |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
155 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
156 my $form = MyApp::Web::Schema::UpdateUser->new(); |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
157 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
158 $this->model->update( $form->Bind($action) ); |
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 |
334 | 161 sub GetChildResources { |
162 return { | |
163 name => 'create', | |
164 contract => { | |
165 class => 'My::Web::FormResource', | |
166 formName => 'create', | |
167 schema => 'profile.schema' | |
168 } | |
169 }, | |
170 { | |
171 match => qr/^(.*)$/, | |
172 contract => { | |
173 class => 'My::Web::ItemResource' | |
174 } | |
175 } | |
176 } | |
332
04a093f0a5a6
IMPL::Web::Application refactoring: resources are created per client request
cin
parents:
330
diff
changeset
|
177 |
229
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
178 =end code |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
179 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
180 =head1 MEMBERS |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
181 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
182 =head2 C<[static]contractFactory> |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
183 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
184 Фабрика, используемая для получения контракта ресурса. По умолчанию |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
185 C<IMPL::Web::Application::CustomResourceContract>. |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
186 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
187 =head2 C<[static]contractInstance> |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
188 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
189 Экземпляр контракта для ресурса. Создается при первом обращении при помощи |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
190 метода C<InitContract()>. |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
191 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
192 =head2 C<[static]InitContract()> |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
193 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
194 Создает новый экземпляр контракта, используя фабрику из свойства C<contractFactory>. |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
195 |
248 | 196 =head2 C<[static]CreateContract(%args)> |
197 | |
198 Создает новый контракт, который при создании ресурсов будет передавать им в | |
199 конструкторе параметры C<%args>. Реализуется при помощи C<IMPL::Object::Factory> | |
200 которой задается параметр ссылка на C<%args>, т.о. при создании ресурса, ему в | |
201 конструкторе будет передан список из ключей и значений хеша C<%args>, а затем | |
202 остальные аргументы. | |
203 | |
295 | 204 =head2 C<[static]CreateResource(%args)> |
205 | |
206 Создает контракт по-умолчанию и вызывает у него метод C<CreateResource(%args)>. | |
207 | |
229
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
208 =head2 C<[static]GetChildResources()> |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
209 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
210 Статический метод, который должны переопределять новые классы ресурсов, у |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
211 которых есть дочерние ресурсы. |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
212 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
213 =begin code |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
214 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
215 package MyApp::Web::MyResource |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
216 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
217 sub GetChildResources { |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
218 my $self = shift; |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
219 return |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
220 $self->SUPER::GetChildResources(), |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
221 { |
230 | 222 name => 'info', |
223 contract => $contractInfo | |
229
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
224 }; |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
225 } |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
226 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
227 =end code |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
228 |
230 | 229 Метод возвращает список из хешей, которые будут переданы в качестве параметра |
230 C<resources> контракту данного ресурса. | |
229
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
231 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
232 =cut |