DS::Transformer - receives, transforms and passes on rows


DSlib documentation Contained in the DSlib distribution.

Index


Code Index:

NAME

Top

DS::Transformer - receives, transforms and passes on rows

DESCRIPTION

Top

This class is the base class of all transformers in DS. If you need to write a transformer, first consider if DS::Transformer::Sub will do. It supports any kind of row-wise transformations where there is a one-to-one correspondence between incoming and outgoing rows.

SUPER CLASSES

Top

DS::Transformer is a mixin of DS::Source and DS::Target.

METHODS

Top

process( $row )

Method for processing of ingoing data. This method is supposed to be overridden. By default it will return $row.

new( $class, $in_type, $out_type, $source, $target )

Constructor. Instantiates an object of class $class, taking the type $in_type, returning the type $out_type, attached to the source $source and attaced to the target $target. Besides $class, any of the parameters can be left out.

attach_target( $target )

Attaches target $target to this object. This method also triggers type checking, ensuring that the outgoing type of this object is sufficient for $target. If the type check fails, an exception is thrown.

target( $target )

This is a method mostly for internal use. It will get or set the target, bypassing type checks.

pass_row( $row )

Calling this metod will cause the transformer to pass $row to the target $target.

out_type( $type )

This is an accessor that gets or sets the outgoing type of this object.

receive_row( $row )

Triggers processing of $row. This method calls process with $row, and then passes the result to pass_row.

attach_source( $source )

Attaches $source as source. This method also validates data types by calling validate_source_type, throwing an exception if the validation fails.

source( $source )

Accessor for source. This method sets the source of this object and triggers type checking.

validate_source_type( $source_type )

Validates source type. If the $source_type is not valid, it returns false, true otherwise. By default, this method ensures that the ingoing type of this object contains no fields not specified in $source_type. Override if you need more complex checking.

in_type( $type )

Accessor for ingoing type.

SEE ALSO

Top

DS::Transformer::Sub, DS::Source, DS::Target.

AUTHOR

Top

Written by Michael Zedeler.


DSlib documentation Contained in the DSlib distribution.

#!perl

# ########################################################################## #
# Title:         Data stream transformer
# Creation date: 2007-03-05
# Author:        Michael Zedeler
# Description:   Transforms data stream
#                Data Stream class
# File:          $Source: /data/cvs/lib/DSlib/lib/DS/Transformer.pm,v $
# Repository:    kronhjorten
# State:         $State: Exp $
# Documentation: inline
# Recepient:     -
# ########################################################################## #

package DS::Transformer;

use strict;
use Carp::Assert;

our ($VERSION) = $DS::VERSION;
our ($REVISION) = '$Revision: 1.2 $' =~ /(\d+\.\d+)/;

require DS::TypeSpec;
require DS::Target;
require DS::Source;

our( @ISA ) = qw{ DS::Target DS::Source };


sub new {
    my( $class, $in_type, $out_type, $source, $target ) = @_;

    bless my $self = {}, $class;

    if( $in_type ) {
        $self->in_type( $in_type );
    }
    if( $out_type ) {
        $self->out_type( $out_type );
    }
    if( $source ) {
        $self->attach_source( $source );
    }
    if( $target ) {
        $self->attach_target( $target );
    }

    return $self;
}

# Override this method if you want to change how the transformer passes
# rows onto its target when this method is called. If you just want to
# transform the row without changing how data is passed on, override
# process() in stead.
# This method MUST NOT return anything. If errors occur, croak or die with exceptions
sub receive_row {
    my( $self, $row ) = @_;

    $self->pass_row( $self->process( $row ) );

    return;
}

# Process row (possibly transforming it) before passing it to
# the next transformer.
# Just no operation (this method is here to be overridden)
sub process {
    return $_[1];
}

1;

__END__