Workflow::Base - Base class with constructor


Workflow documentation Contained in the Workflow distribution.

Index


Code Index:

NAME

Top

Workflow::Base - Base class with constructor

VERSION

Top

This documentation describes version 1.08 of this package

SYNOPSIS

Top

 package My::App::Foo;
 use base qw( Workflow::Base );

DESCRIPTION

Top

Provide a constructor and some other useful methods for subclasses.

METHODS

Top

Class Methods

new( @params )

Just create a new object (blessed hashref) and pass along @params to the init() method, which subclasses can override to initialize themselves.

Returns: new object

Object Methods

init( @params )

Subclasses may implement to do initialization. The @params are whatever is passed into new(). Nothing need be returned.

param( [ $name, $value ] )

Associate arbitrary parameters with this object.

If neither $name nor $value given, return a hashref of all parameters set in object:

 my $params = $object->param();
 while ( my ( $name, $value ) = each %{ $params } ) {
     print "$name = $params->{ $name }\n";
 }

If $name given and it is a hash reference, assign all the values of the reference to the object parameters. This is the way to assign multiple parameters at once. Note that these will overwrite any existing parameter values. Return a hashref of all parameters set in object.

 $object->param({ foo => 'bar',
                  baz => 'blarney' });

If $name given and it is not a hash reference, return the value associated with it, undef if $name was not previously set.

 my $value = $object->param( 'foo' );
 print "Value of 'foo' is '$value'\n";

If $name and $value given, associate $name with $value, overwriting any existing value, and return the new value.

 $object->param( foo => 'blurney' );

delete_param( [ $name ] )

Delete parameters from this object.

If $name given and it is an array reference, then delete all parameters from this object. All deleted parameters will be returned as a hash reference together with their values.

 my $deleted = $object->delete_param(['foo','baz']);
 foreach my $key (keys %{$deleted})
 {
   print $key."::=".$deleted->{$key}."\n";
 }

If $name given and it is not an array reference, delete the parameter and return the value of the parameter.

 my $value = $object->delete_param( 'foo' );
 print "Value of 'foo' was '$value'\n";

If $name is not defined or $name does not exists the undef is returned.

clear_params()

Clears out all parameters associated with this object.

normalize_array( \@array | $item )

If given \@array return it dereferenced; if given $item, return it in a list. If given neither return an empty list.

COPYRIGHT

Top

AUTHORS

Top

Chris Winters <chris@cwinters.com>


Workflow documentation Contained in the Workflow distribution.

package Workflow::Base;

# $Id: Base.pm 454 2009-01-12 10:04:02Z jonasbn $

use warnings;
use strict;
use base qw( Class::Accessor );
use Log::Log4perl;

$Workflow::Base::VERSION = '1.08';

sub new {
    my ( $class, @params ) = @_;
    my $self = bless { PARAMS => {} }, $class;

    # always automatically pull out the name/value pairs from 'param'

    if ( ref $params[0] eq 'HASH' && ref $params[0]->{param} eq 'ARRAY' ) {
        foreach my $declared ( @{ $params[0]->{param} } ) {
            $params[0]->{ $declared->{name} } = $declared->{value};
        }
        delete $params[0]->{param};
    }
    $self->init(@params);
    return $self;
}

sub init {return}

sub param {
    my ( $self, $name, $value ) = @_;
    unless ( defined $name ) {
        return { %{ $self->{PARAMS} } };
    }

    # Allow multiple parameters to be set at once...

    if ( ref $name eq 'HASH' ) {
        foreach my $param_name ( keys %{$name} ) {
            $self->{PARAMS}{$param_name} = $name->{$param_name};
        }
        return { %{ $self->{PARAMS} } };
    }

    unless ( defined $value ) {
        if ( exists $self->{PARAMS}{$name} ) {
            return $self->{PARAMS}{$name};
        }
        return undef;
    }
    return $self->{PARAMS}{$name} = $value;
}

sub delete_param {
    my ( $self, $name ) = @_;
    unless ( defined $name ) {
        ## this is an error - perhaps an exception is too radical
        return undef;
    }

    # Allow multiple parameters to be deleted at once...

    if ( ref $name eq 'ARRAY' ) {
        my %list = ();
        foreach my $param_name ( @{$name} ) {
            next if ( not exists $self->{PARAMS}{$param_name} );
            $list{$param_name} = $self->{PARAMS}{$param_name};
            delete $self->{PARAMS}{$param_name};
        }
        return {%list};
    }

    if ( exists $self->{PARAMS}{$name} ) {
        my $value = $self->{PARAMS}{$name};
        delete $self->{PARAMS}{$name};
        return $value;
    }

    ## this is an error - perhaps an exception is too radical
    return undef;
}

sub clear_params {
    my ($self) = @_;
    $self->{PARAMS} = {};
}

sub normalize_array {
    my ( $self, $ref_or_item ) = @_;
    return () unless ($ref_or_item);
    return ( ref $ref_or_item eq 'ARRAY' ) ? @{$ref_or_item} : ($ref_or_item);
}

1;

__END__