annotate 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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
60
b0c068da93ac Lazy activation for the configuration objects (final concept)
wizard
parents: 59
diff changeset
4 use base qw(IMPL::Object IMPL::Object::Autofill IMPL::Object::PublicSerializable);
59
0f3e369553bd Rewritten property implementation (probably become slower but more flexible)
wizard
parents:
diff changeset
5 use IMPL::Class::Property;
0f3e369553bd Rewritten property implementation (probably become slower but more flexible)
wizard
parents:
diff changeset
6
0f3e369553bd Rewritten property implementation (probably become slower but more flexible)
wizard
parents:
diff changeset
7 BEGIN {
0f3e369553bd Rewritten property implementation (probably become slower but more flexible)
wizard
parents:
diff changeset
8 public property factory => prop_all;
60
b0c068da93ac Lazy activation for the configuration objects (final concept)
wizard
parents: 59
diff changeset
9 public property parameters => prop_all;
b0c068da93ac Lazy activation for the configuration objects (final concept)
wizard
parents: 59
diff changeset
10 public property depends => prop_all | prop_list;
b0c068da93ac Lazy activation for the configuration objects (final concept)
wizard
parents: 59
diff changeset
11 public property object => prop_get | owner_set;
59
0f3e369553bd Rewritten property implementation (probably become slower but more flexible)
wizard
parents:
diff changeset
12 }
0f3e369553bd Rewritten property implementation (probably become slower but more flexible)
wizard
parents:
diff changeset
13
0f3e369553bd Rewritten property implementation (probably become slower but more flexible)
wizard
parents:
diff changeset
14 __PACKAGE__->PassThroughArgs;
0f3e369553bd Rewritten property implementation (probably become slower but more flexible)
wizard
parents:
diff changeset
15
0f3e369553bd Rewritten property implementation (probably become slower but more flexible)
wizard
parents:
diff changeset
16 sub CTOR {
0f3e369553bd Rewritten property implementation (probably become slower but more flexible)
wizard
parents:
diff changeset
17 my $this = shift;
60
b0c068da93ac Lazy activation for the configuration objects (final concept)
wizard
parents: 59
diff changeset
18
b0c068da93ac Lazy activation for the configuration objects (final concept)
wizard
parents: 59
diff changeset
19 die new IMPL::Exception("A Type parameter is required") unless $this->factory;
59
0f3e369553bd Rewritten property implementation (probably become slower but more flexible)
wizard
parents:
diff changeset
20
0f3e369553bd Rewritten property implementation (probably become slower but more flexible)
wizard
parents:
diff changeset
21 }
0f3e369553bd Rewritten property implementation (probably become slower but more flexible)
wizard
parents:
diff changeset
22
0f3e369553bd Rewritten property implementation (probably become slower but more flexible)
wizard
parents:
diff changeset
23 sub _is_class {
0f3e369553bd Rewritten property implementation (probably become slower but more flexible)
wizard
parents:
diff changeset
24 no strict 'refs';
0f3e369553bd Rewritten property implementation (probably become slower but more flexible)
wizard
parents:
diff changeset
25 scalar keys %{"$_[0]::"} ? 1 : 0;
0f3e369553bd Rewritten property implementation (probably become slower but more flexible)
wizard
parents:
diff changeset
26 }
0f3e369553bd Rewritten property implementation (probably become slower but more flexible)
wizard
parents:
diff changeset
27
60
b0c068da93ac Lazy activation for the configuration objects (final concept)
wizard
parents: 59
diff changeset
28 sub activate {
59
0f3e369553bd Rewritten property implementation (probably become slower but more flexible)
wizard
parents:
diff changeset
29 my $this = shift;
0f3e369553bd Rewritten property implementation (probably become slower but more flexible)
wizard
parents:
diff changeset
30
60
b0c068da93ac Lazy activation for the configuration objects (final concept)
wizard
parents: 59
diff changeset
31 unless ($this->object) {
b0c068da93ac Lazy activation for the configuration objects (final concept)
wizard
parents: 59
diff changeset
32 my @args;
b0c068da93ac Lazy activation for the configuration objects (final concept)
wizard
parents: 59
diff changeset
33
b0c068da93ac Lazy activation for the configuration objects (final concept)
wizard
parents: 59
diff changeset
34 my $params = $this->parameters;
b0c068da93ac Lazy activation for the configuration objects (final concept)
wizard
parents: 59
diff changeset
35 if (UNIVERSAL::isa($params,'HASH')) {
b0c068da93ac Lazy activation for the configuration objects (final concept)
wizard
parents: 59
diff changeset
36 while ( my ($key,$value) = each %$params ) {
b0c068da93ac Lazy activation for the configuration objects (final concept)
wizard
parents: 59
diff changeset
37 push @args,$key, UNIVERSAL::isa($value,'IMPL::Config::Activator') ? $value->activate : $value;
b0c068da93ac Lazy activation for the configuration objects (final concept)
wizard
parents: 59
diff changeset
38 }
b0c068da93ac Lazy activation for the configuration objects (final concept)
wizard
parents: 59
diff changeset
39 } elsif (UNIVERSAL::isa($params,'ARRAY')) {
b0c068da93ac Lazy activation for the configuration objects (final concept)
wizard
parents: 59
diff changeset
40 push @args, map UNIVERSAL::isa($_,'IMPL::Config::Activator') ? $_->activate : $_, @$params;
b0c068da93ac Lazy activation for the configuration objects (final concept)
wizard
parents: 59
diff changeset
41 } else {
b0c068da93ac Lazy activation for the configuration objects (final concept)
wizard
parents: 59
diff changeset
42 push @args, UNIVERSAL::isa($params,'IMPL::Config::Activator') ? $params->activate : $params;
b0c068da93ac Lazy activation for the configuration objects (final concept)
wizard
parents: 59
diff changeset
43 }
b0c068da93ac Lazy activation for the configuration objects (final concept)
wizard
parents: 59
diff changeset
44
b0c068da93ac Lazy activation for the configuration objects (final concept)
wizard
parents: 59
diff changeset
45 push @args, map UNIVERSAL::isa($_,'IMPL::Config::Activator') ? $_->activate : $_, @_ if @_;
b0c068da93ac Lazy activation for the configuration objects (final concept)
wizard
parents: 59
diff changeset
46
b0c068da93ac Lazy activation for the configuration objects (final concept)
wizard
parents: 59
diff changeset
47 my $factory = $this->factory;
b0c068da93ac Lazy activation for the configuration objects (final concept)
wizard
parents: 59
diff changeset
48 eval "require $factory; 1;" unless not ref $factory and _is_class($factory);
b0c068da93ac Lazy activation for the configuration objects (final concept)
wizard
parents: 59
diff changeset
49
b0c068da93ac Lazy activation for the configuration objects (final concept)
wizard
parents: 59
diff changeset
50 return $this->object($factory->new(@args));
59
0f3e369553bd Rewritten property implementation (probably become slower but more flexible)
wizard
parents:
diff changeset
51 } else {
60
b0c068da93ac Lazy activation for the configuration objects (final concept)
wizard
parents: 59
diff changeset
52 return $this->object;
59
0f3e369553bd Rewritten property implementation (probably become slower but more flexible)
wizard
parents:
diff changeset
53 }
0f3e369553bd Rewritten property implementation (probably become slower but more flexible)
wizard
parents:
diff changeset
54 }
0f3e369553bd Rewritten property implementation (probably become slower but more flexible)
wizard
parents:
diff changeset
55
0f3e369553bd Rewritten property implementation (probably become slower but more flexible)
wizard
parents:
diff changeset
56 1;