annotate Lib/IMPL/Object/Factory.pm @ 206:c8fe3f84feba

+IMPL::Web::Handlers::ViewSelector +IMPL::Web::Handlers::ErrorHandler *IMPL::Web::Handlers::RestController moved types mappings to ViewSelector
author sergey
date Thu, 03 May 2012 16:48:39 +0400
parents 4d0e1962161c
children 5c82eec23bb6
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
166
4267a2ac3d46 Added Class::Template,
wizard
parents: 114
diff changeset
4 use parent qw(IMPL::Object IMPL::Object::Serializable);
64
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
5
181
47dac58691ee New templating system, small fixes
sourcer
parents: 180
diff changeset
6 use IMPL::lang qw(:declare :constants);
64
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
7
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
8 BEGIN {
194
4d0e1962161c Replaced tabs with spaces
cin
parents: 186
diff changeset
9 public property factory => PROP_GET | PROP_OWNERSET;
4d0e1962161c Replaced tabs with spaces
cin
parents: 186
diff changeset
10 public property parameters => PROP_GET | PROP_OWNERSET;
4d0e1962161c Replaced tabs with spaces
cin
parents: 186
diff changeset
11 public property method => PROP_GET | PROP_OWNERSET;
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 {
194
4d0e1962161c Replaced tabs with spaces
cin
parents: 186
diff changeset
16 my $self = shift;
4d0e1962161c Replaced tabs with spaces
cin
parents: 186
diff changeset
17
4d0e1962161c Replaced tabs with spaces
cin
parents: 186
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 {
194
4d0e1962161c Replaced tabs with spaces
cin
parents: 186
diff changeset
22 my ($this,$factory,$parameters,$method) = @_;
4d0e1962161c Replaced tabs with spaces
cin
parents: 186
diff changeset
23
4d0e1962161c Replaced tabs with spaces
cin
parents: 186
diff changeset
24 $this->factory($factory) or die new IMPL::InvalidArgumentException("The argument 'factory' is mandatory");
4d0e1962161c Replaced tabs with spaces
cin
parents: 186
diff changeset
25 $this->parameters($parameters) if $parameters;
4d0e1962161c Replaced tabs with spaces
cin
parents: 186
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 {
194
4d0e1962161c Replaced tabs with spaces
cin
parents: 186
diff changeset
31 my ($class,$data,$surrogate) = @_;
4d0e1962161c Replaced tabs with spaces
cin
parents: 186
diff changeset
32
4d0e1962161c Replaced tabs with spaces
cin
parents: 186
diff changeset
33 my %args = @$data;
4d0e1962161c Replaced tabs with spaces
cin
parents: 186
diff changeset
34
4d0e1962161c Replaced tabs with spaces
cin
parents: 186
diff changeset
35 if ($surrogate) {
4d0e1962161c Replaced tabs with spaces
cin
parents: 186
diff changeset
36 $surrogate->self::CTOR($args{factory},$args{parameters},$args{method});
4d0e1962161c Replaced tabs with spaces
cin
parents: 186
diff changeset
37 return $surrogate;
4d0e1962161c Replaced tabs with spaces
cin
parents: 186
diff changeset
38 } else {
4d0e1962161c Replaced tabs with spaces
cin
parents: 186
diff changeset
39 return $class->new($args{factory},$args{parameters},$args{method});
4d0e1962161c Replaced tabs with spaces
cin
parents: 186
diff changeset
40 }
64
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 {
194
4d0e1962161c Replaced tabs with spaces
cin
parents: 186
diff changeset
44 my $this = shift;
4d0e1962161c Replaced tabs with spaces
cin
parents: 186
diff changeset
45
4d0e1962161c Replaced tabs with spaces
cin
parents: 186
diff changeset
46 if (my $method = $this->method) {
4d0e1962161c Replaced tabs with spaces
cin
parents: 186
diff changeset
47 $this->factory->$method($this->MergeParameters(@_));
4d0e1962161c Replaced tabs with spaces
cin
parents: 186
diff changeset
48 } else {
4d0e1962161c Replaced tabs with spaces
cin
parents: 186
diff changeset
49 $this->factory->new($this->MergeParameters(@_));
4d0e1962161c Replaced tabs with spaces
cin
parents: 186
diff changeset
50 }
64
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
51 }
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
52
181
47dac58691ee New templating system, small fixes
sourcer
parents: 180
diff changeset
53 sub MergeParameters {
194
4d0e1962161c Replaced tabs with spaces
cin
parents: 186
diff changeset
54 my $this = shift;
4d0e1962161c Replaced tabs with spaces
cin
parents: 186
diff changeset
55
4d0e1962161c Replaced tabs with spaces
cin
parents: 186
diff changeset
56 $this->parameters ? (_as_list($this->parameters),@_) : @_;
181
47dac58691ee New templating system, small fixes
sourcer
parents: 180
diff changeset
57 }
47dac58691ee New templating system, small fixes
sourcer
parents: 180
diff changeset
58
47dac58691ee New templating system, small fixes
sourcer
parents: 180
diff changeset
59
64
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
60 sub _as_list {
194
4d0e1962161c Replaced tabs with spaces
cin
parents: 186
diff changeset
61 ref $_[0] ?
4d0e1962161c Replaced tabs with spaces
cin
parents: 186
diff changeset
62 (ref $_[0] eq 'HASH' ?
4d0e1962161c Replaced tabs with spaces
cin
parents: 186
diff changeset
63 %{$_[0]}
4d0e1962161c Replaced tabs with spaces
cin
parents: 186
diff changeset
64 :
4d0e1962161c Replaced tabs with spaces
cin
parents: 186
diff changeset
65 (ref $_[0] eq 'ARRAY'?
4d0e1962161c Replaced tabs with spaces
cin
parents: 186
diff changeset
66 @{$_[0]}
4d0e1962161c Replaced tabs with spaces
cin
parents: 186
diff changeset
67 :
4d0e1962161c Replaced tabs with spaces
cin
parents: 186
diff changeset
68 $_[0]
4d0e1962161c Replaced tabs with spaces
cin
parents: 186
diff changeset
69 )
4d0e1962161c Replaced tabs with spaces
cin
parents: 186
diff changeset
70 )
4d0e1962161c Replaced tabs with spaces
cin
parents: 186
diff changeset
71 :
4d0e1962161c Replaced tabs with spaces
cin
parents: 186
diff changeset
72 ($_[0]);
64
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
73 }
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
74
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
75
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
76 1;
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
77
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
78 __END__
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
79
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
80 =pod
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
81
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
82 =head1 SYNOPSIS
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
83
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
84 =begin code
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
85
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
86 my $factory = new IMPL::Object::Factory(
194
4d0e1962161c Replaced tabs with spaces
cin
parents: 186
diff changeset
87 'MyApp::User',
4d0e1962161c Replaced tabs with spaces
cin
parents: 186
diff changeset
88 {
4d0e1962161c Replaced tabs with spaces
cin
parents: 186
diff changeset
89 isAdmin => 1
4d0e1962161c Replaced tabs with spaces
cin
parents: 186
diff changeset
90 }
64
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
91 );
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
92
67
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
93 my $class = 'MyApp::User';
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
94
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
95 my $user;
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
96
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
97 $user = $class->new(name => 'nobody'); # will create object MyApp::User
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
98 # and pass parameters (name=>'nobody')
194
4d0e1962161c Replaced tabs with spaces
cin
parents: 186
diff changeset
99
67
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
100 $user = $factory->new(name => 'root'); # will create object MyApp::User
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
101 # and pass paremeters (isAdmin => 1, name => 'root')
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
102
64
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
103 =end code
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
104
180
d1676be8afcc Перекодировка в utf-8
sourcer
parents: 166
diff changeset
105 Или сериализованная форма в XML.
67
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
106
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
107 =begin code xml
64
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
108
67
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
109 <factory type="IMPL::Object::Factory">
194
4d0e1962161c Replaced tabs with spaces
cin
parents: 186
diff changeset
110 <factory>MyApp::User</factory>,
4d0e1962161c Replaced tabs with spaces
cin
parents: 186
diff changeset
111 <parameters type="HASH">
4d0e1962161c Replaced tabs with spaces
cin
parents: 186
diff changeset
112 <isAdmin>1</isAdmin>
4d0e1962161c Replaced tabs with spaces
cin
parents: 186
diff changeset
113 </parameters>
67
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
114 </factory>
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
115
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
116 =end code xml
64
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
117
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
118 =head1 DESCRIPTION
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
119
67
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
120 C<[Serializable]>
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
121
180
d1676be8afcc Перекодировка в utf-8
sourcer
parents: 166
diff changeset
122 Класс, реализующий фабрику классов.
64
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
123
180
d1676be8afcc Перекодировка в utf-8
sourcer
parents: 166
diff changeset
124 Фабрика классов это любой объект, который имеет метод C< new > вызов которого приводит к созданию нового
d1676be8afcc Перекодировка в utf-8
sourcer
parents: 166
diff changeset
125 объекта. Например каждый класс сам явялется фабрикой, поскольку, если у него вызвать метод
d1676be8afcc Перекодировка в utf-8
sourcer
parents: 166
diff changeset
126 C< new >, то будет создан объект. Полученные объекты, в силу механизмов языка Perl, также
d1676be8afcc Перекодировка в utf-8
sourcer
parents: 166
diff changeset
127 являются фабриками, притом такимиже, что и класс.
64
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
128
180
d1676be8afcc Перекодировка в utf-8
sourcer
parents: 166
diff changeset
129 Данный класс меняет поведение метода C< new > в зависимости от контекста вызова: статического
d1676be8afcc Перекодировка в utf-8
sourcer
parents: 166
diff changeset
130 метода или метода объекта. При вызове метода C< new > у класса происходит создание объекта
d1676be8afcc Перекодировка в utf-8
sourcer
parents: 166
diff changeset
131 фабрики с определенными параметрами. Далее объект-фабрика может быть использована для создания
d1676be8afcc Перекодировка в utf-8
sourcer
parents: 166
diff changeset
132 объектов уже на основе параметров фабрики.
64
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
133
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
134 =head1 MEMBERS
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
135
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
136 =over
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
137
73
wizard
parents: 67
diff changeset
138 =item C< CTOR($factory,$parameters,$method) >
67
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
139
180
d1676be8afcc Перекодировка в utf-8
sourcer
parents: 166
diff changeset
140 Создает новый экземпляр фабрики.
67
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
141
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
142 =over
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
143
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
144 =item C<$factory>
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
145
180
d1676be8afcc Перекодировка в utf-8
sourcer
parents: 166
diff changeset
146 Либо имя класса, либо другая фабрика.
67
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
147
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
148 =item C<$parameters>
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
149
180
d1676be8afcc Перекодировка в utf-8
sourcer
parents: 166
diff changeset
150 Ссылка на параметры для создания объектов, может быть ссылкой на хеш, массив и т.д.
67
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
151
180
d1676be8afcc Перекодировка в utf-8
sourcer
parents: 166
diff changeset
152 Если является ссылкой на хеш, то при создании объектов данной фабрикой этот хеш
d1676be8afcc Перекодировка в utf-8
sourcer
parents: 166
diff changeset
153 будет развернут в список и передан параметрами методу C<new>.
67
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
154
180
d1676be8afcc Перекодировка в utf-8
sourcer
parents: 166
diff changeset
155 Если является ссылкой на массив, то при создании объектов данной фабрикой этот массив
d1676be8afcc Перекодировка в utf-8
sourcer
parents: 166
diff changeset
156 будет передан в списк и передан параметрами методу C<new>.
67
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
157
180
d1676be8afcc Перекодировка в utf-8
sourcer
parents: 166
diff changeset
158 Если является любым другим объектом или скаляром, то будет передан параметром методу
d1676be8afcc Перекодировка в utf-8
sourcer
parents: 166
diff changeset
159 C<new> как есть.
67
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
160
73
wizard
parents: 67
diff changeset
161 =item C<$method>
wizard
parents: 67
diff changeset
162
180
d1676be8afcc Перекодировка в utf-8
sourcer
parents: 166
diff changeset
163 Имя метода (или ссылка на процедуру), который будет вызван у C<$factory> при создании
d1676be8afcc Перекодировка в utf-8
sourcer
parents: 166
diff changeset
164 текущей фабрикой нового объекта.
73
wizard
parents: 67
diff changeset
165
67
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
166 =back
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
167
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
168 =item C< [get] factory >
64
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
169
180
d1676be8afcc Перекодировка в utf-8
sourcer
parents: 166
diff changeset
170 Свойство, содержащее фабрику для создание новых объектов текущей фабрикой. Чаще всего оно содержит
d1676be8afcc Перекодировка в utf-8
sourcer
parents: 166
diff changeset
171 имя класса.
64
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
172
67
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
173 =item C< [get] parameters >
64
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
174
180
d1676be8afcc Перекодировка в utf-8
sourcer
parents: 166
diff changeset
175 Свойство, содержит ссылку на параметры для создания объектов, при создании объекта эти параметры будут
d1676be8afcc Перекодировка в utf-8
sourcer
parents: 166
diff changeset
176 развернуты в список и переданы оператору C< new > фабрике из свойства C< factory >, за ними будут
d1676be8afcc Перекодировка в utf-8
sourcer
parents: 166
diff changeset
177 следовать параметры непосредственно текущей фабрики.
67
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
178
181
47dac58691ee New templating system, small fixes
sourcer
parents: 180
diff changeset
179 =item C<MergeParameters(@params)>
47dac58691ee New templating system, small fixes
sourcer
parents: 180
diff changeset
180
47dac58691ee New templating system, small fixes
sourcer
parents: 180
diff changeset
181 Метод смешивающий фиксированные параметры с параметрами переданными методу C<new(@params)>. По умолчанию
47dac58691ee New templating system, small fixes
sourcer
parents: 180
diff changeset
182 добавляет пареметры фабрики в конец к фиксированным параметрам. Для изменения этого поведения требуется
47dac58691ee New templating system, small fixes
sourcer
parents: 180
diff changeset
183 переопределить данный метод. Также этот метод можно переопределить для передачи параметров, значения
47dac58691ee New templating system, small fixes
sourcer
parents: 180
diff changeset
184 которых вычисляются.
47dac58691ee New templating system, small fixes
sourcer
parents: 180
diff changeset
185
67
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
186 =item C<new(@params)>
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
187
180
d1676be8afcc Перекодировка в utf-8
sourcer
parents: 166
diff changeset
188 Создает новый объект, используя свйство C<factory> как фабрику и передавая туда параметры
d1676be8afcc Перекодировка в utf-8
sourcer
parents: 166
diff changeset
189 из свойства C<parameters> и списка C<@params>. Ниже приведен упрощенный пример, как это происходит.
67
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
190
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
191 =begin code
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
192
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
193 sub new {
194
4d0e1962161c Replaced tabs with spaces
cin
parents: 186
diff changeset
194 my ($this,@params) = @_;
4d0e1962161c Replaced tabs with spaces
cin
parents: 186
diff changeset
195
4d0e1962161c Replaced tabs with spaces
cin
parents: 186
diff changeset
196 my $method = $this->method || 'new';
4d0e1962161c Replaced tabs with spaces
cin
parents: 186
diff changeset
197
4d0e1962161c Replaced tabs with spaces
cin
parents: 186
diff changeset
198 return $this->factory->$method(_as_list($this->parameters), @params);
67
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
199 }
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
200
9f5795a10939 Documentation, minor fixes
wizard
parents: 64
diff changeset
201 =end code
64
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
202
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
203 =back
259cd3df6e53 Doc generation
wizard
parents:
diff changeset
204
180
d1676be8afcc Перекодировка в utf-8
sourcer
parents: 166
diff changeset
205 =cut