Mercurial > pub > Impl
annotate lib/IMPL/Config/ReferenceDescriptor.pm @ 423:60c2892a577c ref20150831
working on base class system
| author | cin |
|---|---|
| date | Mon, 02 Apr 2018 07:35:23 +0300 |
| parents | b0481c071bea |
| children |
| rev | line source |
|---|---|
| 415 | 1 package IMPL::Config::ReferenceDescriptor; |
| 2 use strict; | |
| 3 | |
| 4 use IMPL::Exception(); | |
| 5 use IMPL::declare { | |
|
422
b0481c071bea
IMPL::Config::Container tests, YAMLConfiguration now works and tested
cin
parents:
421
diff
changeset
|
6 base => [ |
|
b0481c071bea
IMPL::Config::Container tests, YAMLConfiguration now works and tested
cin
parents:
421
diff
changeset
|
7 'IMPL::Object' => undef, |
|
b0481c071bea
IMPL::Config::Container tests, YAMLConfiguration now works and tested
cin
parents:
421
diff
changeset
|
8 'IMPL::Config::Descriptor' => undef |
|
b0481c071bea
IMPL::Config::Container tests, YAMLConfiguration now works and tested
cin
parents:
421
diff
changeset
|
9 ], |
|
b0481c071bea
IMPL::Config::Container tests, YAMLConfiguration now works and tested
cin
parents:
421
diff
changeset
|
10 props => [ |
|
b0481c071bea
IMPL::Config::Container tests, YAMLConfiguration now works and tested
cin
parents:
421
diff
changeset
|
11 reference => 'ro', |
|
b0481c071bea
IMPL::Config::Container tests, YAMLConfiguration now works and tested
cin
parents:
421
diff
changeset
|
12 services => 'ro', |
|
b0481c071bea
IMPL::Config::Container tests, YAMLConfiguration now works and tested
cin
parents:
421
diff
changeset
|
13 lazy => 'ro', |
|
b0481c071bea
IMPL::Config::Container tests, YAMLConfiguration now works and tested
cin
parents:
421
diff
changeset
|
14 optional => 'ro', |
|
b0481c071bea
IMPL::Config::Container tests, YAMLConfiguration now works and tested
cin
parents:
421
diff
changeset
|
15 default => 'ro', |
|
b0481c071bea
IMPL::Config::Container tests, YAMLConfiguration now works and tested
cin
parents:
421
diff
changeset
|
16 _name => 'rw' |
|
b0481c071bea
IMPL::Config::Container tests, YAMLConfiguration now works and tested
cin
parents:
421
diff
changeset
|
17 ] |
| 415 | 18 }; |
| 19 | |
| 20 sub CTOR { | |
|
422
b0481c071bea
IMPL::Config::Container tests, YAMLConfiguration now works and tested
cin
parents:
421
diff
changeset
|
21 my ( $this, $ref, %opts ) = @_; |
| 415 | 22 |
|
422
b0481c071bea
IMPL::Config::Container tests, YAMLConfiguration now works and tested
cin
parents:
421
diff
changeset
|
23 $this->reference($ref) |
|
b0481c071bea
IMPL::Config::Container tests, YAMLConfiguration now works and tested
cin
parents:
421
diff
changeset
|
24 or die IMPL::InvalidArgumentException->new('ref'); |
| 415 | 25 |
|
422
b0481c071bea
IMPL::Config::Container tests, YAMLConfiguration now works and tested
cin
parents:
421
diff
changeset
|
26 $this->lazy( $opts{lazy} ) if $opts{lazy}; |
|
b0481c071bea
IMPL::Config::Container tests, YAMLConfiguration now works and tested
cin
parents:
421
diff
changeset
|
27 $this->optional( $opts{optional} ) if $opts{optional}; |
|
b0481c071bea
IMPL::Config::Container tests, YAMLConfiguration now works and tested
cin
parents:
421
diff
changeset
|
28 $this->default( $opts{default} ) |
|
b0481c071bea
IMPL::Config::Container tests, YAMLConfiguration now works and tested
cin
parents:
421
diff
changeset
|
29 if $opts{optional} and exists $opts{default}; |
| 416 | 30 |
|
422
b0481c071bea
IMPL::Config::Container tests, YAMLConfiguration now works and tested
cin
parents:
421
diff
changeset
|
31 $this->_name( 'ref ' . $ref ); |
| 415 | 32 } |
| 33 | |
| 34 sub Activate { | |
|
422
b0481c071bea
IMPL::Config::Container tests, YAMLConfiguration now works and tested
cin
parents:
421
diff
changeset
|
35 my ( $this, $context ) = @_; |
| 415 | 36 |
|
422
b0481c071bea
IMPL::Config::Container tests, YAMLConfiguration now works and tested
cin
parents:
421
diff
changeset
|
37 my $ref = $this->reference; |
|
b0481c071bea
IMPL::Config::Container tests, YAMLConfiguration now works and tested
cin
parents:
421
diff
changeset
|
38 my %opts; |
|
b0481c071bea
IMPL::Config::Container tests, YAMLConfiguration now works and tested
cin
parents:
421
diff
changeset
|
39 my $inst; |
|
b0481c071bea
IMPL::Config::Container tests, YAMLConfiguration now works and tested
cin
parents:
421
diff
changeset
|
40 if ( $this->optional ) { |
|
b0481c071bea
IMPL::Config::Container tests, YAMLConfiguration now works and tested
cin
parents:
421
diff
changeset
|
41 $opts{optional} = 1; |
|
b0481c071bea
IMPL::Config::Container tests, YAMLConfiguration now works and tested
cin
parents:
421
diff
changeset
|
42 $opts{default} = $this->default; |
|
b0481c071bea
IMPL::Config::Container tests, YAMLConfiguration now works and tested
cin
parents:
421
diff
changeset
|
43 } |
| 415 | 44 |
|
422
b0481c071bea
IMPL::Config::Container tests, YAMLConfiguration now works and tested
cin
parents:
421
diff
changeset
|
45 if ( $this->lazy ) { |
|
b0481c071bea
IMPL::Config::Container tests, YAMLConfiguration now works and tested
cin
parents:
421
diff
changeset
|
46 my $clone = $context->Clone(); |
|
b0481c071bea
IMPL::Config::Container tests, YAMLConfiguration now works and tested
cin
parents:
421
diff
changeset
|
47 return sub { |
|
b0481c071bea
IMPL::Config::Container tests, YAMLConfiguration now works and tested
cin
parents:
421
diff
changeset
|
48 $clone->Resolve( $ref, %opts ); |
|
b0481c071bea
IMPL::Config::Container tests, YAMLConfiguration now works and tested
cin
parents:
421
diff
changeset
|
49 }; |
|
b0481c071bea
IMPL::Config::Container tests, YAMLConfiguration now works and tested
cin
parents:
421
diff
changeset
|
50 } |
|
b0481c071bea
IMPL::Config::Container tests, YAMLConfiguration now works and tested
cin
parents:
421
diff
changeset
|
51 else { |
|
b0481c071bea
IMPL::Config::Container tests, YAMLConfiguration now works and tested
cin
parents:
421
diff
changeset
|
52 return $context->Resolve( $ref, %opts ); |
|
b0481c071bea
IMPL::Config::Container tests, YAMLConfiguration now works and tested
cin
parents:
421
diff
changeset
|
53 } |
| 415 | 54 } |
| 55 | |
| 56 1; | |
| 416 | 57 |
| 58 __END__ | |
| 59 | |
| 60 =pod | |
| 61 | |
| 62 =head1 NAME | |
| 63 | |
| 64 C<IMPL::Config::ReferenceDescriptor> - A descriptor which is points to the other service | |
| 65 | |
| 66 =head1 MEMBERS | |
| 67 | |
| 68 =head2 PROPS | |
| 69 | |
| 70 =head3 reference | |
| 71 | |
| 72 =head3 lazy | |
| 73 | |
| 74 =head3 optional | |
| 75 | |
| 76 =head3 default | |
| 77 | |
| 78 =head2 METHODS | |
| 79 | |
| 80 =head3 CTOR($ref, %opts) | |
| 81 | |
| 82 =head3 Activate($context) | |
| 83 | |
| 84 Returns a result of the activation of the service specified by the C<reference> property. | |
| 85 | |
| 86 If the reference is lazy the method returns a closure.The C<$context> will be cloned and the clone will be used to create the closure. | |
| 87 =cut |
