Mercurial > pub > Impl
changeset 312:75a78cbf7dcf
View refactoring: INIT blocks are deprecated
author | cin |
---|---|
date | Mon, 29 Apr 2013 01:10:42 +0400 |
parents | d3b5a67ad2e8 |
children | ec4ec1f056fe |
files | Lib/IMPL/Object/Singleton.pm Lib/IMPL/Web/View/TTFactory.pm |
diffstat | 2 files changed, 13 insertions(+), 33 deletions(-) [+] |
line wrap: on
line diff
--- a/Lib/IMPL/Object/Singleton.pm Mon Apr 22 03:42:53 2013 +0400 +++ b/Lib/IMPL/Object/Singleton.pm Mon Apr 29 01:10:42 2013 +0400 @@ -9,11 +9,12 @@ __PACKAGE__->static_accessor_own(_instance => undef); -my %instances; - -sub CTOR { - die IMPL::InvalidOperationException->new("Only one instance of the singleton can be created",ref $_[0]) - if $_[0]->_instance; +sub InitInstance { + my $self = shift; + die IMPL::InvalidOperationException->new("Only one instance of the singleton can be created", $self) + if $self->_instance; + + $self->_instance($self->new(@_)); } sub instance {
--- a/Lib/IMPL/Web/View/TTFactory.pm Mon Apr 22 03:42:53 2013 +0400 +++ b/Lib/IMPL/Web/View/TTFactory.pm Mon Apr 29 01:10:42 2013 +0400 @@ -4,7 +4,7 @@ use Template::Context(); use Carp qw(carp); -use IMPL::lang qw(:hash); +use IMPL::lang qw(:hash is); use IMPL::Exception(); use Scalar::Util qw(weaken); @@ -23,13 +23,13 @@ ], props => [ template => PROP_RW, + activation => PROP_RW, context => PROP_RW, - instances => PROP_RW, baseLocation => PROP_RW, base => PROP_RW, registry => PROP_RO, blocks => PROP_RO, - initialized => PROP_RO + _instance => PROP_RW ] }; @@ -46,10 +46,10 @@ $context ||= new Template::Context(); my $baseLocation = join( '/', splice( @{[split(/\//,$path)]}, 0, -1 ) ); + $this->activation($template->activation || 'new'); $this->template($template); $this->context($context); $this->baseLocation($baseLocation); - $this->instances(0); $this->registry($registry); if (my $baseTplName = $template->extends) { @@ -126,30 +126,9 @@ sub CreateObject { my $this = shift; - $this->InitOnDemand(); - - my $instance = $this->SUPER::CreateObject(@_); - - my $count = $this->instances; - $count++; - $this->instances($count); - - return $instance; -} - -sub InitOnDemand { - my ($this) = @_; - - unless ($this->initialized) { - $this->initialized(1); - - $this->base->InitOnDemand() - if $this->base; - - if (my $init = $this->template->blocks->{INIT}) { - $this->context->process($init); - } - } + $this->activation eq 'singleton' ? + $this->_instance || $this->_instance($this->next::method(@_)) : + $this->next::method(@_); } sub CloneContext {