Mercurial > pub > Impl
annotate Lib/IMPL/Web/Application/CustomResource.pm @ 369:7c784144d2f1
Implemented object metadata class, cleanup
author | cin |
---|---|
date | Mon, 09 Dec 2013 17:35:34 +0400 |
parents | e8be9062ecf2 |
children | e12c14177848 |
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 => [ | |
335 | 15 accessCheck => PROP_RW, |
16 resources => PROP_RO, | |
17 verbs => PROP_RO, | |
18 namedResources => PROP_RO, | |
19 regexResources => PROP_RO | |
229
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
20 ] |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
21 }; |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
22 |
334 | 23 our %RESOURCE_BINDINGS = ( |
24 GET => 'HttpGet', | |
25 POST => 'HttpPost', | |
26 PUT => 'HttpPut', | |
27 DELETE => 'HttpDelete', | |
335 | 28 HEAD => 'HttpHead', |
29 OPTIONS => 'HttpOptions', | |
30 TRACE => 'HttpTrace' | |
334 | 31 ); |
32 | |
330 | 33 sub CTOR { |
34 my ($this,%args) = @_; | |
35 | |
335 | 36 $this->verbs($args{verbs} || {}); |
37 $this->resources($args{resources} || []); | |
38 | |
330 | 39 $this->accessCheck($args{accessCheck}) |
40 if $args{accessCheck}; | |
334 | 41 |
42 while(my ($verb,$methodName) = each %RESOURCE_BINDINGS) { | |
335 | 43 if(my $method = $this->can($methodName)) { |
44 $this->verbs->{lc($verb)} ||= $method; | |
45 } | |
334 | 46 } |
330 | 47 } |
48 | |
334 | 49 sub FindChildResourceInfo { |
50 my ( $this, $name ) = @_; | |
229
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
51 |
335 | 52 $this->PrepareResourcesCache() |
53 unless $this->namedResources; | |
54 | |
55 if ( my $info = $this->namedResources->{$name} ) { | |
56 return $info, [$name]; | |
57 } | |
58 else { | |
59 foreach my $info ( @{$this->regexResources} ) { | |
60 my $rx = $info->{match}; | |
61 if(my @childId = $name =~ m/$rx/) { | |
62 return $info, \@childId; | |
63 } | |
64 } | |
65 } | |
66 | |
67 return; | |
68 } | |
69 | |
70 sub GetAllowedMethods { | |
71 map( uc, keys %{ shift->verbs } ); | |
229
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
72 } |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
73 |
334 | 74 sub PrepareResourcesCache { |
335 | 75 my ($this) = @_; |
76 | |
77 my @resources = ($this->GetChildResources(), @{$this->resources}); | |
78 | |
334 | 79 my %nameMap; |
80 my @rxMap; | |
248 | 81 |
335 | 82 foreach my $res (@resources) { |
334 | 83 #skip resources without contract |
84 next unless $res->{contract}; | |
85 | |
86 if ( my $name = $res->{name} ) { | |
87 $nameMap{$name} = $res; | |
88 } | |
89 if ( $res->{match} ) { | |
90 push @rxMap,$res; | |
91 } | |
92 } | |
93 | |
335 | 94 $this->regexResources(\@rxMap); |
95 $this->namedResources(\%nameMap); | |
295 | 96 } |
97 | |
330 | 98 sub AccessCheck { |
99 my ($this,$verb) = @_; | |
100 | |
101 my $handler = $this->accessCheck; | |
102 | |
103 if(ref($handler) eq 'CODE') { | |
334 | 104 return &$handler($this,$verb); |
330 | 105 } |
106 } | |
107 | |
229
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
108 sub GetChildResources { |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
109 |
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 |
335 | 112 sub HttpOptions { |
334 | 113 my ($this) = @_; |
114 | |
335 | 115 my @allow = $this->GetAllowedMethods(); |
334 | 116 return HttpResponse->new( |
117 status => '200 OK', | |
118 headers => { | |
119 allow => join ( ',', @allow ) | |
120 } | |
121 ); | |
122 } | |
123 | |
229
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
124 1; |
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 __END__ |
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 =pod |
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 =head1 NAME |
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 C<IMPL::Web::Application::CustomResource> - базовый класс для ресурсов, |
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 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
135 =head1 SYNOPSIS |
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 =begin code |
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 package MyApp::Web::Resources::ProfileResource; |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
140 use IMPL::declare { |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
141 base => [ |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
142 'IMPL::Web::Application::CustomResource' => '@_' |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
143 ] |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
144 } |
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 sub HttpGet { |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
147 my ($this) = @_; |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
148 return $this->model; |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
149 } |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
150 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
151 sub HttpPut { |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
152 my ($this,$action) = @_; |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
153 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
154 my $form = MyApp::Web::Schema::UpdateUser->new(); |
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 $this->model->update( $form->Bind($action) ); |
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 |
334 | 159 sub GetChildResources { |
160 return { | |
161 name => 'create', | |
162 contract => { | |
163 class => 'My::Web::FormResource', | |
164 formName => 'create', | |
165 schema => 'profile.schema' | |
166 } | |
167 }, | |
168 { | |
169 match => qr/^(.*)$/, | |
170 contract => { | |
171 class => 'My::Web::ItemResource' | |
172 } | |
173 } | |
174 } | |
332
04a093f0a5a6
IMPL::Web::Application refactoring: resources are created per client request
cin
parents:
330
diff
changeset
|
175 |
229
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 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
178 =head1 MEMBERS |
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 =head2 C<[static]contractFactory> |
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 Фабрика, используемая для получения контракта ресурса. По умолчанию |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
183 C<IMPL::Web::Application::CustomResourceContract>. |
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 =head2 C<[static]contractInstance> |
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 Экземпляр контракта для ресурса. Создается при первом обращении при помощи |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
188 метода C<InitContract()>. |
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 =head2 C<[static]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 Создает новый экземпляр контракта, используя фабрику из свойства C<contractFactory>. |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
193 |
248 | 194 =head2 C<[static]CreateContract(%args)> |
195 | |
196 Создает новый контракт, который при создании ресурсов будет передавать им в | |
197 конструкторе параметры C<%args>. Реализуется при помощи C<IMPL::Object::Factory> | |
198 которой задается параметр ссылка на C<%args>, т.о. при создании ресурса, ему в | |
199 конструкторе будет передан список из ключей и значений хеша C<%args>, а затем | |
200 остальные аргументы. | |
201 | |
295 | 202 =head2 C<[static]CreateResource(%args)> |
203 | |
204 Создает контракт по-умолчанию и вызывает у него метод C<CreateResource(%args)>. | |
205 | |
229
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
206 =head2 C<[static]GetChildResources()> |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
207 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
208 Статический метод, который должны переопределять новые классы ресурсов, у |
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 =begin code |
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 package MyApp::Web::MyResource |
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 sub GetChildResources { |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
216 my $self = shift; |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
217 return |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
218 $self->SUPER::GetChildResources(), |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
219 { |
230 | 220 name => 'info', |
221 contract => $contractInfo | |
229
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
222 }; |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
223 } |
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 =end code |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
226 |
230 | 227 Метод возвращает список из хешей, которые будут переданы в качестве параметра |
228 C<resources> контракту данного ресурса. | |
229
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
229 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
diff
changeset
|
230 =cut |