Mercurial > pub > Impl
annotate Lib/IMPL/Web/Application/OperationContract.pm @ 229:47f77e6409f7
heavily reworked the resource model of the web application:
*some ResourcesContraact functionality moved to Resource
+Added CustomResource
*Corrected action handlers
author | sergey |
---|---|
date | Sat, 29 Sep 2012 02:34:47 +0400 |
parents | 431db7034a88 |
children | 6d8092d8ce1b |
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 => { |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
228
diff
changeset
|
7 'Exception' => 'IMPL::Exception', |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
228
diff
changeset
|
8 'ArgumentException' => '-IMPL::ArgumentException', |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
228
diff
changeset
|
9 'ResourceBaseClass' => 'IMPL::Web::Application::ResourceBase' |
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 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
228
diff
changeset
|
25 die ArgumentException( resource => 'A valid resource is required' ) |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
228
diff
changeset
|
26 unless eval { $resource->isa(ResourceBaseClass) }; |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
228
diff
changeset
|
27 |
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 = $@) { |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
228
diff
changeset
|
33 $result = _InvokeDelegate($this->error, $resource, $request, $e); |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
228
diff
changeset
|
34 } else { |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
228
diff
changeset
|
35 $result = _InvokeDelegate($this->success, $resource, $request, $result); |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
228
diff
changeset
|
36 } |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
228
diff
changeset
|
37 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
228
diff
changeset
|
38 return $result; |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
228
diff
changeset
|
39 } |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
228
diff
changeset
|
40 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
228
diff
changeset
|
41 sub _InvokeDelegate { |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
228
diff
changeset
|
42 my $delegate = shift; |
228 | 43 |
229
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
228
diff
changeset
|
44 return $delegete->(@_) if ref $delegate eq 'CODE'; |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
228
diff
changeset
|
45 return $delegate->Invoke(@_) if eval { $delegate->can('Invoke')}; |
228 | 46 } |
47 | |
48 1; | |
49 | |
50 __END__ | |
51 | |
52 =pod | |
53 | |
54 =head1 NAME | |
55 | |
56 C<IMPL::Web::Application::OperationContract> - Описание операции над | |
57 веб-ресурсом. | |
58 | |
59 =head1 SYNOPSIS | |
60 | |
61 =begin code | |
62 | |
63 use IMPL::require { | |
64 'OperationContract' => 'IMPL::Web::Application::OperationContract', | |
65 'RedirectResponse' => 'IMPL::Web::Application::RedirectResponse' | |
66 }; | |
67 | |
68 my $operation = OperationContract->new( | |
229
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
228
diff
changeset
|
69 binding => sub { |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
228
diff
changeset
|
70 my ($resource,$request) = @_; |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
228
diff
changeset
|
71 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
228
diff
changeset
|
72 my $itemName = $request->param('itemName', qr/^(\w+)$/); |
228 | 73 |
229
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
228
diff
changeset
|
74 return $model->FindItem($itemName); |
228 | 75 }, |
229
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
228
diff
changeset
|
76 success => sub { |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
228
diff
changeset
|
77 my ($resource,$request,$result) = @_; |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
228
diff
changeset
|
78 return HttpReponse->Redirect(location => $resource->location->Child($result->id)); |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
228
diff
changeset
|
79 } |
228 | 80 ); |
81 | |
82 my $response = $operation->InvokeOperation($resource); | |
83 | |
84 =end code | |
85 | |
86 =head1 DESCRIPTION | |
87 | |
229
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
228
diff
changeset
|
88 Для орисания контракта операции используется понятие делегата, тоесть объекта, |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
228
diff
changeset
|
89 представляющего собой функцию, либо объект, имеющий метод C<Invoke>. |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
228
diff
changeset
|
90 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
228
diff
changeset
|
91 Поскольку предметная область должна быть отделена от |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
228
diff
changeset
|
92 контроллеров веб-сервиса, она ничего не знает про существование ресурсов и их |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
228
diff
changeset
|
93 организацию и тем более о протоколе C<HTTP>, поэтому все вещи, связанные с |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
228
diff
changeset
|
94 формированием ответов сервера, представлениями данных и т.п. должны выполняться |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
228
diff
changeset
|
95 самими контроллерами. Поведение контроллеров описывается контрактами, в которых |
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 Контракт операции состоит из нескольких свойств, осуществляющих привязку к |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
228
diff
changeset
|
100 предметной области: |
228 | 101 |
102 =over | |
103 | |
229
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
228
diff
changeset
|
104 =item * C<binding> |
228 | 105 |
229
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
228
diff
changeset
|
106 делегат для привязки операции над ресурсом к предметной области. |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
228
diff
changeset
|
107 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
228
diff
changeset
|
108 =item * C<success> |
228 | 109 |
229
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
228
diff
changeset
|
110 делегат для обработки результат операции, например для формирования ответа с |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
228
diff
changeset
|
111 перенаправлением. |
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 =item * C<error> |
228 | 114 |
229
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
228
diff
changeset
|
115 делегат для обработки исключительной ситуации, может быть использован для |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
228
diff
changeset
|
116 формирования представления для повторного ввода данных на форме. |
228 | 117 |
229
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
228
diff
changeset
|
118 =back |
228 | 119 |
120 =head1 MEMBERS | |
121 | |
229
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
228
diff
changeset
|
122 =head2 C<[get,set] binding> |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
228
diff
changeset
|
123 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
228
diff
changeset
|
124 Привязка операции к ресурсу, например |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
228
diff
changeset
|
125 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
228
diff
changeset
|
126 =begin code |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
228
diff
changeset
|
127 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
228
diff
changeset
|
128 $operationContract->binding(sub { |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
228
diff
changeset
|
129 my ($resource,$action) = @_; |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
228
diff
changeset
|
130 $resource->model |
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 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
228
diff
changeset
|
133 =end code |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
228
diff
changeset
|
134 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
228
diff
changeset
|
135 Может быть как ссылка на процедуру, так и ссылкой на объект, имеющий метод |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
228
diff
changeset
|
136 C<Invoke>. |
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 =head2 C<[get,set] success> |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
228
diff
changeset
|
139 |
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 =begin code |
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 # redirect (for example after POST) |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
228
diff
changeset
|
145 $operationContract->success(sub { |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
228
diff
changeset
|
146 my ($resource,$action,$result) = @_; |
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 return IMPL::Web::HttpResponse |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
228
diff
changeset
|
149 ->Redirect($resource->location->Child($result->id)); |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
228
diff
changeset
|
150 }) |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
228
diff
changeset
|
151 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
228
diff
changeset
|
152 =end code |
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 Может быть как ссылка на процедуру, так и ссылкой на объект, имеющий метод |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
228
diff
changeset
|
155 C<Invoke>. |
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 =head2 C<[get,set] error> |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
228
diff
changeset
|
158 |
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 =begin |
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 $operationContract->error(sub { |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
228
diff
changeset
|
164 my ($resource,$action,$error) = @_; |
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 $action->form->errors->{''} = $error; |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
228
diff
changeset
|
167 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
228
diff
changeset
|
168 return $resource->model; |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
228
diff
changeset
|
169 }); |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
228
diff
changeset
|
170 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
228
diff
changeset
|
171 =end |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
228
diff
changeset
|
172 |
47f77e6409f7
heavily reworked the resource model of the web application:
sergey
parents:
228
diff
changeset
|
173 =cut |