--- nfo/perl/libs/DesignPattern/Bridge.pm 2003/02/09 16:22:51 1.6 +++ nfo/perl/libs/DesignPattern/Bridge.pm 2003/02/11 10:34:19 1.7 @@ -1,7 +1,12 @@ ## -------------------------------------------------------------------------------- -## $Id: Bridge.pm,v 1.6 2003/02/09 16:22:51 joko Exp $ +## $Id: Bridge.pm,v 1.7 2003/02/11 10:34:19 joko Exp $ ## -------------------------------------------------------------------------------- ## $Log: Bridge.pm,v $ +## Revision 1.7 2003/02/11 10:34:19 joko +## + loaded module may now lack 'mixin::with' declaration +## + this gets us the possibility to load modules from any perl namespace +## + enabled this mechanism +## ## Revision 1.6 2003/02/09 16:22:51 joko ## + pseudo constructor mechanism via options ## @@ -35,7 +40,8 @@ ## ======== object inheritance ======== -# TODO: +# TODO / REFACTORING PROPOSAL +# leading from Data::Storage to code abstracted out into this module - DesignPattern::Bridge # - this is no inheritance and it doesn't have to be # - implement this module as a bridge to its sub-modules # - use the BridgePattern (http://c2.com/cgi/wiki?BridgePattern) @@ -46,27 +52,31 @@ # - sub getChildNodes # - sub run +# 2003-02-11, joko: does this have anything in parallel with CPAN's Class::Inner? + + # get logger instance my $logger = eval { Log::Dispatch::Config->instance; }; my $meta; ## ======== object constructor ======== - sub new { - my $invocant = shift; - my $class = ref($invocant) || $invocant; - my @args = (); - @_ && (@args = @_); - $logger->debug( __PACKAGE__ . "->new(@args)" ) if $logger; - my $self = { @_ }; +sub new { + my $invocant = shift; + my $class = ref($invocant) || $invocant; + my @args = (); + @_ && (@args = @_); + $logger->debug( __PACKAGE__ . "->new(@args)" ) if $logger; + my $self = { @_ }; + + # trace #print "class: $class", "\n"; - bless $self, $class; - ##if (my $bizWorks = shift) { - ##$self->boot($bizWorks); - ##} - - return $self; - } + + # create instance + bless $self, $class; + + return $self; +} ## ======== method overrider ======== @@ -101,7 +111,15 @@ # build full package name my $self_classname = ref $self; - my $package = $self_classname . '::' . $modulename_load; + # name + my $package = $modulename_load; + + # if package is absolute, cut away prefix ('/' or '::') + if ($package !~ s/^:://) { + # else: prefix with base classname if above name is relative (lacks of '/' or '::') + $package = $self_classname . '::' . $package + } + return $package; } @@ -145,8 +163,12 @@ #bless $self, $package; # V2: - # switch into foreign package and mixin plugin-module - $self->mixinPackage($package); + + # switch into foreign package and prepare for mixin + $self->mixin_prepare($package); + + # switch into local package (scope which uses DesignPattern::Bridge) and mixin plugin-module + $self->mixin_do($package); if (my $method = $options->{method}) { $self->$method(); @@ -157,19 +179,40 @@ } # TODO: maybe refactor to DesignPattern::Object? what about the '$logger'? - sub mixinPackage { + sub mixin_prepare { + my $self = shift; + my $package = shift; + my $self_classname = ref $self; + eval("package $package; use mixin::with '$self_classname';"); + + # FIXME: --- this is redundant --- + if ($@) { + $meta->{loaded}->{$package} = 0; + $logger->error( __PACKAGE__ . "->load: $@" ) if $logger; + } else { + $meta->{loaded}->{$package} = 1; + } + # FIXME: --- this is redundant --- + + } + + sub mixin_do { my $self = shift; my $package = shift; # switch into foreign package and mixin plugin-module my $self_classname = ref $self; eval("package $self_classname; use mixin '$package';"); #eval("use mixin_all '$package';"); + + # FIXME: --- this is redundant --- if ($@) { $meta->{loaded}->{$package} = 0; $logger->error( __PACKAGE__ . "->load: $@" ) if $logger; } else { $meta->{loaded}->{$package} = 1; } + # FIXME: --- this is redundant --- + } sub unload { @@ -203,3 +246,4 @@ } 1; +__END__