comparison lib/IMPL/Config/ReferenceDescriptor.pm @ 422:b0481c071bea ref20150831

IMPL::Config::Container tests, YAMLConfiguration now works and tested
author cin
date Sun, 20 Aug 2017 00:20:41 +0300
parents 7798345304bc
children
comparison
equal deleted inserted replaced
421:7798345304bc 422:b0481c071bea
1 package IMPL::Config::ReferenceDescriptor; 1 package IMPL::Config::ReferenceDescriptor;
2 use strict; 2 use strict;
3 3
4 use IMPL::Exception(); 4 use IMPL::Exception();
5 use IMPL::declare { 5 use IMPL::declare {
6 base => [ 6 base => [
7 'IMPL::Object' => undef, 7 'IMPL::Object' => undef,
8 'IMPL::Config::Descriptor' => undef 8 'IMPL::Config::Descriptor' => undef
9 ], 9 ],
10 props => [ 10 props => [
11 reference => 'ro', 11 reference => 'ro',
12 services => 'ro', 12 services => 'ro',
13 lazy => 'ro', 13 lazy => 'ro',
14 optional => 'ro', 14 optional => 'ro',
15 default => 'ro', 15 default => 'ro',
16 _name => 'rw' 16 _name => 'rw'
17 ] 17 ]
18 }; 18 };
19 19
20 sub CTOR { 20 sub CTOR {
21 my ( $this, $ref, %opts ) = @_; 21 my ( $this, $ref, %opts ) = @_;
22 22
23 $this->reference($ref) 23 $this->reference($ref)
24 or die IMPL::InvalidArgumentException->new('ref'); 24 or die IMPL::InvalidArgumentException->new('ref');
25 25
26 $this->lazy( $opts{lazy} ) if $opts{lazy}; 26 $this->lazy( $opts{lazy} ) if $opts{lazy};
27 $this->optional( $opts{optional} ) if $opts{optional}; 27 $this->optional( $opts{optional} ) if $opts{optional};
28 $this->default( $opts{default} ) 28 $this->default( $opts{default} )
29 if $opts{optional} and exists $opts{default}; 29 if $opts{optional} and exists $opts{default};
30 30
31 $this->_name( 'ref ' . $ref ); 31 $this->_name( 'ref ' . $ref );
32 } 32 }
33 33
34 sub Activate { 34 sub Activate {
35 my ( $this, $context ) = @_; 35 my ( $this, $context ) = @_;
36 36
37 $context->EnterScope( $this->_name, $this->services ); 37 my $ref = $this->reference;
38 my %opts;
39 my $inst;
40 if ( $this->optional ) {
41 $opts{optional} = 1;
42 $opts{default} = $this->default;
43 }
38 44
39 my $ref = $this->reference; 45 if ( $this->lazy ) {
40 my %opts; 46 my $clone = $context->Clone();
41 my $inst; 47 return sub {
42 $opts{default} = $this->default 48 $clone->Resolve( $ref, %opts );
43 if $this->optional; 49 };
44 50 }
45 if ( $this->lazy ) { 51 else {
46 my $clone = $context->Clone(); 52 return $context->Resolve( $ref, %opts );
47 $inst = sub { 53 }
48 $clone->Resolve( $ref, %opts );
49 };
50 }
51 else {
52 $inst = $context->Resolve( $ref, %opts );
53 }
54
55 $context->LeaveScope();
56 return $inst;
57 } 54 }
58 55
59 1; 56 1;
60 57
61 __END__ 58 __END__