annotate Lib/IMPL/Web/Application/ResourceContract.pm @ 241:f48a1a9f4fa2

+Added ViewResult to allow implementation of the view environment. *TTDocuments now storing creation parameters *TTControls automatically propagating layout and title meta to their attributes +Added UnauthorizaedException web exception *minor fixes
author sergey
date Thu, 18 Oct 2012 04:49:55 +0400
parents abc7c26bf615
children 814d755e5d12
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;
230
6d8092d8ce1b *reworked IMPL::Security
sergey
parents: 229
diff changeset
3 use IMPL::Const qw(:prop);
226
b6cde007a175 Added resource contract
sergey
parents:
diff changeset
4 use IMPL::declare {
227
sergey
parents: 226
diff changeset
5 require => {
240
abc7c26bf615 +Added ObjectFactory for templates with static members support
sergey
parents: 230
diff changeset
6 Exception => 'IMPL::Exception',
abc7c26bf615 +Added ObjectFactory for templates with static members support
sergey
parents: 230
diff changeset
7 ArgumentException => '-IMPL::ArgumentException',
abc7c26bf615 +Added ObjectFactory for templates with static members support
sergey
parents: 230
diff changeset
8 KeyNotFoundException => '-IMPL::KeyNotFoundException',
abc7c26bf615 +Added ObjectFactory for templates with static members support
sergey
parents: 230
diff changeset
9 ResourceClass => 'IMPL::Web::Application::Resource'
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 => [ 'IMPL::Object' => undef ],
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
12 props => [
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
13 resourceFactory => PROP_ALL,
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
14 verbs => PROP_ALL,
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
15 _namedResources => PROP_ALL,
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
16 _regexpResources => PROP_ALL | PROP_LIST,
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
17 ]
226
b6cde007a175 Added resource contract
sergey
parents:
diff changeset
18 };
b6cde007a175 Added resource contract
sergey
parents:
diff changeset
19
227
sergey
parents: 226
diff changeset
20 sub CTOR {
sergey
parents: 226
diff changeset
21 my $this = shift;
sergey
parents: 226
diff changeset
22 my %args = @_;
229
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
23
227
sergey
parents: 226
diff changeset
24 $this->resourceFactory( $args{resourceFactory} || ResourceClass );
229
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
25
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
26 my $resources = $args{resources} || [];
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
27 my $verbs = $args{verbs} || {};
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
28
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
29 die ArgumentException->new(
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
30 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
31 unless ref $resources eq 'ARRAY';
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
32
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
33 die ArgumentException->new(
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
34 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
35 unless ref $verbs eq 'HASH';
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 $this->verbs(
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
38 { map { lc($_), $verbs->{$_} } keys %$verbs } );
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
39
228
431db7034a88 Для синхронизации
andrei <andrei@nap21.upri>
parents: 227
diff changeset
40 my %nameMap;
229
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
41
230
6d8092d8ce1b *reworked IMPL::Security
sergey
parents: 229
diff changeset
42 foreach my $res (@$resources) {
228
431db7034a88 Для синхронизации
andrei <andrei@nap21.upri>
parents: 227
diff changeset
43 next unless $res->{contract};
229
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
44 if ( my $name = $res->{name} ) {
228
431db7034a88 Для синхронизации
andrei <andrei@nap21.upri>
parents: 227
diff changeset
45 $nameMap{$name} = $res;
431db7034a88 Для синхронизации
andrei <andrei@nap21.upri>
parents: 227
diff changeset
46 }
229
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
47 if ( $res->{match} ) {
228
431db7034a88 Для синхронизации
andrei <andrei@nap21.upri>
parents: 227
diff changeset
48 $this->_regexpResources->Append($res);
431db7034a88 Для синхронизации
andrei <andrei@nap21.upri>
parents: 227
diff changeset
49 }
431db7034a88 Для синхронизации
andrei <andrei@nap21.upri>
parents: 227
diff changeset
50 }
229
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
51
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
52 $this->_namedResources( \%nameMap );
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
53 }
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
54
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
55 sub AddChildResourceContract {
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
56 my ($this,$res) = @_;
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
57
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
58 die ArgumentException->new(res => "A valid child resource definition")
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
59 unless ref $res eq 'HASH';
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->{$res->{name}} = $res if $res->{name};
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
62 $this->_regexpResources->Append($res) if $res->{match};
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
63
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
64 return;
227
sergey
parents: 226
diff changeset
65 }
sergey
parents: 226
diff changeset
66
sergey
parents: 226
diff changeset
67 sub CreateResource {
228
431db7034a88 Для синхронизации
andrei <andrei@nap21.upri>
parents: 227
diff changeset
68 my $this = shift;
431db7034a88 Для синхронизации
andrei <andrei@nap21.upri>
parents: 227
diff changeset
69 my %args = @_;
431db7034a88 Для синхронизации
andrei <andrei@nap21.upri>
parents: 227
diff changeset
70
229
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
71 return $this->resourceFactory->new( %args, contract => $this );
228
431db7034a88 Для синхронизации
andrei <andrei@nap21.upri>
parents: 227
diff changeset
72 }
431db7034a88 Для синхронизации
andrei <andrei@nap21.upri>
parents: 227
diff changeset
73
229
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
74 sub FindChildResourceInfo {
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
75 my ( $this, $name ) = @_;
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
76
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
77 if ( my $info = $this->_namedResources->{$name} ) {
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
78 return $info, [$name];
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
79 }
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
80 else {
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
81 foreach my $info ( $this->_regexpResources ) {
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
82 my $rx = $info->{match};
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
83 if(my @childId = $name =~ m/$rx/) {
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
84 return $info, \@childId;
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
85 }
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
86 }
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
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
89 return;
227
sergey
parents: 226
diff changeset
90 }
sergey
parents: 226
diff changeset
91
226
b6cde007a175 Added resource contract
sergey
parents:
diff changeset
92 1;
b6cde007a175 Added resource contract
sergey
parents:
diff changeset
93
b6cde007a175 Added resource contract
sergey
parents:
diff changeset
94 __END__
b6cde007a175 Added resource contract
sergey
parents:
diff changeset
95
b6cde007a175 Added resource contract
sergey
parents:
diff changeset
96 =pod
b6cde007a175 Added resource contract
sergey
parents:
diff changeset
97
b6cde007a175 Added resource contract
sergey
parents:
diff changeset
98 =head1 NAME
b6cde007a175 Added resource contract
sergey
parents:
diff changeset
99
b6cde007a175 Added resource contract
sergey
parents:
diff changeset
100 C<IMPL::Web::Application::ResourceContract> - описание ресурса
b6cde007a175 Added resource contract
sergey
parents:
diff changeset
101
b6cde007a175 Added resource contract
sergey
parents:
diff changeset
102 =head1 SYNIOSIS
b6cde007a175 Added resource contract
sergey
parents:
diff changeset
103
b6cde007a175 Added resource contract
sergey
parents:
diff changeset
104 =begin code
b6cde007a175 Added resource contract
sergey
parents:
diff changeset
105
b6cde007a175 Added resource contract
sergey
parents:
diff changeset
106 use IMPL::require {
b6cde007a175 Added resource contract
sergey
parents:
diff changeset
107 ResourceContract => 'IMPL::Web::Application::ResourceContract',
b6cde007a175 Added resource contract
sergey
parents:
diff changeset
108 OperationContract => 'IMPL::Web::Application::OperationContract'
b6cde007a175 Added resource contract
sergey
parents:
diff changeset
109 };
b6cde007a175 Added resource contract
sergey
parents:
diff changeset
110
b6cde007a175 Added resource contract
sergey
parents:
diff changeset
111 my $contract = ResourceContract->new(
229
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
112 verbs => {
226
b6cde007a175 Added resource contract
sergey
parents:
diff changeset
113 get => OperationContract->new(
229
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
114 binding => sub {
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
115 my ($resource,$action) = @_;
226
b6cde007a175 Added resource contract
sergey
parents:
diff changeset
116 return "Hello!";
b6cde007a175 Added resource contract
sergey
parents:
diff changeset
117 }
229
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 post => OperationContract->new(
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
120 parameters => [
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
121 IMPL::Transform::DataToModel->new() # создаем преобразование для формы
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
122 ],
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
123 binding => sub {
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
124 my ($resource,$action,$data) = @_;
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
125 return $resource->model->AddItem($data);
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
126 },
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
127 success => sub {
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
128 my ($resource,$action,$result) = @_;
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
129 return IMPL::Web::HttpResponse->Redirect(
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
130 location => $resource->location->Child($result->id)
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 error => sub {
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
134 my ($resource,$action,$error) = @_;
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
135 $action->errors->Append($error);
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
136 return $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 ),
226
b6cde007a175 Added resource contract
sergey
parents:
diff changeset
140 },
b6cde007a175 Added resource contract
sergey
parents:
diff changeset
141 resources => [
b6cde007a175 Added resource contract
sergey
parents:
diff changeset
142 {
b6cde007a175 Added resource contract
sergey
parents:
diff changeset
143 name => 'info',
229
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
144 binding => sub {
226
b6cde007a175 Added resource contract
sergey
parents:
diff changeset
145 return $_[0]->model->info;
b6cde007a175 Added resource contract
sergey
parents:
diff changeset
146 },
b6cde007a175 Added resource contract
sergey
parents:
diff changeset
147 contract => ResourceContract->new(
229
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
148 verbs => {
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
149 get => OperationContract->new(
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
150 binding => sub {
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
151 my ($resource,$action) = @_;
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
152 return $resource->model;
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 }
226
b6cde007a175 Added resource contract
sergey
parents:
diff changeset
156 )
b6cde007a175 Added resource contract
sergey
parents:
diff changeset
157 }
b6cde007a175 Added resource contract
sergey
parents:
diff changeset
158 ]
b6cde007a175 Added resource contract
sergey
parents:
diff changeset
159 )
b6cde007a175 Added resource contract
sergey
parents:
diff changeset
160
227
sergey
parents: 226
diff changeset
161 my $obj = My::App::Data->fetch('something');
sergey
parents: 226
diff changeset
162
sergey
parents: 226
diff changeset
163 my $resource = $contract->CreateResource(
sergey
parents: 226
diff changeset
164 model => $obj,
sergey
parents: 226
diff changeset
165 parent => $prentResource,
sergey
parents: 226
diff changeset
166 id => 'item-something'
sergey
parents: 226
diff changeset
167 );
sergey
parents: 226
diff changeset
168
229
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
169 my $child = $contract->FetchChildResource('info');
228
431db7034a88 Для синхронизации
andrei <andrei@nap21.upri>
parents: 227
diff changeset
170
226
b6cde007a175 Added resource contract
sergey
parents:
diff changeset
171 =end code
b6cde007a175 Added resource contract
sergey
parents:
diff changeset
172
b6cde007a175 Added resource contract
sergey
parents:
diff changeset
173 =head1 DESCRIPTION
b6cde007a175 Added resource contract
sergey
parents:
diff changeset
174
229
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 не использует их напрямую, до тех пор пока не требуется изменить стандартный
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
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
180
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
181 Ресурс представляе собой набор контрактов операций, соответствующих методам
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
182 C<HTTP> которые доступны у данного ресурса. Кроме операций ресурс состоит из
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
183 дочерних ресурсов, которые могут соответствовать регулярным выражениям, либо
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
184 иметь фиксированные имена.
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
185
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
186 Каждая операция над ресурсом C<IMPL::Web::Application::OperationContract>
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 контроллер запросов C<HTTP> сможет выполнить операцию. Контракт может создавать
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
193 дочерние ресурсы, на основе указанного родительского ресурса и идетификатора
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 =head2 Динамический контракт
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 реализовывать метод C<CreateResource(%args)>.
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 Результатом выполнения этого метода должен быть Web-ресурс, см.
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
204 C<IMPL::Web::Application::Resource>. Другими словами не существует жесткого
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 Таким образом можно реализовать контракт, который выполняет роль посредника,
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 =begin code
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 package My::Web::Application::ContractMapper;
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
215 use strict;
230
6d8092d8ce1b *reworked IMPL::Security
sergey
parents: 229
diff changeset
216 use IMPL::Const qw(:prop);
229
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
217 use IMPL::declare {
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
218 require => {
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
219 ForbiddenException => 'IMPL::Web::Forbidden'
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 base => [
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
222 'IMPL::Object' => undef,
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
223 'IMPL::Object::Autofill' => '@_'
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 props => [
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
226 map => PROP_GET | PROP_OWNERSET
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
227 ]
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
228 }
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
229
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
230 sub CreateResource {
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
231 my ($this,%args) = @_;
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
232
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
233 my $type = ref $args{model} || '_default';
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
234
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
235 my $contract = $this->map->{$type};
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
236
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
237 die ForbiddenException->new()
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
238 unless $contract;
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 return $contract->CreateResource(%args);
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
241 }
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
242
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
243 =end code
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
244
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
245 =head1 MEMBERS
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
246
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
247 =head2 C<CTOR(%args)>
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
248
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
249 =over
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 =item * C<resourceFactory>
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 Фабрика объектов C<IMPL::Object::Factory> которая будет использоваться при
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
254 создании новых ресурсов. По-умолчанию C<IMPL::Web::Application::Resource>.
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 =item * C<operations>
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 Хеш с доступными действиями над C<HTTP> ресурсом, ключом является имя ресурса,
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
259 значением C<IMPL::Web::Application::OperationContract>.
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
260
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
261 =item * C<resources>
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
262
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 ресурс и связать его с контрактом. Ниже преведено описание элементов хеша.
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
265
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
266 =over
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
267
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
268 =item * C<name>
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
269
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
270 Имя дочернегно ресурса.
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<match>
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 =item * C<bind>
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
277
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 передается родительский объект, далее передаются граппы из регулярного
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 элемента C<match>, либо имя ресурса, если это ресурс с именем, указанным в
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
282 элементе C<name>.
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
283
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
284 =item * C<contract>
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 Ссылка на C<IMPL::Web::Application::ResourceContract> для дочернего ресурса.
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
287 У данного контракта используется только метод C<CreateContract>.
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 =back
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 По крайней мере C<name> или C<match> должны присутсвовать.
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
292
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
293 =back
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 =head2 C<CreateResource(%args)>
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<%args> будут переданы напрямую констркутору
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
298 ресурса, для создания ресурса используется фабрика C<resourceFactory>.
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
299 При создании, конгструктору ресурса, будет передана ссылка на текущй контракт.
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
300
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
301 По-сути никакого дополнительного функционала данный метод не несет.
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
302
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
303 =head2 C<FindChildResourceInfo($childId)>
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
304
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<($info,$childIdParts)>
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 =over
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 =item * C<$info>
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 Информация о контракте дочернего ресурса, как правило это ссылка на хеш, похожий
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
313 по формату на
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 =back
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 =head2 C<[get]verbs>
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
318
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
319 Хеш с доступными действиями над C<HTTP> ресурсом, все имена операций приведены
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
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
322 =begin code
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
323
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
324 my $result = $contract->verbs->{get}->Invoke($resource,$action);
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
325
47f77e6409f7 heavily reworked the resource model of the web application:
sergey
parents: 228
diff changeset
326 =end code
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 =cut