Mercurial > pub > Impl
annotate Lib/IMPL/Config/Activator.pm @ 293:15d87ef41764
*IMPL::Config::Activator: refactoring
| author | sergey | 
|---|---|
| date | Wed, 27 Feb 2013 17:37:28 +0400 | 
| parents | c8fe3f84feba | 
| children | 8088779e539d | 
| 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 { | |
| 8 require => [ | |
| 9 Loader => 'IMPL::Code::Loader', | |
| 10 Exception => 'IMPL::Exception' | |
| 11 ], | |
| 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, | |
| 20 object => PROP_RO | |
| 21 ] | |
| 22 }; | |
| 59 
0f3e369553bd
Rewritten property implementation (probably become slower but more flexible)
 wizard parents: diff
changeset | 23 | 
| 293 | 24 use constant { | 
| 25 SELF_CLASS => __PACKAGE__ | |
| 26 }; | |
| 59 
0f3e369553bd
Rewritten property implementation (probably become slower but more flexible)
 wizard parents: diff
changeset | 27 | 
| 
0f3e369553bd
Rewritten property implementation (probably become slower but more flexible)
 wizard parents: diff
changeset | 28 sub CTOR { | 
| 
0f3e369553bd
Rewritten property implementation (probably become slower but more flexible)
 wizard parents: diff
changeset | 29 my $this = shift; | 
| 60 
b0c068da93ac
Lazy activation for the configuration objects (final concept)
 wizard parents: 
59diff
changeset | 30 | 
| 293 | 31 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 | 32 | 
| 
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 | 
| 60 
b0c068da93ac
Lazy activation for the configuration objects (final concept)
 wizard parents: 
59diff
changeset | 36 sub activate { | 
| 59 
0f3e369553bd
Rewritten property implementation (probably become slower but more flexible)
 wizard parents: diff
changeset | 37 my $this = shift; | 
| 
0f3e369553bd
Rewritten property implementation (probably become slower but more flexible)
 wizard parents: diff
changeset | 38 | 
| 60 
b0c068da93ac
Lazy activation for the configuration objects (final concept)
 wizard parents: 
59diff
changeset | 39 unless ($this->object) { | 
| 
b0c068da93ac
Lazy activation for the configuration objects (final concept)
 wizard parents: 
59diff
changeset | 40 my @args; | 
| 
b0c068da93ac
Lazy activation for the configuration objects (final concept)
 wizard parents: 
59diff
changeset | 41 | 
| 
b0c068da93ac
Lazy activation for the configuration objects (final concept)
 wizard parents: 
59diff
changeset | 42 my $params = $this->parameters; | 
| 293 | 43 if (reftype($params) eq 'HASH') { | 
| 194 | 44 while ( my ($key,$value) = each %$params ) { | 
| 293 | 45 push @args,$key, is($value,SELF_CLASS) ? $value->activate : $value; | 
| 194 | 46 } | 
| 293 | 47 } elsif (reftype($params) eq 'ARRAY') { | 
| 48 push @args, map is($_,SELF_CLASS) ? $_->activate : $_, @$params; | |
| 60 
b0c068da93ac
Lazy activation for the configuration objects (final concept)
 wizard parents: 
59diff
changeset | 49 } else { | 
| 293 | 50 push @args, is($params,SELF_CLASS) ? $params->activate : $params; | 
| 60 
b0c068da93ac
Lazy activation for the configuration objects (final concept)
 wizard parents: 
59diff
changeset | 51 } | 
| 
b0c068da93ac
Lazy activation for the configuration objects (final concept)
 wizard parents: 
59diff
changeset | 52 | 
| 293 | 53 push @args, map is($_,SELF_CLASS) ? $_->activate : $_, @_ if @_; | 
| 60 
b0c068da93ac
Lazy activation for the configuration objects (final concept)
 wizard parents: 
59diff
changeset | 54 | 
| 
b0c068da93ac
Lazy activation for the configuration objects (final concept)
 wizard parents: 
59diff
changeset | 55 my $factory = $this->factory; | 
| 293 | 56 Loader->Require($factory) unless ref $factory; | 
| 60 
b0c068da93ac
Lazy activation for the configuration objects (final concept)
 wizard parents: 
59diff
changeset | 57 | 
| 
b0c068da93ac
Lazy activation for the configuration objects (final concept)
 wizard parents: 
59diff
changeset | 58 return $this->object($factory->new(@args)); | 
| 59 
0f3e369553bd
Rewritten property implementation (probably become slower but more flexible)
 wizard parents: diff
changeset | 59 } else { | 
| 194 | 60 return $this->object; | 
| 59 
0f3e369553bd
Rewritten property implementation (probably become slower but more flexible)
 wizard parents: diff
changeset | 61 } | 
| 
0f3e369553bd
Rewritten property implementation (probably become slower but more flexible)
 wizard parents: diff
changeset | 62 } | 
| 
0f3e369553bd
Rewritten property implementation (probably become slower but more flexible)
 wizard parents: diff
changeset | 63 | 
| 180 | 64 1; | 
| 293 | 65 | 
| 66 __END__ | |
| 67 | |
| 68 =pod | |
| 69 | |
| 70 =head1 NAME | |
| 71 | |
| 72 C<> | |
| 73 | |
| 74 =cut | 
