| 1 | #!perl | ||
| 2 | |||
| 3 | # ########################################################################## # | ||
| 4 | # Title: Data stream source proxy | ||
| 5 | # Creation date: 2007-04-16 | ||
| 6 | # Author: Michael Zedeler | ||
| 7 | # Description: Receives data and passes it to another, underlying source | ||
| 8 | # Data Stream class | ||
| 9 | # File: $Source: /data/cvs/lib/DSlib/lib/DS/Source/Proxy.pm,v $ | ||
| 10 | # Repository: kronhjorten | ||
| 11 | # State: $State: Exp $ | ||
| 12 | # Documentation: inline | ||
| 13 | # Recepient: - | ||
| 14 | # ########################################################################## # | ||
| 15 | |||
| 16 | package DS::Source::Proxy; | ||
| 17 | |||
| 18 | use base qw{ DS::Source }; | ||
| 19 | |||
| 20 | use strict; | ||
| 21 | use Carp qw{ croak cluck confess carp }; | ||
| 22 | use Carp::Assert; | ||
| 23 | |||
| 24 | our ($VERSION) = $DS::VERSION; | ||
| 25 | our ($REVISION) = '$Revision: 1.1 $' =~ /(\d+\.\d+)/; | ||
| 26 | |||
| 27 | my @proxy_methods = qw{ pass_row target attach_target out_type }; | ||
| 28 | |||
| 29 | sub new { | ||
| 30 | my( $class, $inner_object, $delegate ) = @_; | ||
| 31 | |||
| 32 | bless my $self = {}, $class; | ||
| 33 | $self->inner_object( $inner_object ) if $inner_object; | ||
| 34 | $self->delegate( $delegate ) if $delegate; | ||
| 35 | |||
| 36 | return $self; | ||
| 37 | } | ||
| 38 | |||
| 39 | sub inner_object { | ||
| 40 | my( $self, $object ) = @_; | ||
| 41 | |||
| 42 | my $result = 1; | ||
| 43 | if( $object ) { | ||
| 44 | assert($object->isa( 'DS::Source' ) ); | ||
| 45 | $self->{inner_object} = $object; | ||
| 46 | } else { | ||
| 47 | $result = $self->{inner_object}; | ||
| 48 | } | ||
| 49 | return $result; | ||
| 50 | } | ||
| 51 | |||
| 52 | sub delegate { | ||
| 53 | my( $self, $delegate ) = @_; | ||
| 54 | |||
| 55 | my $result = 1; | ||
| 56 | if( $delegate ) { | ||
| 57 | $self->{delegate} = $delegate; | ||
| 58 | } else { | ||
| 59 | $result = $self->{delegate}; | ||
| 60 | } | ||
| 61 | return $result; | ||
| 62 | } | ||
| 63 | |||
| 64 | # Create all proxy methods using eval | ||
| 65 | # This is not very expensive, since it is only done once - at load time | ||
| 66 | foreach my $method ( @proxy_methods ) { | ||
| 67 | eval <<"END_METHOD"; ## no critic | ||
| 68 | sub $method { | ||
| 69 | my( \$self, \@args ) = \@_; | ||
| 70 | if( \$self->{delegate}->can( 'delegate_$method' ) ) { | ||
| 71 | return \$self->{delegate}->delegate_invoke( '$method', \$self, \@args ); | ||
| 72 | } else { | ||
| 73 | return \$self->{inner_object}->$method( \@args ); | ||
| 74 | } | ||
| 75 | } | ||
| 76 | END_METHOD | ||
| 77 | } | ||
| 78 | |||
| 79 | 1; |