Mercurial > pub > Impl
annotate Lib/IMPL/Web/Application/OperationContract.pm @ 272:47db27ed5b43
sync
author | sergey |
---|---|
date | Mon, 28 Jan 2013 17:24:37 +0400 |
parents | 4abda21186cd |
children |
rev | line source |
---|---|
228 | 1 package IMPL::Web::Application::OperationContract; |
2 use strict; | |
3 | |
229
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
228
diff
changeset
|
4 use IMPL::lang qw(:declare); |
228 | 5 use IMPL::declare { |
229
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
228
diff
changeset
|
6 require => { |
230 | 7 Exception => 'IMPL::Exception', |
8 ArgumentException => '-IMPL::InvalidArgumentException', | |
9 ResourceInterface => 'IMPL::Web::Application::ResourceInterface' | |
229
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
228
diff
changeset
|
10 }, |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
228
diff
changeset
|
11 base => [ |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
228
diff
changeset
|
12 'IMPL::Object' => undef, |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
228
diff
changeset
|
13 'IMPL::Object::Autofill' => '@_' |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
228
diff
changeset
|
14 ], |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
228
diff
changeset
|
15 props => [ |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
228
diff
changeset
|
16 binding => PROP_ALL, |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
228
diff
changeset
|
17 success => PROP_ALL, |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
228
diff
changeset
|
18 error => PROP_ALL |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
228
diff
changeset
|
19 ] |
228 | 20 }; |
21 | |
22 sub Invoke { | |
229
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
228
diff
changeset
|
23 my ( $this, $resource, $request ) = @_; |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
228
diff
changeset
|
24 |
230 | 25 die ArgumentException->new( resource => 'A valid resource is required' ) |
26 unless eval { $resource->isa(ResourceInterface) }; | |
27 | |
229
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
228
diff
changeset
|
28 my $result = eval { |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
228
diff
changeset
|
29 _InvokeDelegate($this->binding, $resource, $request) |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
228
diff
changeset
|
30 }; |
228 | 31 |
229
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
228
diff
changeset
|
32 if (my $e = $@) { |
230 | 33 if ($this->error) { |
34 $result = _InvokeDelegate($this->error, $resource, $request, $e) ; | |
35 } else { | |
36 die $e; | |
37 } | |
38 | |
229
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
228
diff
changeset
|
39 } else { |
230 | 40 $result = _InvokeDelegate($this->success, $resource, $request, $result) |
41 if ($this->success); | |
229
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
228
diff
changeset
|
42 } |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
228
diff
changeset
|
43 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
228
diff
changeset
|
44 return $result; |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
228
diff
changeset
|
45 } |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
228
diff
changeset
|
46 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
228
diff
changeset
|
47 sub _InvokeDelegate { |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
228
diff
changeset
|
48 my $delegate = shift; |
228 | 49 |
230 | 50 return $delegate->(@_) if ref $delegate eq 'CODE'; |
229
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
228
diff
changeset
|
51 return $delegate->Invoke(@_) if eval { $delegate->can('Invoke')}; |
228 | 52 } |
53 | |
54 1; | |
55 | |
56 __END__ | |
57 | |
58 =pod | |
59 | |
60 =head1 NAME | |
61 | |
62 C<IMPL::Web::Application::OperationContract> - Описание операции над | |
63 веб-ресурсом. | |
64 | |
65 =head1 SYNOPSIS | |
66 | |
67 =begin code | |
68 | |
69 use IMPL::require { | |
70 'OperationContract' => 'IMPL::Web::Application::OperationContract', | |
268
4abda21186cd
*refactoring IMPL::Web: added 'application' property to resources
cin
parents:
230
diff
changeset
|
71 'HttpReponse' => 'IMPL::Web::Application::HttpReponse' |
228 | 72 }; |
73 | |
74 my $operation = OperationContract->new( | |
229
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
228
diff
changeset
|
75 binding => sub { |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
228
diff
changeset
|
76 my ($resource,$request) = @_; |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
228
diff
changeset
|
77 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
228
diff
changeset
|
78 my $itemName = $request->param('itemName', qr/^(\w+)$/); |
228 | 79 |
229
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
228
diff
changeset
|
80 return $model->FindItem($itemName); |
228 | 81 }, |
229
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
228
diff
changeset
|
82 success => sub { |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
228
diff
changeset
|
83 my ($resource,$request,$result) = @_; |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
228
diff
changeset
|
84 return HttpReponse->Redirect(location => $resource->location->Child($result->id)); |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
228
diff
changeset
|
85 } |
228 | 86 ); |
87 | |
88 my $response = $operation->InvokeOperation($resource); | |
89 | |
90 =end code | |
91 | |
92 =head1 DESCRIPTION | |
93 | |
268
4abda21186cd
*refactoring IMPL::Web: added 'application' property to resources
cin
parents:
230
diff
changeset
|
94 Для описания контракта операции используется понятие делегата, тоесть объекта, |
229
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
228
diff
changeset
|
95 представляющего собой функцию, либо объект, имеющий метод C<Invoke>. |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
228
diff
changeset
|
96 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
228
diff
changeset
|
97 Поскольку предметная область должна быть отделена от |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
228
diff
changeset
|
98 контроллеров веб-сервиса, она ничего не знает про существование ресурсов и их |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
228
diff
changeset
|
99 организацию и тем более о протоколе C<HTTP>, поэтому все вещи, связанные с |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
228
diff
changeset
|
100 формированием ответов сервера, представлениями данных и т.п. должны выполняться |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
228
diff
changeset
|
101 самими контроллерами. Поведение контроллеров описывается контрактами, в которых |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
228
diff
changeset
|
102 указываются делегаты для реализации необходимого функционала, для корректного |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
228
diff
changeset
|
103 отображения ресурсов в объекты предметной области и обратно. |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
228
diff
changeset
|
104 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
228
diff
changeset
|
105 Контракт операции состоит из нескольких свойств, осуществляющих привязку к |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
228
diff
changeset
|
106 предметной области: |
228 | 107 |
108 =over | |
109 | |
229
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
228
diff
changeset
|
110 =item * C<binding> |
228 | 111 |
229
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
228
diff
changeset
|
112 делегат для привязки операции над ресурсом к предметной области. |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
228
diff
changeset
|
113 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
228
diff
changeset
|
114 =item * C<success> |
228 | 115 |
229
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
228
diff
changeset
|
116 делегат для обработки результат операции, например для формирования ответа с |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
228
diff
changeset
|
117 перенаправлением. |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
228
diff
changeset
|
118 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
228
diff
changeset
|
119 =item * C<error> |
228 | 120 |
229
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
228
diff
changeset
|
121 делегат для обработки исключительной ситуации, может быть использован для |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
228
diff
changeset
|
122 формирования представления для повторного ввода данных на форме. |
228 | 123 |
229
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
228
diff
changeset
|
124 =back |
228 | 125 |
126 =head1 MEMBERS | |
127 | |
229
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
228
diff
changeset
|
128 =head2 C<[get,set] binding> |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
228
diff
changeset
|
129 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
228
diff
changeset
|
130 Привязка операции к ресурсу, например |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
228
diff
changeset
|
131 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
228
diff
changeset
|
132 =begin code |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
228
diff
changeset
|
133 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
228
diff
changeset
|
134 $operationContract->binding(sub { |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
228
diff
changeset
|
135 my ($resource,$action) = @_; |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
228
diff
changeset
|
136 $resource->model |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
228
diff
changeset
|
137 }) |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
228
diff
changeset
|
138 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
228
diff
changeset
|
139 =end code |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
228
diff
changeset
|
140 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
228
diff
changeset
|
141 Может быть как ссылка на процедуру, так и ссылкой на объект, имеющий метод |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
228
diff
changeset
|
142 C<Invoke>. |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
228
diff
changeset
|
143 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
228
diff
changeset
|
144 =head2 C<[get,set] success> |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
228
diff
changeset
|
145 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
228
diff
changeset
|
146 Обрабатывает результат привязки к предметной области. |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
228
diff
changeset
|
147 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
228
diff
changeset
|
148 =begin code |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
228
diff
changeset
|
149 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
228
diff
changeset
|
150 # redirect (for example after POST) |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
228
diff
changeset
|
151 $operationContract->success(sub { |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
228
diff
changeset
|
152 my ($resource,$action,$result) = @_; |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
228
diff
changeset
|
153 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
228
diff
changeset
|
154 return IMPL::Web::HttpResponse |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
228
diff
changeset
|
155 ->Redirect($resource->location->Child($result->id)); |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
228
diff
changeset
|
156 }) |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
228
diff
changeset
|
157 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
228
diff
changeset
|
158 =end code |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
228
diff
changeset
|
159 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
228
diff
changeset
|
160 Может быть как ссылка на процедуру, так и ссылкой на объект, имеющий метод |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
228
diff
changeset
|
161 C<Invoke>. |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
228
diff
changeset
|
162 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
228
diff
changeset
|
163 =head2 C<[get,set] error> |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
228
diff
changeset
|
164 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
228
diff
changeset
|
165 Обрабатывает ошибку возникшую при выполнении привязки к предметной области. |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
228
diff
changeset
|
166 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
228
diff
changeset
|
167 =begin |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
228
diff
changeset
|
168 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
228
diff
changeset
|
169 $operationContract->error(sub { |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
228
diff
changeset
|
170 my ($resource,$action,$error) = @_; |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
228
diff
changeset
|
171 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
228
diff
changeset
|
172 $action->form->errors->{''} = $error; |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
228
diff
changeset
|
173 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
228
diff
changeset
|
174 return $resource->model; |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
228
diff
changeset
|
175 }); |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
228
diff
changeset
|
176 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
228
diff
changeset
|
177 =end |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
228
diff
changeset
|
178 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
228
diff
changeset
|
179 =cut |