Mercurial > pub > Impl
comparison Lib/IMPL/Config/Activator.pm @ 60:b0c068da93ac
Lazy activation for the configuration objects (final concept)
small fixes
author | wizard |
---|---|
date | Tue, 09 Mar 2010 19:47:39 +0300 |
parents | 0f3e369553bd |
children | 8d0ae27d15c1 |
comparison
equal
deleted
inserted
replaced
59:0f3e369553bd | 60:b0c068da93ac |
---|---|
1 package IMPL::Config::Activator; | 1 package IMPL::Config::Activator; |
2 use strict; | 2 use strict; |
3 | 3 |
4 use base qw(IMPL::Object IMPL::Object::Autofill); | 4 use base qw(IMPL::Object IMPL::Object::Autofill IMPL::Object::PublicSerializable); |
5 use IMPL::Class::Property; | 5 use IMPL::Class::Property; |
6 | 6 |
7 BEGIN { | 7 BEGIN { |
8 public property factory => prop_all; | 8 public property factory => prop_all; |
9 public property args => prop_all; | 9 public property parameters => prop_all; |
10 private property _object => prop_all; | 10 public property depends => prop_all | prop_list; |
11 public property object => prop_get | owner_set; | |
11 } | 12 } |
12 | 13 |
13 __PACKAGE__->PassThroughArgs; | 14 __PACKAGE__->PassThroughArgs; |
14 | 15 |
15 sub CTOR { | 16 sub CTOR { |
16 my $this = shift; | 17 my $this = shift; |
17 | 18 |
18 die new IMPL::Exception("A Type parameter is required") unless $this->Type; | 19 die new IMPL::Exception("A Type parameter is required") unless $this->factory; |
19 | 20 |
20 } | 21 } |
21 | 22 |
22 sub _is_class { | 23 sub _is_class { |
23 no strict 'refs'; | 24 no strict 'refs'; |
24 scalar keys %{"$_[0]::"} ? 1 : 0; | 25 scalar keys %{"$_[0]::"} ? 1 : 0; |
25 } | 26 } |
26 | 27 |
27 sub instance { | 28 sub activate { |
28 my $this = shift; | 29 my $this = shift; |
29 | 30 |
30 my $factory = $this->fatory; | 31 unless ($this->object) { |
31 | 32 my @args; |
32 if (my $obj = $this->_object) { | 33 |
33 return $obj; | 34 my $params = $this->parameters; |
35 if (UNIVERSAL::isa($params,'HASH')) { | |
36 while ( my ($key,$value) = each %$params ) { | |
37 push @args,$key, UNIVERSAL::isa($value,'IMPL::Config::Activator') ? $value->activate : $value; | |
38 } | |
39 } elsif (UNIVERSAL::isa($params,'ARRAY')) { | |
40 push @args, map UNIVERSAL::isa($_,'IMPL::Config::Activator') ? $_->activate : $_, @$params; | |
41 } else { | |
42 push @args, UNIVERSAL::isa($params,'IMPL::Config::Activator') ? $params->activate : $params; | |
43 } | |
44 | |
45 push @args, map UNIVERSAL::isa($_,'IMPL::Config::Activator') ? $_->activate : $_, @_ if @_; | |
46 | |
47 my $factory = $this->factory; | |
48 eval "require $factory; 1;" unless not ref $factory and _is_class($factory); | |
49 | |
50 return $this->object($factory->new(@args)); | |
34 } else { | 51 } else { |
35 my %args = (%{$this->args || {}},@_); | 52 return $this->object; |
36 eval "require $factory" unless not ref $factory and _is_class($factory); | |
37 my $inst = $factory->new(%args); | |
38 $this->_object($inst); | |
39 return $inst; | |
40 } | 53 } |
41 } | 54 } |
42 | 55 |
43 1; | 56 1; |