diff Lib/IMPL/Config/Activator.pm @ 60:b0c068da93ac

Lazy activation for the configuration objects (final concept) small fixes
author wizard
date Tue, 09 Mar 2010 19:47:39 +0300
parents 0f3e369553bd
children 8d0ae27d15c1
line wrap: on
line diff
--- a/Lib/IMPL/Config/Activator.pm	Tue Mar 09 02:50:45 2010 +0300
+++ b/Lib/IMPL/Config/Activator.pm	Tue Mar 09 19:47:39 2010 +0300
@@ -1,21 +1,22 @@
 package IMPL::Config::Activator;
 use strict;
 
-use base qw(IMPL::Object IMPL::Object::Autofill);
+use base qw(IMPL::Object IMPL::Object::Autofill IMPL::Object::PublicSerializable);
 use IMPL::Class::Property;
 
 BEGIN {
 	public property factory => prop_all;
-	public property args => prop_all;
-	private property _object => prop_all;
+	public property parameters => prop_all;
+	public property depends => prop_all | prop_list;
+	public property object => prop_get | owner_set;
 }
 
 __PACKAGE__->PassThroughArgs;
 
 sub CTOR {
     my $this = shift;
-    
-    die new IMPL::Exception("A Type parameter is required") unless $this->Type;
+
+    die new IMPL::Exception("A Type parameter is required") unless $this->factory;
     
 }
 
@@ -24,19 +25,31 @@
     scalar keys %{"$_[0]::"} ? 1 : 0;
 }
 
-sub instance {
+sub activate {
     my $this = shift;
     
-    my $factory = $this->fatory;
-    
-    if (my $obj = $this->_object) {
-        return $obj;
+    unless ($this->object) {
+        my @args;
+        
+        my $params = $this->parameters;
+        if (UNIVERSAL::isa($params,'HASH')) {
+        	while ( my ($key,$value) = each %$params ) {
+        		push @args,$key, UNIVERSAL::isa($value,'IMPL::Config::Activator') ? $value->activate : $value;
+        	}
+        } elsif (UNIVERSAL::isa($params,'ARRAY')) {
+        	push @args, map UNIVERSAL::isa($_,'IMPL::Config::Activator') ? $_->activate : $_, @$params;
+        } else {
+        	push @args, UNIVERSAL::isa($params,'IMPL::Config::Activator') ? $params->activate : $params;
+        }
+        
+        push @args,  map UNIVERSAL::isa($_,'IMPL::Config::Activator') ? $_->activate : $_, @_ if @_;
+        
+        my $factory = $this->factory;
+        eval "require $factory; 1;" unless not ref $factory and _is_class($factory);
+        
+        return $this->object($factory->new(@args));
     } else {
-        my %args = (%{$this->args || {}},@_);
-        eval "require $factory" unless not ref $factory and _is_class($factory);
-        my $inst = $factory->new(%args);
-        $this->_object($inst);
-        return $inst;
+    	return $this->object;
     }
 }