diff lib/IMPL/Config/ReferenceDescriptor.pm @ 415:3d24b10dd0d5 ref20150831

working on IMPL::Config::Container
author cin
date Tue, 20 Oct 2015 07:32:55 +0300
parents c6e90e02dd17
children cc2cf8c0edc2
line wrap: on
line diff
--- a/lib/IMPL/Config/ReferenceDescriptor.pm	Fri Oct 02 06:56:24 2015 +0300
+++ b/lib/IMPL/Config/ReferenceDescriptor.pm	Tue Oct 20 07:32:55 2015 +0300
@@ -0,0 +1,52 @@
+package IMPL::Config::ReferenceDescriptor;
+use strict;
+
+use IMPL::Exception();
+use IMPL::declare {
+	base => [
+		'IMPL::Object'             => undef,
+		'IMPL::Config::Descriptor' => undef
+	],
+	props => [
+		reference => 'ro',
+		services  => 'ro',
+		lazy      => 'ro',
+		optional  => 'ro',
+		default   => 'ro',
+		_name     => 'rw'
+	]
+};
+
+sub CTOR {
+	my ( $this, $ref, %opts ) = @_;
+
+	$this->reference($ref)
+	  or die IMPL::InvalidArgumentException->new('ref');
+
+	$this->_name( 'ref ' . $ref );
+}
+
+sub Activate {
+	my ( $this, $context ) = @_;
+
+	$this->EnterScope( $this->_name, $this->services );
+
+	my $ref = $this->reference;
+	my %opts;
+	$opts{default} = $this->default
+	  if $this->optional;
+
+	if ( $this->lazy ) {
+		my $clone = $context->Clone();
+		return sub {
+			$clone->Resolve( $ref, %opts );
+		};
+	}
+	else {
+		return $context->Resolve( $ref, %opts );
+	}
+
+	$this->LeaveScope();
+}
+
+1;