Verby::Step::Closure - Quick and dirty (in the fun sense, like playing with


Verby documentation Contained in the Verby distribution.

Index


Code Index:

NAME

Top

Verby::Step::Closure - Quick and dirty (in the fun sense, like playing with mud) step generator.

SYNOPSIS

Top

	use Verby::Step::Closure qw/step/;

	my $s = step "Action::Class" => sub {
		# called before action
	}, sub {
		# called after action
	};

DESCRIPTION

Top

This module eases the creation of step objects, by using closures and accessors. It's purpose is to be able to rapidly create simple steps based on an action class and some clalbacks.

Since Verby::Action and Verby::Step are separated, this may lead to unnecessary typing, class creation, or other silly crap. Verby::Step::Closure's purpose is to make this boundry unnoticable, so that when you don't need it it doesn't get in your way.

EXPORTED FUNCTIONS

Top

step $action_class ?$pre ?$post

This function (optionally exportable) is used as a quick and dirty constructor.

It will require $action_class with UNIVERSAL::require, and then create a new Verby::Step::Closure with the action field set to an instance.

METHODS

Top

new $action_class ?$pre ?$post

Creates a new anonymous step.

depends *@steps

Just a plain old accessor.

add_deps *@steps

Append more steps to the dep list.

is_satisfied

Calls the pre hook, verify and then the post hook.

do

Calls the pre hook, do, and lastly the post hook.

stringify

Stringifies to the action's class.

get
set

Replacements for Class::Accessor's methods that convert between lists and array references.

can $method

A special case of can in UNIVERSAL that will return false for methods the action can't fulfill.

EXAMPLE

Top

The test files, as well as the demo scripts make extensive use of Verby::Step::Closure. Look at scripts/module_builder.pl for some documented usage.

BUGS

Top

None that we are aware of. Of course, if you find a bug, let us know, and we will be sure to fix it.

CODE COVERAGE

Top

We use Devel::Cover to test the code coverage of the tests, please refer to COVERAGE section of the Verby module for more information.

SEE ALSO

Top

AUTHOR

Top

Yuval Kogman, <nothingmuch@woobling.org>

COPYRIGHT AND LICENSE

Top


Verby documentation Contained in the Verby distribution.

#!/usr/bin/perl

package Verby::Step::Closure;
use Moose;

with qw/Verby::Step::Simple/;

extends qw(Moose::Object Exporter);

our @EXPORT = "step";
our @EXPORT_OK = ( @EXPORT, qw(chain_steps) );

our $VERSION = "0.05";

use overload '""' => 'stringify';

use Class::Inspector;
use Carp qw/croak/;

use POE;

#my $id;
#has id => (
#	isa => "Int",
#	is  => "ro",
#	init_arg => undef,
#	default => sub { ++$id },
#);

has pre => (
	isa => "CodeRef",
	is  => "rw",
);

has post => (
	isa => "CodeRef",
	is  => "rw",
);

has provides_cxt => (
	isa => "Bool",
	is  => "rw",
);

sub is_satisfied {
	my $self = shift;
	$self->_wrapped("verify", @_);
}

sub do {
	my $self = shift;
	$self->_wrapped("do", @_);
}

sub _wrapped {
	my ( $self, $action_method, @args ) = @_;
	my ( $c, $poe ) = @args;

	if (my $pre_hook = $self->pre){
		$self->$pre_hook(@args);
	}
	
	if (my $post_hook = $self->post){
		my $heap = $poe_kernel->get_active_session->get_heap;

		push @{ $heap->{post_hooks} }, sub { $self->$post_hook(@args) };
	}

	$self->action->$action_method(@args);
}

sub step ($;%) {
	my ( $action, @args ) = @_;

	if ( @args == 1 ) {
		unshift @args, "pre";
	} elsif ( ref $args[0] and ref $args[0] ) {
		my ( $pre, $post ) = splice @args, 0, 2;
		unshift @args, pre => $pre, post => $post;
	}

	my %args = @args;

	unless (blessed $action){
		unless (Class::Inspector->loaded($action)) {
			(my $file = "${action}.pm") =~ s{::}{/}g;
			require $file;
		}

		$action = $action->new;
	}

	if ( exists $args{depends} and ref $args{depends} and ref $args{depends} ne 'ARRAY' ) {
		warn "$args{depends} is not an array";
		$args{depends} = [ $args{depends} ];
	}

	my $step = Verby::Step::Closure->new(
		%args,
		action => $action
	);

	$step;
}

sub chain_steps {
	my ( $head, @tail ) = @_;

	return unless $head;

	return $head unless @tail;

	my @rest = chain_steps(@tail);

	$rest[0]->add_deps($head);
	
	if ( wantarray ) {
		return ( $head, @rest );
	} else {
		return $rest[-1];
	}
}

sub stringify {
	my $self = shift;
	ref $self->action || $self->action;
}

__PACKAGE__

__END__