annotate Lib/IMPL/Object/Factory.pm @ 120:41e9d9ea3db5

Merge with 79cdd6c86409806bd1de092d9f0fb2b048775720
author wizard
date Mon, 07 Jun 2010 17:45:14 +0400 (2010-06-07)
parents 7084af955c57
children 4267a2ac3d46
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
64
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
1 package IMPL::Object::Factory;
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
2 use strict;
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
3
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
4 use base qw(IMPL::Object IMPL::Object::Serializable);
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
5
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
6 use IMPL::Class::Property;
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
7
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
8 BEGIN {
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
9 public property factory => prop_get | owner_set;
73
wizard
parents: 67
diff changeset
10 public property parameters => prop_get | owner_set;
wizard
parents: 67
diff changeset
11 public property method => prop_get | owner_set;
64
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
12 }
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
13
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
14 # custom factory, overrides default
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
15 sub new {
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
16 my $self = shift;
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
17
114
7084af955c57 minor changes, more strict code, no bugs fixed, no features added
wizard
parents: 91
diff changeset
18 return ref $self ? $self->CreateObject(@_) : $self->IMPL::Object::new(@_);
64
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
19 }
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
20
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
21 sub CTOR {
73
wizard
parents: 67
diff changeset
22 my ($this,$factory,$parameters,$method) = @_;
64
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
23
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
24 $this->factory($factory) or die new IMPL::InvalidArgumentException("The argument 'factory' is mandatory");
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
25 $this->parameters($parameters) if $parameters;
73
wizard
parents: 67
diff changeset
26 $this->method($method) if $method;
64
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
27 }
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
28
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
29 # override default restore method
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
30 sub restore {
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
31 my ($class,$data,$surrogate) = @_;
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
32
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
33 my %args = @$data;
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
34
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
35 if ($surrogate) {
91
9cb8e730fa86 fixed factory
wizard
parents: 73
diff changeset
36 $surrogate->callCTOR($args{factory},$args{parameters},$args{method});
64
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
37 return $surrogate;
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
38 } else {
91
9cb8e730fa86 fixed factory
wizard
parents: 73
diff changeset
39 return $class->new($args{factory},$args{parameters},$args{method});
64
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
40 }
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
41 }
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
42
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
43 sub CreateObject {
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
44 my $this = shift;
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
45
73
wizard
parents: 67
diff changeset
46 if (my $method = $this->method) {
wizard
parents: 67
diff changeset
47 $this->factory->$method($this->parameters ? (_as_list($this->parameters),@_) : @_);
wizard
parents: 67
diff changeset
48 } else {
wizard
parents: 67
diff changeset
49 $this->factory->new($this->parameters ? (_as_list($this->parameters),@_) : @_);
wizard
parents: 67
diff changeset
50 }
64
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
51 }
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
52
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
53 sub _as_list {
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
54 ref $_[0] ?
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
55 (ref $_[0] eq 'HASH' ?
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
56 %{$_[0]}
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
57 :
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
58 (ref $_[0] eq 'ARRAY'?
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
59 @{$_[0]}
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
60 :
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
61 $_[0]
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
62 )
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
63 )
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
64 :
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
65 ($_[0]);
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
66 }
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
67
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
68
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
69 1;
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
70
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
71 __END__
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
72
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
73 =pod
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
74
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
75 =head1 SYNOPSIS
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
76
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
77 =begin code
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
78
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
79 my $factory = new IMPL::Object::Factory(
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
80 'MyApp::User',
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
81 {
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
82 isAdmin => 1
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
83 }
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
84 );
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
85
67
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
86 my $class = 'MyApp::User';
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
87
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
88 my $user;
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
89
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
90 $user = $class->new(name => 'nobody'); # will create object MyApp::User
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
91 # and pass parameters (name=>'nobody')
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
92
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
93 $user = $factory->new(name => 'root'); # will create object MyApp::User
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
94 # and pass paremeters (isAdmin => 1, name => 'root')
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
95
64
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
96 =end code
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
97
67
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
98 ��� ��������������� ����� � XML.
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
99
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
100 =begin code xml
64
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
101
67
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
102 <factory type="IMPL::Object::Factory">
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
103 <factory>MyApp::User</factory>,
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
104 <parameters type="HASH">
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
105 <isAdmin>1</isAdmin>
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
106 </parameters>
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
107 </factory>
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
108
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
109 =end code xml
64
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
110
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
111 =head1 DESCRIPTION
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
112
67
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
113 C<[Serializable]>
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
114
64
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
115 �����, ����������� ������� �������.
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
116
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
117 ������� ������� ��� ����� ������, ������� ����� ����� C< new > ����� �������� �������� � �������� ������
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
118 �������. �������� ������ ����� ��� �������� ��������, ���������, ���� � ���� ������� �����
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
119 C< new >, �� ����� ������ ������. ���������� �������, � ���� ���������� ����� Perl, �����
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
120 �������� ���������, ������ ��������, ��� � �����.
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
121
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
122 ������ ����� ������ ��������� ������ C< new > � ����������� �� ��������� ������: ������������
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
123 ������ ��� ������ �������. ��� ������ ������ C< new > � ������ ���������� �������� �������
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
124 ������� � ������������� �����������. ����� ������-������� ����� ���� ������������ ��� ��������
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
125 �������� ��� �� ������ ���������� �������.
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
126
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
127 =head1 MEMBERS
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
128
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
129 =over
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
130
73
wizard
parents: 67
diff changeset
131 =item C< CTOR($factory,$parameters,$method) >
67
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
132
73
wizard
parents: 67
diff changeset
133 ������� ����� ��������� �������.
67
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
134
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
135 =over
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
136
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
137 =item C<$factory>
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
138
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
139 ���� ��� ������, ���� ������ �������.
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
140
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
141 =item C<$parameters>
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
142
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
143 ������ �� ��������� ��� �������� ��������, ����� ���� ������� �� ���, ������ � �.�.
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
144
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
145 ���� �������� ������� �� ���, �� ��� �������� �������� ������ �������� ���� ���
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
146 ����� ��������� � ������ � ������� ����������� ������ C<new>.
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
147
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
148 ���� �������� ������� �� ������, �� ��� �������� �������� ������ �������� ���� ������
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
149 ����� ������� � ����� � ������� ����������� ������ C<new>.
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
150
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
151 ���� �������� ����� ������ �������� ��� ��������, �� ����� ������� ���������� ������
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
152 C<new> ��� ����.
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
153
73
wizard
parents: 67
diff changeset
154 =item C<$method>
wizard
parents: 67
diff changeset
155
wizard
parents: 67
diff changeset
156 ��� ������ (��� ������ �� ���������), ������� ����� ������ � C<$factory> ��� ��������
wizard
parents: 67
diff changeset
157 ������� �������� ������ �������.
wizard
parents: 67
diff changeset
158
67
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
159 =back
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
160
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
161 =item C< [get] factory >
64
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
162
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
163 ��������, ���������� ������� ��� �������� ����� �������� ������� ��������. ���� ����� ��� ��������
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
164 ��� ������.
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
165
67
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
166 =item C< [get] parameters >
64
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
167
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
168 ��������, �������� ������ �� ��������� ��� �������� ��������, ��� �������� ������� ��� ��������� �����
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
169 ���������� � ������ � �������� ��������� C< new > ������� �� �������� C< factory >, �� ���� �����
67
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
170 ��������� ��������� ��������������� ������� �������.
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
171
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
172 =item C<new(@params)>
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
173
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
174 ������� ����� ������, ��������� ������� C<factory> ��� ������� � ��������� ���� ���������
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
175 �� �������� C<parameters> � ������ C<@params>. ���� �������� ���������� ������, ��� ��� ����������.
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
176
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
177 =begin code
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
178
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
179 sub new {
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
180 my ($this,@params) = @_;
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
181
73
wizard
parents: 67
diff changeset
182 my $method = $this->method || 'new';
wizard
parents: 67
diff changeset
183
wizard
parents: 67
diff changeset
184 return $this->factory->$method(_as_list($this->parameters), @params);
67
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
185 }
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
186
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
187 =end code
64
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
188
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
189 =back
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
190
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
191 =cut