comparison 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
comparison
equal deleted inserted replaced
59:0f3e369553bd 60:b0c068da93ac
1 package IMPL::Config; 1 package IMPL::Config;
2 use strict; 2 use strict;
3 use warnings; 3 use warnings;
4 4
5 use base qw(IMPL::Object IMPL::Object::Serializable IMPL::Object::Autofill); 5 use base qw(IMPL::Object::Accessor IMPL::Object::Serializable IMPL::Object::Autofill);
6 6
7 __PACKAGE__->PassThroughArgs; 7 __PACKAGE__->PassThroughArgs;
8 8
9 use IMPL::Class::Member; 9 use IMPL::Class::Member;
10 use IMPL::Class::PropertyInfo; 10 use IMPL::Class::PropertyInfo;
11 use IMPL::Exception; 11 use IMPL::Exception;
12 12
13 use IMPL::Serialization; 13 use IMPL::Serialization;
14 use IMPL::Serialization::XmlFormatter; 14 use IMPL::Serialization::XmlFormatter;
15
16
15 17
16 sub LoadXMLFile { 18 sub LoadXMLFile {
17 my ($self,$file) = @_; 19 my ($self,$file) = @_;
18 20
19 my $class = ref $self || $self; 21 my $class = ref $self || $self;
77 79
78 foreach my $info ($this->get_meta('IMPL::Class::PropertyInfo')) { 80 foreach my $info ($this->get_meta('IMPL::Class::PropertyInfo')) {
79 next if $info->Access != IMPL::Class::Member::MOD_PUBLIC; # save only public properties 81 next if $info->Access != IMPL::Class::Member::MOD_PUBLIC; # save only public properties
80 82
81 my $name = $info->Name; 83 my $name = $info->Name;
82 $ctx->AddVar($name => $this->$name()) if $this->$name(); 84 $ctx->AddVar($name => $this->rawGet($name)) if $this->rawGet($name);
83 } 85 }
86
84 } 87 }
85 88
86 sub spawn { 89 sub spawn {
87 goto &LoadXMLFile; 90 goto &LoadXMLFile;
91 }
92
93 sub get {
94 my $this = shift;
95
96 if (@_ == 1) {
97 my $obj = $this->SUPER::get(@_);
98 return UNIVERSAL::isa($obj,'IMPL::Config::Activator') ? $obj->activate : $obj;
99 } else {
100 my @objs = $this->SUPER::get(@_);
101 return map UNIVERSAL::isa($_,'IMPL::Config::Activator') ? $_->activate : $_, @objs ;
102 }
103 }
104
105 sub rawGet {
106 my $this = shift;
107 return $this->SUPER::get(@_);
108 }
109
110 sub Exists {
111 $_[0]->SUPER::get($_[1]) ? 1 : 0;
88 } 112 }
89 113
90 1; 114 1;
91 __END__ 115 __END__
92 116
101 use IMPL::Config::Class; 125 use IMPL::Config::Class;
102 126
103 BEGIN { 127 BEGIN {
104 public property SimpleString => prop_all; 128 public property SimpleString => prop_all;
105 public property MyClass => prop_all; 129 public property MyClass => prop_all;
106 public lazy property DataSource => prop_all, {type => 'App::DataSource', factory => sub {}}; 130 public property DataSource => prop_all;
107 } 131 }
108 132
109 sub CTOR { 133 sub CTOR {
110 my $this = shift; 134 my $this = shift;
111 $this->superCTOR(@_);
112 135
113 $this->MyClass(new IMPL::Config::Class(Type => MyClass)) unless $this->MyClass; 136 $this->MyClass(new IMPL::Config::Class(Type => 'MyClass'')) unless $this->MyClass;
137 $this->DataSource(
138 new IMPL::Config::Activator(
139 type => 'MyDataSource',
140 args=>{
141 host => 'localhost',
142 user => 'dbuser'
143 }
144 )
145 ) unless $this->Exists('DataSource');
114 } 146 }
115 147
116 # in some script 148 # in some script
117 149
118 my $app = spawn App::Config('default.xml'); 150 my $app = spawn App::Config('default.xml');
127 159
128 =head1 MEMBERS 160 =head1 MEMBERS
129 161
130 =over 162 =over
131 163
132 =item static LoadXMLFile($fileName) 164 =item C<< IMPL::Config->LoadXMLFile($fileName) >>
165
133 Создает из XML файла экземпляр приложения 166 Создает из XML файла экземпляр приложения
134 167
135 =item SaveXMLFile($fileName) 168 =item C<< $instance->SaveXMLFile($fileName) >>
169
136 Сохраняет приложение в файл 170 Сохраняет приложение в файл
137 171
138 =item xml 172 =item C<< xml >>
173
139 Сохраняет конфигурацию приложения в XML строку 174 Сохраняет конфигурацию приложения в XML строку
175
176 =item C<< IMPL::Config->spawn($file) >>
177
178 Синоним для C<LoadXMLFile>
140 179
141 =back 180 =back
142 181
143 =cut 182 =cut