comparison 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
comparison
equal deleted inserted replaced
227:70ad6bc20908 228:431db7034a88
1 package IMPL::Web::Application::ResourceContract; 1 package IMPL::Web::Application::ResourceContract;
2 use strict; 2 use strict;
3 use IMPL::lang qw(:declare); 3 use IMPL::lang qw(:declare);
4 use IMPL::declare { 4 use IMPL::declare {
5 require => { 5 require => {
6 'Exception' => 'IMPL::Exception',
7 'ArgumentException' => '-IMPL::ArgumentException',
8 'KeyNotFoundException' => '-IMPL::KeyNotFoundException',
6 'ResourceClass' => 'IMPL::Web::Application::Resource' 9 'ResourceClass' => 'IMPL::Web::Application::Resource'
7 }, 10 },
8 base => [ 11 base => [
9 'IMPL::Object' => undef 12 'IMPL::Object' => undef
10 ] 13 ]
12 15
13 BEGIN { 16 BEGIN {
14 public property resourceFactory => PROP_ALL; 17 public property resourceFactory => PROP_ALL;
15 public property operations => PROP_ALL; 18 public property operations => PROP_ALL;
16 private property _namedResources => PROP_ALL; 19 private property _namedResources => PROP_ALL;
17 private property _regexpResources => PROP_ALL; 20 private property _regexpResources => PROP_ALL | PROP_LIST;
18 } 21 }
19 22
20 sub CTOR { 23 sub CTOR {
21 my $this = shift; 24 my $this = shift;
22 my %args = @_; 25 my %args = @_;
23 26
24 $this->resourceFactory( $args{resourceFactory} || ResourceClass ); 27 $this->resourceFactory( $args{resourceFactory} || ResourceClass );
28
29 my $resources = $args{resources} || [];
30 my $operations = $args{operations} || {};
31
32 die ArgumentException->new(resources => 'resources parameter must be a reference to an array')
33 unless ref $resources eq 'ARRAY';
34
35 die ArgumentException->new(opearations => 'operations parameter must be a reference to a hash')
36 unless ref $operations eq 'HASH';
37
38 my %nameMap;
39
40 foreach my $res (@$resources) {
41 next unless $res->{contract};
42 if(my $name = $res->{name}) {
43 $nameMap{$name} = $res;
44 }
45 if($res->{match}) {
46 $this->_regexpResources->Append($res);
47 }
48 }
49
50 $this->_namedResources(\%nameMap);
25 } 51 }
26 52
27 sub CreateResource { 53 sub CreateResource {
28 my ($this,) 54 my $this = shift;
55 my %args = @_;
56
57 return $this->resourceFactory->new (
58 %args,
59 contract => $this
60 );
61 }
62
63 sub FindChildResourceContractInfo {
64 my ($this,$name) = @_;
65
66 if(my $contract = $this->_namedResources->{$name}) {
67 return $contract;
68 } else {
69 foreach my $info ( $this->_regexpResources ) {
70 my $rx = $info->{match};
71 return $info if $name =~ m/$rx/;
72 }
73 }
74
75 return undef;
76 }
77
78 sub CreateChildResource {
79 my $this = @_;
80 my %args = @_;
81
82 my $id = $args{id} or die ArgumentException( id => 'id parameter must be specified');
83 my $parent = $args{parent};
84 my $model = $parent->model if $parent;
85 my $binding, $childContract, @bindingVars;
86
87 if(my $info = $this->_namedResources->{$id}) {
88 @bindingVars = ($id);
89 $childContract = $info->{contract};
90 $binding = $info->{bind};
91 } else {
92 foreach my $info ( $this->_regexpResources ) {
93 my $rx = $info->{match};
94 next unless $rx;
95 if( @bindingVars = ($id =~ m/$rx/) ) {
96 $childContract = $info->{contract};
97 $binding = $info->{bind};
98 }
99 }
100 }
101
102 if ($childContract) {
103 my $childModel = $binding ? $binding->($parent,$model,@bindingVars) : undef;
104
105 return $childContract->CreateResource(
106 %args,
107 model => $childModel
108 );
109 } else {
110 die KeyNotFoundException->new($id);
111 }
29 } 112 }
30 113
31 1; 114 1;
32 115
33 __END__ 116 __END__
79 model => $obj, 162 model => $obj,
80 parent => $prentResource, 163 parent => $prentResource,
81 id => 'item-something' 164 id => 'item-something'
82 ); 165 );
83 166
167 my $child = $contract->CreateChildResource(
168 parent => $resource,
169 id => 'info'
170 );
171
84 =end code 172 =end code
85 173
86 =head1 DESCRIPTION 174 =head1 DESCRIPTION
87 175
88 =cut 176 =cut