comparison 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
comparison
equal deleted inserted replaced
292:6dc1c369eb71 293:15d87ef41764
1 package IMPL::Config::Activator; 1 package IMPL::Config::Activator;
2 use strict; 2 use strict;
3 3
4 use parent qw(IMPL::Object IMPL::Object::Autofill IMPL::Object::PublicSerializable); 4 use Scalar::Util qw(reftype);
5 use IMPL::Class::Property; 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 };
6 23
7 BEGIN { 24 use constant {
8 public property factory => prop_all; 25 SELF_CLASS => __PACKAGE__
9 public property parameters => prop_all; 26 };
10 public property object => prop_get | owner_set;
11 }
12
13 __PACKAGE__->PassThroughArgs;
14 27
15 sub CTOR { 28 sub CTOR {
16 my $this = shift; 29 my $this = shift;
17 30
18 die new IMPL::Exception("A factory parameter is required") unless $this->factory; 31 die Exception->new("A factory parameter is required") unless $this->factory;
19 32
20 } 33 }
21 34
22 sub _is_class {
23 no strict 'refs';
24 UNIVERSAL::can($_[0],'new') ? 1 : 0;
25 }
26 35
27 sub activate { 36 sub activate {
28 my $this = shift; 37 my $this = shift;
29 38
30 unless ($this->object) { 39 unless ($this->object) {
31 my @args; 40 my @args;
32 41
33 my $params = $this->parameters; 42 my $params = $this->parameters;
34 if (UNIVERSAL::isa($params,'HASH')) { 43 if (reftype($params) eq 'HASH') {
35 while ( my ($key,$value) = each %$params ) { 44 while ( my ($key,$value) = each %$params ) {
36 push @args,$key, UNIVERSAL::isa($value,'IMPL::Config::Activator') ? $value->activate : $value; 45 push @args,$key, is($value,SELF_CLASS) ? $value->activate : $value;
37 } 46 }
38 } elsif (UNIVERSAL::isa($params,'ARRAY')) { 47 } elsif (reftype($params) eq 'ARRAY') {
39 push @args, map UNIVERSAL::isa($_,'IMPL::Config::Activator') ? $_->activate : $_, @$params; 48 push @args, map is($_,SELF_CLASS) ? $_->activate : $_, @$params;
40 } else { 49 } else {
41 push @args, UNIVERSAL::isa($params,'IMPL::Config::Activator') ? $params->activate : $params; 50 push @args, is($params,SELF_CLASS) ? $params->activate : $params;
42 } 51 }
43 52
44 push @args, map UNIVERSAL::isa($_,'IMPL::Config::Activator') ? $_->activate : $_, @_ if @_; 53 push @args, map is($_,SELF_CLASS) ? $_->activate : $_, @_ if @_;
45 54
46 my $factory = $this->factory; 55 my $factory = $this->factory;
47 eval "require $factory; 1;" unless (ref $factory or _is_class($factory)); 56 Loader->Require($factory) unless ref $factory;
48 57
49 return $this->object($factory->new(@args)); 58 return $this->object($factory->new(@args));
50 } else { 59 } else {
51 return $this->object; 60 return $this->object;
52 } 61 }
53 } 62 }
54 63
55 1; 64 1;
65
66 __END__
67
68 =pod
69
70 =head1 NAME
71
72 C<>
73
74 =cut