Mercurial > pub > Impl
annotate Lib/IMPL/Config/Activator.pm @ 364:82b6c967bcf1
sync, working on metadata
author | cin |
---|---|
date | Fri, 29 Nov 2013 16:33:07 +0400 |
parents | 8088779e539d |
children |
rev | line source |
---|---|
59
0f3e369553bd
Rewritten property implementation (probably become slower but more flexible)
wizard
parents:
diff
changeset
|
1 package IMPL::Config::Activator; |
0f3e369553bd
Rewritten property implementation (probably become slower but more flexible)
wizard
parents:
diff
changeset
|
2 use strict; |
0f3e369553bd
Rewritten property implementation (probably become slower but more flexible)
wizard
parents:
diff
changeset
|
3 |
293 | 4 use Scalar::Util qw(reftype); |
5 use IMPL::lang; | |
6 use IMPL::Const qw(:prop); | |
7 use IMPL::declare { | |
294 | 8 require => { |
293 | 9 Loader => 'IMPL::Code::Loader', |
10 Exception => 'IMPL::Exception' | |
294 | 11 }, |
293 | 12 base => [ |
13 'IMPL::Object' => undef, | |
14 'IMPL::Object::Autofill' => '@_', | |
15 'IMPL::Object::PublicSerializable' => undef | |
16 ], | |
17 props => [ | |
18 factory => PROP_RW, | |
19 parameters => PROP_RW, | |
294 | 20 singleCall => PROP_RW, |
21 _object => PROP_RW | |
293 | 22 ] |
23 }; | |
59
0f3e369553bd
Rewritten property implementation (probably become slower but more flexible)
wizard
parents:
diff
changeset
|
24 |
293 | 25 use constant { |
294 | 26 SELF_CLASS => __PACKAGE__, |
293 | 27 }; |
59
0f3e369553bd
Rewritten property implementation (probably become slower but more flexible)
wizard
parents:
diff
changeset
|
28 |
0f3e369553bd
Rewritten property implementation (probably become slower but more flexible)
wizard
parents:
diff
changeset
|
29 sub CTOR { |
0f3e369553bd
Rewritten property implementation (probably become slower but more flexible)
wizard
parents:
diff
changeset
|
30 my $this = shift; |
60
b0c068da93ac
Lazy activation for the configuration objects (final concept)
wizard
parents:
59
diff
changeset
|
31 |
293 | 32 die Exception->new("A factory parameter is required") unless $this->factory; |
59
0f3e369553bd
Rewritten property implementation (probably become slower but more flexible)
wizard
parents:
diff
changeset
|
33 |
0f3e369553bd
Rewritten property implementation (probably become slower but more flexible)
wizard
parents:
diff
changeset
|
34 } |
0f3e369553bd
Rewritten property implementation (probably become slower but more flexible)
wizard
parents:
diff
changeset
|
35 |
0f3e369553bd
Rewritten property implementation (probably become slower but more flexible)
wizard
parents:
diff
changeset
|
36 |
60
b0c068da93ac
Lazy activation for the configuration objects (final concept)
wizard
parents:
59
diff
changeset
|
37 sub activate { |
59
0f3e369553bd
Rewritten property implementation (probably become slower but more flexible)
wizard
parents:
diff
changeset
|
38 my $this = shift; |
0f3e369553bd
Rewritten property implementation (probably become slower but more flexible)
wizard
parents:
diff
changeset
|
39 |
294 | 40 unless ($this->_object) { |
60
b0c068da93ac
Lazy activation for the configuration objects (final concept)
wizard
parents:
59
diff
changeset
|
41 my @args; |
b0c068da93ac
Lazy activation for the configuration objects (final concept)
wizard
parents:
59
diff
changeset
|
42 |
b0c068da93ac
Lazy activation for the configuration objects (final concept)
wizard
parents:
59
diff
changeset
|
43 my $params = $this->parameters; |
294 | 44 if (ref $params eq 'HASH') { |
194 | 45 while ( my ($key,$value) = each %$params ) { |
293 | 46 push @args,$key, is($value,SELF_CLASS) ? $value->activate : $value; |
194 | 47 } |
294 | 48 } elsif (ref $params eq 'ARRAY') { |
293 | 49 push @args, map is($_,SELF_CLASS) ? $_->activate : $_, @$params; |
60
b0c068da93ac
Lazy activation for the configuration objects (final concept)
wizard
parents:
59
diff
changeset
|
50 } else { |
293 | 51 push @args, is($params,SELF_CLASS) ? $params->activate : $params; |
60
b0c068da93ac
Lazy activation for the configuration objects (final concept)
wizard
parents:
59
diff
changeset
|
52 } |
b0c068da93ac
Lazy activation for the configuration objects (final concept)
wizard
parents:
59
diff
changeset
|
53 |
293 | 54 push @args, map is($_,SELF_CLASS) ? $_->activate : $_, @_ if @_; |
60
b0c068da93ac
Lazy activation for the configuration objects (final concept)
wizard
parents:
59
diff
changeset
|
55 |
b0c068da93ac
Lazy activation for the configuration objects (final concept)
wizard
parents:
59
diff
changeset
|
56 my $factory = $this->factory; |
294 | 57 Loader->default->Require($factory) |
58 unless ref($factory); | |
59 | |
60 my $instance = $factory->new(@args); | |
60
b0c068da93ac
Lazy activation for the configuration objects (final concept)
wizard
parents:
59
diff
changeset
|
61 |
294 | 62 $this->_object($instance) |
63 unless $this->singleCall; | |
64 | |
65 return $instance; | |
59
0f3e369553bd
Rewritten property implementation (probably become slower but more flexible)
wizard
parents:
diff
changeset
|
66 } else { |
294 | 67 return $this->_object; |
59
0f3e369553bd
Rewritten property implementation (probably become slower but more flexible)
wizard
parents:
diff
changeset
|
68 } |
0f3e369553bd
Rewritten property implementation (probably become slower but more flexible)
wizard
parents:
diff
changeset
|
69 } |
0f3e369553bd
Rewritten property implementation (probably become slower but more flexible)
wizard
parents:
diff
changeset
|
70 |
180 | 71 1; |
293 | 72 |
73 __END__ | |
74 | |
75 =pod | |
76 | |
77 =head1 NAME | |
78 | |
294 | 79 C<IMPL::Config::Activator> - объект, используемый для получения других объектов. |
80 | |
81 =head1 DESCRIPTION | |
82 | |
83 Служит дополнительным уровнем абстракции в тех случаях, когда нужный объект | |
84 заранее не известен или его создание должно происходить по требованию. | |
85 От обычной фабрики отличается также тем, что рассматривает формальные параметры | |
86 на наличие активаторов и выполняет их при активации. | |
87 | |
88 Кроме того можно указать, что процесс активации должен происходить при каждом | |
89 обращении. | |
293 | 90 |
91 =cut |