annotate Lib/IMPL/Web/Application/ResourceContract.pm @ 331:2ff1726c066c

removed operation contract (due it's useless)
author cin
date Wed, 05 Jun 2013 18:21:11 +0400
parents fe725fad2d90
children 04a093f0a5a6
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
226
b6cde007a175 Added resource contract
sergey
parents:
diff changeset
1 package IMPL::Web::Application::ResourceContract;
b6cde007a175 Added resource contract
sergey
parents:
diff changeset
2 use strict;
302
673581380e79 Added templates provider functionality to controls
cin
parents: 285
diff changeset
3
673581380e79 Added templates provider functionality to controls
cin
parents: 285
diff changeset
4 use IMPL::lang;
230
6d8092d8ce1b *reworked IMPL::Security
sergey
parents: 229
diff changeset
5 use IMPL::Const qw(:prop);
226
b6cde007a175 Added resource contract
sergey
parents:
diff changeset
6 use IMPL::declare {
227
sergey
parents: 226
diff changeset
7 require => {
240
abc7c26bf615 +Added ObjectFactory for templates with static members support
sergey
parents: 230
diff changeset
8 Exception => 'IMPL::Exception',
abc7c26bf615 +Added ObjectFactory for templates with static members support
sergey
parents: 230
diff changeset
9 ArgumentException => '-IMPL::ArgumentException',
abc7c26bf615 +Added ObjectFactory for templates with static members support
sergey
parents: 230
diff changeset
10 KeyNotFoundException => '-IMPL::KeyNotFoundException',
330
fe725fad2d90 Added access checking to web resources
sergey
parents: 302
diff changeset
11 ResourceClass => 'IMPL::Web::Application::CustomResource',
fe725fad2d90 Added access checking to web resources
sergey
parents: 302
diff changeset
12 CustomContract => 'IMPL::Web::Application::CustomResourceContract',
fe725fad2d90 Added access checking to web resources
sergey
parents: 302
diff changeset
13 Factory => 'IMPL::Object::Factory'
229
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 base => [ 'IMPL::Object' => undef ],
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
16 props => [
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
17 resourceFactory => PROP_ALL,
248
814d755e5d12 Minor fixes
sergey
parents: 240
diff changeset
18 verbs => PROP_ALL,
229
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
19 _namedResources => PROP_ALL,
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
20 _regexpResources => PROP_ALL | PROP_LIST,
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
21 ]
226
b6cde007a175 Added resource contract
sergey
parents:
diff changeset
22 };
b6cde007a175 Added resource contract
sergey
parents:
diff changeset
23
227
sergey
parents: 226
diff changeset
24 sub CTOR {
sergey
parents: 226
diff changeset
25 my $this = shift;
sergey
parents: 226
diff changeset
26 my %args = @_;
229
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
27
330
fe725fad2d90 Added access checking to web resources
sergey
parents: 302
diff changeset
28 my $factory = delete $args{resourceFactory} || ResourceClass;
fe725fad2d90 Added access checking to web resources
sergey
parents: 302
diff changeset
29 my $resources = delete $args{resources} || [];
fe725fad2d90 Added access checking to web resources
sergey
parents: 302
diff changeset
30 my $verbs = delete $args{verbs} || {};
229
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
31
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
32 die ArgumentException->new(
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
33 resources => 'resources parameter must be a reference to an array' )
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
34 unless ref $resources eq 'ARRAY';
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
35
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
36 die ArgumentException->new(
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
37 opearations => 'operations parameter must be a reference to a hash' )
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
38 unless ref $verbs eq 'HASH';
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 $this->verbs(
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
41 { map { lc($_), $verbs->{$_} } keys %$verbs } );
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
42
228
431db7034a88 Для синхронизации
andrei <andrei@nap21.upri>
parents: 227
diff changeset
43 my %nameMap;
229
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
44
230
6d8092d8ce1b *reworked IMPL::Security
sergey
parents: 229
diff changeset
45 foreach my $res (@$resources) {
302
673581380e79 Added templates provider functionality to controls
cin
parents: 285
diff changeset
46 if (ref $res->{contract} eq 'HASH') {
331
2ff1726c066c removed operation contract (due it's useless)
cin
parents: 330
diff changeset
47 # custom contract will add OPTIONS verb if needed
2ff1726c066c removed operation contract (due it's useless)
cin
parents: 330
diff changeset
48 $res->{contract} = CustomContract->new(%{$res->{contract}});
302
673581380e79 Added templates provider functionality to controls
cin
parents: 285
diff changeset
49 }
673581380e79 Added templates provider functionality to controls
cin
parents: 285
diff changeset
50
228
431db7034a88 Для синхронизации
andrei <andrei@nap21.upri>
parents: 227
diff changeset
51 next unless $res->{contract};
302
673581380e79 Added templates provider functionality to controls
cin
parents: 285
diff changeset
52
229
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
53 if ( my $name = $res->{name} ) {
228
431db7034a88 Для синхронизации
andrei <andrei@nap21.upri>
parents: 227
diff changeset
54 $nameMap{$name} = $res;
431db7034a88 Для синхронизации
andrei <andrei@nap21.upri>
parents: 227
diff changeset
55 }
229
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
56 if ( $res->{match} ) {
228
431db7034a88 Для синхронизации
andrei <andrei@nap21.upri>
parents: 227
diff changeset
57 $this->_regexpResources->Append($res);
431db7034a88 Для синхронизации
andrei <andrei@nap21.upri>
parents: 227
diff changeset
58 }
431db7034a88 Для синхронизации
andrei <andrei@nap21.upri>
parents: 227
diff changeset
59 }
229
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
60
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
61 $this->_namedResources( \%nameMap );
330
fe725fad2d90 Added access checking to web resources
sergey
parents: 302
diff changeset
62
fe725fad2d90 Added access checking to web resources
sergey
parents: 302
diff changeset
63 $this->resourceFactory( %args ? Factory->new($factory,\%args) : $factory );
229
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
64 }
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
65
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
66 sub AddChildResourceContract {
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
67 my ($this,$res) = @_;
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
68
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
69 die ArgumentException->new(res => "A valid child resource definition")
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
70 unless ref $res eq 'HASH';
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 $this->_namedResources->{$res->{name}} = $res if $res->{name};
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
73 $this->_regexpResources->Append($res) if $res->{match};
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
74
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
75 return;
227
sergey
parents: 226
diff changeset
76 }
sergey
parents: 226
diff changeset
77
sergey
parents: 226
diff changeset
78 sub CreateResource {
228
431db7034a88 Для синхронизации
andrei <andrei@nap21.upri>
parents: 227
diff changeset
79 my $this = shift;
431db7034a88 Для синхронизации
andrei <andrei@nap21.upri>
parents: 227
diff changeset
80 my %args = @_;
431db7034a88 Для синхронизации
andrei <andrei@nap21.upri>
parents: 227
diff changeset
81
229
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
82 return $this->resourceFactory->new( %args, contract => $this );
228
431db7034a88 Для синхронизации
andrei <andrei@nap21.upri>
parents: 227
diff changeset
83 }
431db7034a88 Для синхронизации
andrei <andrei@nap21.upri>
parents: 227
diff changeset
84
229
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
85 sub FindChildResourceInfo {
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
86 my ( $this, $name ) = @_;
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
87
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
88 if ( my $info = $this->_namedResources->{$name} ) {
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
89 return $info, [$name];
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 else {
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
92 foreach my $info ( $this->_regexpResources ) {
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
93 my $rx = $info->{match};
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
94 if(my @childId = $name =~ m/$rx/) {
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
95 return $info, \@childId;
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 return;
227
sergey
parents: 226
diff changeset
101 }
sergey
parents: 226
diff changeset
102
226
b6cde007a175 Added resource contract
sergey
parents:
diff changeset
103 1;
b6cde007a175 Added resource contract
sergey
parents:
diff changeset
104
b6cde007a175 Added resource contract
sergey
parents:
diff changeset
105 __END__
b6cde007a175 Added resource contract
sergey
parents:
diff changeset
106
b6cde007a175 Added resource contract
sergey
parents:
diff changeset
107 =pod
b6cde007a175 Added resource contract
sergey
parents:
diff changeset
108
b6cde007a175 Added resource contract
sergey
parents:
diff changeset
109 =head1 NAME
b6cde007a175 Added resource contract
sergey
parents:
diff changeset
110
b6cde007a175 Added resource contract
sergey
parents:
diff changeset
111 C<IMPL::Web::Application::ResourceContract> - описание ресурса
b6cde007a175 Added resource contract
sergey
parents:
diff changeset
112
b6cde007a175 Added resource contract
sergey
parents:
diff changeset
113 =head1 SYNIOSIS
b6cde007a175 Added resource contract
sergey
parents:
diff changeset
114
b6cde007a175 Added resource contract
sergey
parents:
diff changeset
115 =begin code
b6cde007a175 Added resource contract
sergey
parents:
diff changeset
116
b6cde007a175 Added resource contract
sergey
parents:
diff changeset
117 use IMPL::require {
b6cde007a175 Added resource contract
sergey
parents:
diff changeset
118 ResourceContract => 'IMPL::Web::Application::ResourceContract',
b6cde007a175 Added resource contract
sergey
parents:
diff changeset
119 OperationContract => 'IMPL::Web::Application::OperationContract'
b6cde007a175 Added resource contract
sergey
parents:
diff changeset
120 };
b6cde007a175 Added resource contract
sergey
parents:
diff changeset
121
b6cde007a175 Added resource contract
sergey
parents:
diff changeset
122 my $contract = ResourceContract->new(
229
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
123 verbs => {
226
b6cde007a175 Added resource contract
sergey
parents:
diff changeset
124 get => OperationContract->new(
229
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
125 binding => sub {
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
126 my ($resource,$action) = @_;
226
b6cde007a175 Added resource contract
sergey
parents:
diff changeset
127 return "Hello!";
b6cde007a175 Added resource contract
sergey
parents:
diff changeset
128 }
229
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 post => OperationContract->new(
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
131 parameters => [
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
132 IMPL::Transform::DataToModel->new() # создаем преобразование для формы
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 binding => sub {
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
135 my ($resource,$action,$data) = @_;
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
136 return $resource->model->AddItem($data);
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 success => sub {
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
139 my ($resource,$action,$result) = @_;
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
140 return IMPL::Web::HttpResponse->Redirect(
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
141 location => $resource->location->Child($result->id)
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
142 )
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 error => sub {
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
145 my ($resource,$action,$error) = @_;
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
146 $action->errors->Append($error);
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
147 return $resource->model;
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
148 }
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 ),
226
b6cde007a175 Added resource contract
sergey
parents:
diff changeset
151 },
b6cde007a175 Added resource contract
sergey
parents:
diff changeset
152 resources => [
b6cde007a175 Added resource contract
sergey
parents:
diff changeset
153 {
b6cde007a175 Added resource contract
sergey
parents:
diff changeset
154 name => 'info',
229
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
155 binding => sub {
226
b6cde007a175 Added resource contract
sergey
parents:
diff changeset
156 return $_[0]->model->info;
b6cde007a175 Added resource contract
sergey
parents:
diff changeset
157 },
b6cde007a175 Added resource contract
sergey
parents:
diff changeset
158 contract => ResourceContract->new(
229
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
159 verbs => {
285
546957c50a36 *IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents: 268
diff changeset
160 # using method references is also possible
546957c50a36 *IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents: 268
diff changeset
161 get => sub {
546957c50a36 *IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents: 268
diff changeset
162 my ($resource,$action) = @_;
546957c50a36 *IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents: 268
diff changeset
163 return $resource->model;
546957c50a36 *IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents: 268
diff changeset
164 }
546957c50a36 *IMPL::Web::Handler::TTView Reworked template selection mechanism
cin
parents: 268
diff changeset
165
229
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
166 }
226
b6cde007a175 Added resource contract
sergey
parents:
diff changeset
167 )
b6cde007a175 Added resource contract
sergey
parents:
diff changeset
168 }
b6cde007a175 Added resource contract
sergey
parents:
diff changeset
169 ]
b6cde007a175 Added resource contract
sergey
parents:
diff changeset
170 )
b6cde007a175 Added resource contract
sergey
parents:
diff changeset
171
227
sergey
parents: 226
diff changeset
172 my $obj = My::App::Data->fetch('something');
sergey
parents: 226
diff changeset
173
sergey
parents: 226
diff changeset
174 my $resource = $contract->CreateResource(
sergey
parents: 226
diff changeset
175 model => $obj,
sergey
parents: 226
diff changeset
176 parent => $prentResource,
sergey
parents: 226
diff changeset
177 id => 'item-something'
sergey
parents: 226
diff changeset
178 );
sergey
parents: 226
diff changeset
179
229
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
180 my $child = $contract->FetchChildResource('info');
228
431db7034a88 Для синхронизации
andrei <andrei@nap21.upri>
parents: 227
diff changeset
181
226
b6cde007a175 Added resource contract
sergey
parents:
diff changeset
182 =end code
b6cde007a175 Added resource contract
sergey
parents:
diff changeset
183
b6cde007a175 Added resource contract
sergey
parents:
diff changeset
184 =head1 DESCRIPTION
b6cde007a175 Added resource contract
sergey
parents:
diff changeset
185
229
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
186 Контракт описывает структуру Веб-ресурса и отображение операций над ним в методы
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
187 предметной области. Контракты используются инфраструктурой и пользователь
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
188 не использует их напрямую, до тех пор пока не требуется изменить стандартный
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
189 функционал.
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
190
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
191
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
192 Ресурс представляе собой набор контрактов операций, соответствующих методам
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
193 C<HTTP> которые доступны у данного ресурса. Кроме операций ресурс состоит из
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
194 дочерних ресурсов, которые могут соответствовать регулярным выражениям, либо
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
195 иметь фиксированные имена.
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
196
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
197 Каждая операция над ресурсом C<IMPL::Web::Application::OperationContract>
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
198 описывается преобразованием параметров, привязкой к предметной области,
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
199 дополнительным обработчиком результата выполнения привязки, либо обработчиком
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
200 исключения, если привязку не удалось выполнить.
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
201
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
202 Основное назначение контракта - создавать объекты ресурсов, над которыми
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
203 контроллер запросов C<HTTP> сможет выполнить операцию. Контракт может создавать
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
204 дочерние ресурсы, на основе указанного родительского ресурса и идетификатора
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
205 нового ресурса. При этом будет найден подходящий контракт для дочернего ресурса
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
206 и с его помощью создан дочерний ресурс.
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
207
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
208 =head2 Динамический контракт
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
209
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
210 Основная функция контракта - превращать данные модели предметной области в
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
211 данные ресурсной модели, тоесть в ресурсы, для чего каждый контракт обязан
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
212 реализовывать метод C<CreateResource(%args)>.
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
213
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
214 Результатом выполнения этого метода должен быть Web-ресурс, см.
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
215 C<IMPL::Web::Application::Resource>. Другими словами не существует жесткого
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
216 требования к реализации самого контракта, как и того, что созданный ресурс
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
217 должен ссылаться именно на этот контракт (да и вообще ссылаться на контракт).
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
218
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
219 Таким образом можно реализовать контракт, который выполняет роль посредника,
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
220 ниже приведен пример, который выбирает нужный контракт на основе типа модели
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
221 переданной для создания ресурса.
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
222
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
223 =begin code
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
224
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
225 package My::Web::Application::ContractMapper;
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
226 use strict;
230
6d8092d8ce1b *reworked IMPL::Security
sergey
parents: 229
diff changeset
227 use IMPL::Const qw(:prop);
229
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
228 use IMPL::declare {
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
229 require => {
268
4abda21186cd *refactoring IMPL::Web: added 'application' property to resources
cin
parents: 248
diff changeset
230 ForbiddenException => 'IMPL::Web::ForbiddenException'
229
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
231 },
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
232 base => [
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
233 'IMPL::Object' => undef,
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
234 'IMPL::Object::Autofill' => '@_'
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
235 ],
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
236 props => [
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
237 map => PROP_GET | PROP_OWNERSET
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
238 ]
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
239 }
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
240
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
241 sub CreateResource {
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
242 my ($this,%args) = @_;
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
243
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
244 my $type = ref $args{model} || '_default';
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
245
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
246 my $contract = $this->map->{$type};
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
247
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
248 die ForbiddenException->new()
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
249 unless $contract;
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
250
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
251 return $contract->CreateResource(%args);
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
252 }
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
253
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
254 =end code
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
255
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
256 =head1 MEMBERS
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
257
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
258 =head2 C<CTOR(%args)>
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
259
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
260 =over
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
261
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
262 =item * C<resourceFactory>
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
263
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
264 Фабрика объектов C<IMPL::Object::Factory> которая будет использоваться при
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
265 создании новых ресурсов. По-умолчанию C<IMPL::Web::Application::Resource>.
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
266
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
267 =item * C<operations>
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
268
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
269 Хеш с доступными действиями над C<HTTP> ресурсом, ключом является имя ресурса,
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
270 значением C<IMPL::Web::Application::OperationContract>.
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
271
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
272 =item * C<resources>
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
273
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
274 Ссылка на массив хешей, каждый хеш описывает правила, как получить дочерний
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
275 ресурс и связать его с контрактом. Ниже преведено описание элементов хеша.
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
276
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
277 =over
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
278
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
279 =item * C<name>
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
280
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
281 Имя дочернегно ресурса.
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
282
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
283 =item * C<match>
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
284
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
285 Регулярное выражение, которому должно удовлетворять имя дочернего ресурса.
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
286
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
287 =item * C<bind>
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
288
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
289 Делегат, получающий модель для дочернего ресурса. Первым параметром ему
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
290 передается родительский объект, далее передаются граппы из регулярного
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
291 выражения, если это ресурс с именем удовлетворяющим регулярному выражению из
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
292 элемента C<match>, либо имя ресурса, если это ресурс с именем, указанным в
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
293 элементе C<name>.
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
294
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
295 =item * C<contract>
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
296
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
297 Ссылка на C<IMPL::Web::Application::ResourceContract> для дочернего ресурса.
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
298 У данного контракта используется только метод C<CreateContract>.
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
299
268
4abda21186cd *refactoring IMPL::Web: added 'application' property to resources
cin
parents: 248
diff changeset
300 Для реализации отложенной загрузки контракта (чтобы снизить ресурсоемкость
4abda21186cd *refactoring IMPL::Web: added 'application' property to resources
cin
parents: 248
diff changeset
301 запуска приложения) вместо ссылки на контракт может быть либо ссылка на
4abda21186cd *refactoring IMPL::Web: added 'application' property to resources
cin
parents: 248
diff changeset
302 процедуру, либо объект имеющий метод C<Invoke>.
4abda21186cd *refactoring IMPL::Web: added 'application' property to resources
cin
parents: 248
diff changeset
303
229
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
304 =back
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
305
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
306 По крайней мере C<name> или C<match> должны присутсвовать.
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
307
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
308 =back
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
309
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
310 =head2 C<CreateResource(%args)>
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
311
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
312 Создает ресурс, параметры C<%args> будут переданы напрямую констркутору
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
313 ресурса, для создания ресурса используется фабрика C<resourceFactory>.
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
314 При создании, конгструктору ресурса, будет передана ссылка на текущй контракт.
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
315
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
316 По-сути никакого дополнительного функционала данный метод не несет.
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
317
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
318 =head2 C<FindChildResourceInfo($childId)>
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
319
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
320 Используется для поиска информации о дочернем ресурсе, возвращает список из двух
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
321 элементов. C<($info,$childIdParts)>
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
322
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
323 =over
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
324
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
325 =item * C<$info>
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
326
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
327 Информация о контракте дочернего ресурса, как правило это ссылка на хеш, похожий
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
328 по формату на
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
329
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
330 =back
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
331
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
332 =head2 C<[get]verbs>
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
333
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
334 Хеш с доступными действиями над C<HTTP> ресурсом, все имена операций приведены
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
335 к нижнему регистру.
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
336
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
337 =begin code
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
338
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
339 my $result = $contract->verbs->{get}->Invoke($resource,$action);
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
340
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
341 =end code
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
342
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
343 =cut