Mercurial > pub > Impl
comparison Lib/IMPL/Object/Factory.pm @ 194:4d0e1962161c
Replaced tabs with spaces
IMPL::Web::View - fixed document model, new features (control classes, document constructor parameters)
author | cin |
---|---|
date | Tue, 10 Apr 2012 20:08:29 +0400 |
parents | 6c0fee769b0c |
children | 5c82eec23bb6 |
comparison
equal
deleted
inserted
replaced
193:8e8401c0aea4 | 194:4d0e1962161c |
---|---|
4 use parent qw(IMPL::Object IMPL::Object::Serializable); | 4 use parent qw(IMPL::Object IMPL::Object::Serializable); |
5 | 5 |
6 use IMPL::lang qw(:declare :constants); | 6 use IMPL::lang qw(:declare :constants); |
7 | 7 |
8 BEGIN { | 8 BEGIN { |
9 public property factory => PROP_GET | PROP_OWNERSET; | 9 public property factory => PROP_GET | PROP_OWNERSET; |
10 public property parameters => PROP_GET | PROP_OWNERSET; | 10 public property parameters => PROP_GET | PROP_OWNERSET; |
11 public property method => PROP_GET | PROP_OWNERSET; | 11 public property method => PROP_GET | PROP_OWNERSET; |
12 } | 12 } |
13 | 13 |
14 # custom factory, overrides default | 14 # custom factory, overrides default |
15 sub new { | 15 sub new { |
16 my $self = shift; | 16 my $self = shift; |
17 | 17 |
18 return ref $self ? $self->CreateObject(@_) : $self->IMPL::Object::new(@_); | 18 return ref $self ? $self->CreateObject(@_) : $self->IMPL::Object::new(@_); |
19 } | 19 } |
20 | 20 |
21 sub CTOR { | 21 sub CTOR { |
22 my ($this,$factory,$parameters,$method) = @_; | 22 my ($this,$factory,$parameters,$method) = @_; |
23 | 23 |
24 $this->factory($factory) or die new IMPL::InvalidArgumentException("The argument 'factory' is mandatory"); | 24 $this->factory($factory) or die new IMPL::InvalidArgumentException("The argument 'factory' is mandatory"); |
25 $this->parameters($parameters) if $parameters; | 25 $this->parameters($parameters) if $parameters; |
26 $this->method($method) if $method; | 26 $this->method($method) if $method; |
27 } | 27 } |
28 | 28 |
29 # override default restore method | 29 # override default restore method |
30 sub restore { | 30 sub restore { |
31 my ($class,$data,$surrogate) = @_; | 31 my ($class,$data,$surrogate) = @_; |
32 | 32 |
33 my %args = @$data; | 33 my %args = @$data; |
34 | 34 |
35 if ($surrogate) { | 35 if ($surrogate) { |
36 $surrogate->self::CTOR($args{factory},$args{parameters},$args{method}); | 36 $surrogate->self::CTOR($args{factory},$args{parameters},$args{method}); |
37 return $surrogate; | 37 return $surrogate; |
38 } else { | 38 } else { |
39 return $class->new($args{factory},$args{parameters},$args{method}); | 39 return $class->new($args{factory},$args{parameters},$args{method}); |
40 } | 40 } |
41 } | 41 } |
42 | 42 |
43 sub CreateObject { | 43 sub CreateObject { |
44 my $this = shift; | 44 my $this = shift; |
45 | 45 |
46 if (my $method = $this->method) { | 46 if (my $method = $this->method) { |
47 $this->factory->$method($this->MergeParameters(@_)); | 47 $this->factory->$method($this->MergeParameters(@_)); |
48 } else { | 48 } else { |
49 $this->factory->new($this->MergeParameters(@_)); | 49 $this->factory->new($this->MergeParameters(@_)); |
50 } | 50 } |
51 } | 51 } |
52 | 52 |
53 sub MergeParameters { | 53 sub MergeParameters { |
54 my $this = shift; | 54 my $this = shift; |
55 | 55 |
56 $this->parameters ? (_as_list($this->parameters),@_) : @_; | 56 $this->parameters ? (_as_list($this->parameters),@_) : @_; |
57 } | 57 } |
58 | 58 |
59 | 59 |
60 sub _as_list { | 60 sub _as_list { |
61 ref $_[0] ? | 61 ref $_[0] ? |
62 (ref $_[0] eq 'HASH' ? | 62 (ref $_[0] eq 'HASH' ? |
63 %{$_[0]} | 63 %{$_[0]} |
64 : | 64 : |
65 (ref $_[0] eq 'ARRAY'? | 65 (ref $_[0] eq 'ARRAY'? |
66 @{$_[0]} | 66 @{$_[0]} |
67 : | 67 : |
68 $_[0] | 68 $_[0] |
69 ) | 69 ) |
70 ) | 70 ) |
71 : | 71 : |
72 ($_[0]); | 72 ($_[0]); |
73 } | 73 } |
74 | 74 |
75 | 75 |
76 1; | 76 1; |
77 | 77 |
82 =head1 SYNOPSIS | 82 =head1 SYNOPSIS |
83 | 83 |
84 =begin code | 84 =begin code |
85 | 85 |
86 my $factory = new IMPL::Object::Factory( | 86 my $factory = new IMPL::Object::Factory( |
87 'MyApp::User', | 87 'MyApp::User', |
88 { | 88 { |
89 isAdmin => 1 | 89 isAdmin => 1 |
90 } | 90 } |
91 ); | 91 ); |
92 | 92 |
93 my $class = 'MyApp::User'; | 93 my $class = 'MyApp::User'; |
94 | 94 |
95 my $user; | 95 my $user; |
96 | 96 |
97 $user = $class->new(name => 'nobody'); # will create object MyApp::User | 97 $user = $class->new(name => 'nobody'); # will create object MyApp::User |
98 # and pass parameters (name=>'nobody') | 98 # and pass parameters (name=>'nobody') |
99 | 99 |
100 $user = $factory->new(name => 'root'); # will create object MyApp::User | 100 $user = $factory->new(name => 'root'); # will create object MyApp::User |
101 # and pass paremeters (isAdmin => 1, name => 'root') | 101 # and pass paremeters (isAdmin => 1, name => 'root') |
102 | 102 |
103 =end code | 103 =end code |
104 | 104 |
105 Или сериализованная форма в XML. | 105 Или сериализованная форма в XML. |
106 | 106 |
107 =begin code xml | 107 =begin code xml |
108 | 108 |
109 <factory type="IMPL::Object::Factory"> | 109 <factory type="IMPL::Object::Factory"> |
110 <factory>MyApp::User</factory>, | 110 <factory>MyApp::User</factory>, |
111 <parameters type="HASH"> | 111 <parameters type="HASH"> |
112 <isAdmin>1</isAdmin> | 112 <isAdmin>1</isAdmin> |
113 </parameters> | 113 </parameters> |
114 </factory> | 114 </factory> |
115 | 115 |
116 =end code xml | 116 =end code xml |
117 | 117 |
118 =head1 DESCRIPTION | 118 =head1 DESCRIPTION |
189 из свойства C<parameters> и списка C<@params>. Ниже приведен упрощенный пример, как это происходит. | 189 из свойства C<parameters> и списка C<@params>. Ниже приведен упрощенный пример, как это происходит. |
190 | 190 |
191 =begin code | 191 =begin code |
192 | 192 |
193 sub new { | 193 sub new { |
194 my ($this,@params) = @_; | 194 my ($this,@params) = @_; |
195 | 195 |
196 my $method = $this->method || 'new'; | 196 my $method = $this->method || 'new'; |
197 | 197 |
198 return $this->factory->$method(_as_list($this->parameters), @params); | 198 return $this->factory->$method(_as_list($this->parameters), @params); |
199 } | 199 } |
200 | 200 |
201 =end code | 201 =end code |
202 | 202 |
203 =back | 203 =back |