annotate Lib/IMPL/Web/Application/ResourceContract.pm @ 228:431db7034a88

Для синхронизации
author andrei <andrei@nap21.upri>
date Thu, 13 Sep 2012 17:55:01 +0400
parents 70ad6bc20908
children 47f77e6409f7
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;
b6cde007a175 Added resource contract
sergey
parents:
diff changeset
3 use IMPL::lang qw(:declare);
b6cde007a175 Added resource contract
sergey
parents:
diff changeset
4 use IMPL::declare {
227
sergey
parents: 226
diff changeset
5 require => {
228
431db7034a88 Для синхронизации
andrei <andrei@nap21.upri>
parents: 227
diff changeset
6 'Exception' => 'IMPL::Exception',
431db7034a88 Для синхронизации
andrei <andrei@nap21.upri>
parents: 227
diff changeset
7 'ArgumentException' => '-IMPL::ArgumentException',
431db7034a88 Для синхронизации
andrei <andrei@nap21.upri>
parents: 227
diff changeset
8 'KeyNotFoundException' => '-IMPL::KeyNotFoundException',
227
sergey
parents: 226
diff changeset
9 'ResourceClass' => 'IMPL::Web::Application::Resource'
sergey
parents: 226
diff changeset
10 },
226
b6cde007a175 Added resource contract
sergey
parents:
diff changeset
11 base => [
b6cde007a175 Added resource contract
sergey
parents:
diff changeset
12 'IMPL::Object' => undef
b6cde007a175 Added resource contract
sergey
parents:
diff changeset
13 ]
b6cde007a175 Added resource contract
sergey
parents:
diff changeset
14 };
b6cde007a175 Added resource contract
sergey
parents:
diff changeset
15
b6cde007a175 Added resource contract
sergey
parents:
diff changeset
16 BEGIN {
227
sergey
parents: 226
diff changeset
17 public property resourceFactory => PROP_ALL;
226
b6cde007a175 Added resource contract
sergey
parents:
diff changeset
18 public property operations => PROP_ALL;
b6cde007a175 Added resource contract
sergey
parents:
diff changeset
19 private property _namedResources => PROP_ALL;
228
431db7034a88 Для синхронизации
andrei <andrei@nap21.upri>
parents: 227
diff changeset
20 private property _regexpResources => PROP_ALL | PROP_LIST;
226
b6cde007a175 Added resource contract
sergey
parents:
diff changeset
21 }
b6cde007a175 Added resource contract
sergey
parents:
diff changeset
22
227
sergey
parents: 226
diff changeset
23 sub CTOR {
sergey
parents: 226
diff changeset
24 my $this = shift;
sergey
parents: 226
diff changeset
25 my %args = @_;
sergey
parents: 226
diff changeset
26
sergey
parents: 226
diff changeset
27 $this->resourceFactory( $args{resourceFactory} || ResourceClass );
228
431db7034a88 Для синхронизации
andrei <andrei@nap21.upri>
parents: 227
diff changeset
28
431db7034a88 Для синхронизации
andrei <andrei@nap21.upri>
parents: 227
diff changeset
29 my $resources = $args{resources} || [];
431db7034a88 Для синхронизации
andrei <andrei@nap21.upri>
parents: 227
diff changeset
30 my $operations = $args{operations} || {};
431db7034a88 Для синхронизации
andrei <andrei@nap21.upri>
parents: 227
diff changeset
31
431db7034a88 Для синхронизации
andrei <andrei@nap21.upri>
parents: 227
diff changeset
32 die ArgumentException->new(resources => 'resources parameter must be a reference to an array')
431db7034a88 Для синхронизации
andrei <andrei@nap21.upri>
parents: 227
diff changeset
33 unless ref $resources eq 'ARRAY';
431db7034a88 Для синхронизации
andrei <andrei@nap21.upri>
parents: 227
diff changeset
34
431db7034a88 Для синхронизации
andrei <andrei@nap21.upri>
parents: 227
diff changeset
35 die ArgumentException->new(opearations => 'operations parameter must be a reference to a hash')
431db7034a88 Для синхронизации
andrei <andrei@nap21.upri>
parents: 227
diff changeset
36 unless ref $operations eq 'HASH';
431db7034a88 Для синхронизации
andrei <andrei@nap21.upri>
parents: 227
diff changeset
37
431db7034a88 Для синхронизации
andrei <andrei@nap21.upri>
parents: 227
diff changeset
38 my %nameMap;
431db7034a88 Для синхронизации
andrei <andrei@nap21.upri>
parents: 227
diff changeset
39
431db7034a88 Для синхронизации
andrei <andrei@nap21.upri>
parents: 227
diff changeset
40 foreach my $res (@$resources) {
431db7034a88 Для синхронизации
andrei <andrei@nap21.upri>
parents: 227
diff changeset
41 next unless $res->{contract};
431db7034a88 Для синхронизации
andrei <andrei@nap21.upri>
parents: 227
diff changeset
42 if(my $name = $res->{name}) {
431db7034a88 Для синхронизации
andrei <andrei@nap21.upri>
parents: 227
diff changeset
43 $nameMap{$name} = $res;
431db7034a88 Для синхронизации
andrei <andrei@nap21.upri>
parents: 227
diff changeset
44 }
431db7034a88 Для синхронизации
andrei <andrei@nap21.upri>
parents: 227
diff changeset
45 if($res->{match}) {
431db7034a88 Для синхронизации
andrei <andrei@nap21.upri>
parents: 227
diff changeset
46 $this->_regexpResources->Append($res);
431db7034a88 Для синхронизации
andrei <andrei@nap21.upri>
parents: 227
diff changeset
47 }
431db7034a88 Для синхронизации
andrei <andrei@nap21.upri>
parents: 227
diff changeset
48 }
431db7034a88 Для синхронизации
andrei <andrei@nap21.upri>
parents: 227
diff changeset
49
431db7034a88 Для синхронизации
andrei <andrei@nap21.upri>
parents: 227
diff changeset
50 $this->_namedResources(\%nameMap);
227
sergey
parents: 226
diff changeset
51 }
sergey
parents: 226
diff changeset
52
sergey
parents: 226
diff changeset
53 sub CreateResource {
228
431db7034a88 Для синхронизации
andrei <andrei@nap21.upri>
parents: 227
diff changeset
54 my $this = shift;
431db7034a88 Для синхронизации
andrei <andrei@nap21.upri>
parents: 227
diff changeset
55 my %args = @_;
431db7034a88 Для синхронизации
andrei <andrei@nap21.upri>
parents: 227
diff changeset
56
431db7034a88 Для синхронизации
andrei <andrei@nap21.upri>
parents: 227
diff changeset
57 return $this->resourceFactory->new (
431db7034a88 Для синхронизации
andrei <andrei@nap21.upri>
parents: 227
diff changeset
58 %args,
431db7034a88 Для синхронизации
andrei <andrei@nap21.upri>
parents: 227
diff changeset
59 contract => $this
431db7034a88 Для синхронизации
andrei <andrei@nap21.upri>
parents: 227
diff changeset
60 );
431db7034a88 Для синхронизации
andrei <andrei@nap21.upri>
parents: 227
diff changeset
61 }
431db7034a88 Для синхронизации
andrei <andrei@nap21.upri>
parents: 227
diff changeset
62
431db7034a88 Для синхронизации
andrei <andrei@nap21.upri>
parents: 227
diff changeset
63 sub FindChildResourceContractInfo {
431db7034a88 Для синхронизации
andrei <andrei@nap21.upri>
parents: 227
diff changeset
64 my ($this,$name) = @_;
431db7034a88 Для синхронизации
andrei <andrei@nap21.upri>
parents: 227
diff changeset
65
431db7034a88 Для синхронизации
andrei <andrei@nap21.upri>
parents: 227
diff changeset
66 if(my $contract = $this->_namedResources->{$name}) {
431db7034a88 Для синхронизации
andrei <andrei@nap21.upri>
parents: 227
diff changeset
67 return $contract;
431db7034a88 Для синхронизации
andrei <andrei@nap21.upri>
parents: 227
diff changeset
68 } else {
431db7034a88 Для синхронизации
andrei <andrei@nap21.upri>
parents: 227
diff changeset
69 foreach my $info ( $this->_regexpResources ) {
431db7034a88 Для синхронизации
andrei <andrei@nap21.upri>
parents: 227
diff changeset
70 my $rx = $info->{match};
431db7034a88 Для синхронизации
andrei <andrei@nap21.upri>
parents: 227
diff changeset
71 return $info if $name =~ m/$rx/;
431db7034a88 Для синхронизации
andrei <andrei@nap21.upri>
parents: 227
diff changeset
72 }
431db7034a88 Для синхронизации
andrei <andrei@nap21.upri>
parents: 227
diff changeset
73 }
431db7034a88 Для синхронизации
andrei <andrei@nap21.upri>
parents: 227
diff changeset
74
431db7034a88 Для синхронизации
andrei <andrei@nap21.upri>
parents: 227
diff changeset
75 return undef;
431db7034a88 Для синхронизации
andrei <andrei@nap21.upri>
parents: 227
diff changeset
76 }
431db7034a88 Для синхронизации
andrei <andrei@nap21.upri>
parents: 227
diff changeset
77
431db7034a88 Для синхронизации
andrei <andrei@nap21.upri>
parents: 227
diff changeset
78 sub CreateChildResource {
431db7034a88 Для синхронизации
andrei <andrei@nap21.upri>
parents: 227
diff changeset
79 my $this = @_;
431db7034a88 Для синхронизации
andrei <andrei@nap21.upri>
parents: 227
diff changeset
80 my %args = @_;
431db7034a88 Для синхронизации
andrei <andrei@nap21.upri>
parents: 227
diff changeset
81
431db7034a88 Для синхронизации
andrei <andrei@nap21.upri>
parents: 227
diff changeset
82 my $id = $args{id} or die ArgumentException( id => 'id parameter must be specified');
431db7034a88 Для синхронизации
andrei <andrei@nap21.upri>
parents: 227
diff changeset
83 my $parent = $args{parent};
431db7034a88 Для синхронизации
andrei <andrei@nap21.upri>
parents: 227
diff changeset
84 my $model = $parent->model if $parent;
431db7034a88 Для синхронизации
andrei <andrei@nap21.upri>
parents: 227
diff changeset
85 my $binding, $childContract, @bindingVars;
431db7034a88 Для синхронизации
andrei <andrei@nap21.upri>
parents: 227
diff changeset
86
431db7034a88 Для синхронизации
andrei <andrei@nap21.upri>
parents: 227
diff changeset
87 if(my $info = $this->_namedResources->{$id}) {
431db7034a88 Для синхронизации
andrei <andrei@nap21.upri>
parents: 227
diff changeset
88 @bindingVars = ($id);
431db7034a88 Для синхронизации
andrei <andrei@nap21.upri>
parents: 227
diff changeset
89 $childContract = $info->{contract};
431db7034a88 Для синхронизации
andrei <andrei@nap21.upri>
parents: 227
diff changeset
90 $binding = $info->{bind};
431db7034a88 Для синхронизации
andrei <andrei@nap21.upri>
parents: 227
diff changeset
91 } else {
431db7034a88 Для синхронизации
andrei <andrei@nap21.upri>
parents: 227
diff changeset
92 foreach my $info ( $this->_regexpResources ) {
431db7034a88 Для синхронизации
andrei <andrei@nap21.upri>
parents: 227
diff changeset
93 my $rx = $info->{match};
431db7034a88 Для синхронизации
andrei <andrei@nap21.upri>
parents: 227
diff changeset
94 next unless $rx;
431db7034a88 Для синхронизации
andrei <andrei@nap21.upri>
parents: 227
diff changeset
95 if( @bindingVars = ($id =~ m/$rx/) ) {
431db7034a88 Для синхронизации
andrei <andrei@nap21.upri>
parents: 227
diff changeset
96 $childContract = $info->{contract};
431db7034a88 Для синхронизации
andrei <andrei@nap21.upri>
parents: 227
diff changeset
97 $binding = $info->{bind};
431db7034a88 Для синхронизации
andrei <andrei@nap21.upri>
parents: 227
diff changeset
98 }
431db7034a88 Для синхронизации
andrei <andrei@nap21.upri>
parents: 227
diff changeset
99 }
431db7034a88 Для синхронизации
andrei <andrei@nap21.upri>
parents: 227
diff changeset
100 }
431db7034a88 Для синхронизации
andrei <andrei@nap21.upri>
parents: 227
diff changeset
101
431db7034a88 Для синхронизации
andrei <andrei@nap21.upri>
parents: 227
diff changeset
102 if ($childContract) {
431db7034a88 Для синхронизации
andrei <andrei@nap21.upri>
parents: 227
diff changeset
103 my $childModel = $binding ? $binding->($parent,$model,@bindingVars) : undef;
431db7034a88 Для синхронизации
andrei <andrei@nap21.upri>
parents: 227
diff changeset
104
431db7034a88 Для синхронизации
andrei <andrei@nap21.upri>
parents: 227
diff changeset
105 return $childContract->CreateResource(
431db7034a88 Для синхронизации
andrei <andrei@nap21.upri>
parents: 227
diff changeset
106 %args,
431db7034a88 Для синхронизации
andrei <andrei@nap21.upri>
parents: 227
diff changeset
107 model => $childModel
431db7034a88 Для синхронизации
andrei <andrei@nap21.upri>
parents: 227
diff changeset
108 );
431db7034a88 Для синхронизации
andrei <andrei@nap21.upri>
parents: 227
diff changeset
109 } else {
431db7034a88 Для синхронизации
andrei <andrei@nap21.upri>
parents: 227
diff changeset
110 die KeyNotFoundException->new($id);
431db7034a88 Для синхронизации
andrei <andrei@nap21.upri>
parents: 227
diff changeset
111 }
227
sergey
parents: 226
diff changeset
112 }
sergey
parents: 226
diff changeset
113
226
b6cde007a175 Added resource contract
sergey
parents:
diff changeset
114 1;
b6cde007a175 Added resource contract
sergey
parents:
diff changeset
115
b6cde007a175 Added resource contract
sergey
parents:
diff changeset
116 __END__
b6cde007a175 Added resource contract
sergey
parents:
diff changeset
117
b6cde007a175 Added resource contract
sergey
parents:
diff changeset
118 =pod
b6cde007a175 Added resource contract
sergey
parents:
diff changeset
119
b6cde007a175 Added resource contract
sergey
parents:
diff changeset
120 =head1 NAME
b6cde007a175 Added resource contract
sergey
parents:
diff changeset
121
b6cde007a175 Added resource contract
sergey
parents:
diff changeset
122 C<IMPL::Web::Application::ResourceContract> - описание ресурса
b6cde007a175 Added resource contract
sergey
parents:
diff changeset
123
b6cde007a175 Added resource contract
sergey
parents:
diff changeset
124 =head1 SYNIOSIS
b6cde007a175 Added resource contract
sergey
parents:
diff changeset
125
b6cde007a175 Added resource contract
sergey
parents:
diff changeset
126 =begin code
b6cde007a175 Added resource contract
sergey
parents:
diff changeset
127
b6cde007a175 Added resource contract
sergey
parents:
diff changeset
128 use IMPL::require {
b6cde007a175 Added resource contract
sergey
parents:
diff changeset
129 ResourceContract => 'IMPL::Web::Application::ResourceContract',
b6cde007a175 Added resource contract
sergey
parents:
diff changeset
130 OperationContract => 'IMPL::Web::Application::OperationContract'
b6cde007a175 Added resource contract
sergey
parents:
diff changeset
131 };
b6cde007a175 Added resource contract
sergey
parents:
diff changeset
132
b6cde007a175 Added resource contract
sergey
parents:
diff changeset
133 my $contract = ResourceContract->new(
b6cde007a175 Added resource contract
sergey
parents:
diff changeset
134 operations => {
b6cde007a175 Added resource contract
sergey
parents:
diff changeset
135 get => OperationContract->new(
b6cde007a175 Added resource contract
sergey
parents:
diff changeset
136 bind => sub {
b6cde007a175 Added resource contract
sergey
parents:
diff changeset
137 return "Hello!";
b6cde007a175 Added resource contract
sergey
parents:
diff changeset
138 }
b6cde007a175 Added resource contract
sergey
parents:
diff changeset
139 )
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',
b6cde007a175 Added resource contract
sergey
parents:
diff changeset
144 bind => sub {
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(
b6cde007a175 Added resource contract
sergey
parents:
diff changeset
148 get => OperationContract->new(
b6cde007a175 Added resource contract
sergey
parents:
diff changeset
149 bind => sub {
b6cde007a175 Added resource contract
sergey
parents:
diff changeset
150 my ($resource,$model) = @_;
b6cde007a175 Added resource contract
sergey
parents:
diff changeset
151 return $model; # or the same: $resource->model;
b6cde007a175 Added resource contract
sergey
parents:
diff changeset
152 }
b6cde007a175 Added resource contract
sergey
parents:
diff changeset
153 )
b6cde007a175 Added resource contract
sergey
parents:
diff changeset
154 )
b6cde007a175 Added resource contract
sergey
parents:
diff changeset
155 }
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
227
sergey
parents: 226
diff changeset
159 my $obj = My::App::Data->fetch('something');
sergey
parents: 226
diff changeset
160
sergey
parents: 226
diff changeset
161 my $resource = $contract->CreateResource(
sergey
parents: 226
diff changeset
162 model => $obj,
sergey
parents: 226
diff changeset
163 parent => $prentResource,
sergey
parents: 226
diff changeset
164 id => 'item-something'
sergey
parents: 226
diff changeset
165 );
sergey
parents: 226
diff changeset
166
228
431db7034a88 Для синхронизации
andrei <andrei@nap21.upri>
parents: 227
diff changeset
167 my $child = $contract->CreateChildResource(
431db7034a88 Для синхронизации
andrei <andrei@nap21.upri>
parents: 227
diff changeset
168 parent => $resource,
431db7034a88 Для синхронизации
andrei <andrei@nap21.upri>
parents: 227
diff changeset
169 id => 'info'
431db7034a88 Для синхронизации
andrei <andrei@nap21.upri>
parents: 227
diff changeset
170 );
431db7034a88 Для синхронизации
andrei <andrei@nap21.upri>
parents: 227
diff changeset
171
226
b6cde007a175 Added resource contract
sergey
parents:
diff changeset
172 =end code
b6cde007a175 Added resource contract
sergey
parents:
diff changeset
173
b6cde007a175 Added resource contract
sergey
parents:
diff changeset
174 =head1 DESCRIPTION
b6cde007a175 Added resource contract
sergey
parents:
diff changeset
175
b6cde007a175 Added resource contract
sergey
parents:
diff changeset
176 =cut