annotate lib/IMPL/Config/Activator.pm @ 420:df591e3afd10 ref20150831

sync
author cin
date Sat, 25 Feb 2017 22:35:26 +0300
parents c6e90e02dd17
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
407
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
1 package IMPL::Config::Activator;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
2 use strict;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
3
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
4 use Scalar::Util qw(reftype);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
5 use IMPL::lang;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
6 use IMPL::Const qw(:prop);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
7 use IMPL::declare {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
8 require => {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
9 Loader => 'IMPL::Code::Loader',
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
10 Exception => 'IMPL::Exception'
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
11 },
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
12 base => [
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
13 'IMPL::Object' => undef,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
14 'IMPL::Object::Autofill' => '@_',
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
15 'IMPL::Object::PublicSerializable' => undef
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
16 ],
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
17 props => [
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
18 factory => PROP_RW,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
19 parameters => PROP_RW,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
20 singleCall => PROP_RW,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
21 _object => PROP_RW
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
22 ]
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
23 };
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
24
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
25 use constant {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
26 SELF_CLASS => __PACKAGE__,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
27 };
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
28
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
29 sub CTOR {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
30 my $this = shift;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
31
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
32 die Exception->new("A factory parameter is required") unless $this->factory;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
33
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
34 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
35
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
36
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
37 sub activate {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
38 my $this = shift;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
39
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
40 unless ($this->_object) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
41 my @args;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
42
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
43 my $params = $this->parameters;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
44 if (ref $params eq 'HASH') {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
45 while ( my ($key,$value) = each %$params ) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
46 push @args,$key, is($value,SELF_CLASS) ? $value->activate : $value;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
47 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
48 } elsif (ref $params eq 'ARRAY') {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
49 push @args, map is($_,SELF_CLASS) ? $_->activate : $_, @$params;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
50 } else {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
51 push @args, is($params,SELF_CLASS) ? $params->activate : $params;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
52 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
53
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
54 push @args, map is($_,SELF_CLASS) ? $_->activate : $_, @_ if @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
55
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
56 my $factory = $this->factory;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
57 Loader->default->Require($factory)
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
58 unless ref($factory);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
59
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
60 my $instance = $factory->new(@args);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
61
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
62 $this->_object($instance)
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
63 unless $this->singleCall;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
64
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
65 return $instance;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
66 } else {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
67 return $this->_object;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
68 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
69 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
70
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
71 1;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
72
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
73 __END__
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
74
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
75 =pod
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
76
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
77 =head1 NAME
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
78
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
79 C<IMPL::Config::Activator> - объект, используемый для получения других объектов.
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
80
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
81 =head1 DESCRIPTION
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
82
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
83 Служит дополнительным уровнем абстракции в тех случаях, когда нужный объект
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
84 заранее не известен или его создание должно происходить по требованию.
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
85 От обычной фабрики отличается также тем, что рассматривает формальные параметры
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
86 на наличие активаторов и выполняет их при активации.
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
87
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
88 Кроме того можно указать, что процесс активации должен происходить при каждом
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
89 обращении.
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
90
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
91 =cut