diff Lib/IMPL/Config.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 a35b60b16a99
children 76b878ad6596
line wrap: on
line diff
--- a/Lib/IMPL/Config.pm	Tue Mar 09 02:50:45 2010 +0300
+++ b/Lib/IMPL/Config.pm	Tue Mar 09 19:47:39 2010 +0300
@@ -2,7 +2,7 @@
 use strict;
 use warnings;
 
-use base qw(IMPL::Object IMPL::Object::Serializable IMPL::Object::Autofill);
+use base qw(IMPL::Object::Accessor IMPL::Object::Serializable IMPL::Object::Autofill);
 
 __PACKAGE__->PassThroughArgs;
 
@@ -13,6 +13,8 @@
 use IMPL::Serialization;
 use IMPL::Serialization::XmlFormatter;
 
+
+
 sub LoadXMLFile {
     my ($self,$file) = @_;
     
@@ -79,14 +81,36 @@
         next if $info->Access != IMPL::Class::Member::MOD_PUBLIC; # save only public properties
 
         my $name = $info->Name;
-        $ctx->AddVar($name => $this->$name()) if $this->$name();
+        $ctx->AddVar($name => $this->rawGet($name)) if $this->rawGet($name);
     }
+    
 }
 
 sub spawn {
 	goto &LoadXMLFile;
 }
 
+sub get {
+	my $this = shift;
+	
+	if (@_ == 1) {
+		my $obj = $this->SUPER::get(@_);
+		return UNIVERSAL::isa($obj,'IMPL::Config::Activator') ? $obj->activate : $obj;
+	} else {
+		my @objs = $this->SUPER::get(@_);	
+		return map UNIVERSAL::isa($_,'IMPL::Config::Activator') ? $_->activate : $_, @objs ;	
+	}
+}
+
+sub rawGet {
+	my $this = shift;
+	return $this->SUPER::get(@_);
+}
+
+sub Exists {
+	$_[0]->SUPER::get($_[1]) ? 1 : 0;
+}
+
 1;
 __END__
 
@@ -103,14 +127,22 @@
 BEGIN {
     public property SimpleString => prop_all;
     public property MyClass => prop_all;
-    public lazy property DataSource => prop_all, {type => 'App::DataSource', factory => sub {}}; 
+    public property DataSource => prop_all; 
 }
 
 sub CTOR {
     my $this = shift;
-    $this->superCTOR(@_);
 
-    $this->MyClass(new IMPL::Config::Class(Type => MyClass)) unless $this->MyClass;
+    $this->MyClass(new IMPL::Config::Class(Type => 'MyClass'')) unless $this->MyClass;
+    $this->DataSource(
+    	new IMPL::Config::Activator(
+    		type => 'MyDataSource',
+    		args=>{
+    			host => 'localhost',
+    			user => 'dbuser'
+    		}
+    	)
+    ) unless $this->Exists('DataSource');
 }
 
 # in some script
@@ -129,15 +161,22 @@
 
 =over
 
-=item static LoadXMLFile($fileName)
+=item C<< IMPL::Config->LoadXMLFile($fileName) >>
+
 Создает из XML файла экземпляр приложения
 
-=item SaveXMLFile($fileName)
+=item C<< $instance->SaveXMLFile($fileName) >>
+
 Сохраняет приложение в файл
 
-=item xml
+=item C<< xml >>
+
 Сохраняет конфигурацию приложения в XML строку
 
+=item C<< IMPL::Config->spawn($file) >>
+
+Синоним для C<LoadXMLFile>
+
 =back
 
 =cut