Skip to content

Commit

Permalink
Merge pull request #2 from clkao/master
Browse files Browse the repository at this point in the history
allow state_class instantiated with arguments

this makes state_class override more useful
  • Loading branch information
theory committed May 2, 2012
2 parents e66924a + 14004fe commit 53b946b
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
3 changes: 2 additions & 1 deletion lib/FSA/Rules.pm
Expand Up @@ -304,6 +304,7 @@ sub new {
};

$params->{state_class} ||= 'FSA::State';
$params->{state_args} ||= {};
while (@_) {
my $state = shift;
my $def = shift;
Expand All @@ -320,7 +321,7 @@ sub new {
}

# Create the state object and cache the state data.
my $obj = $params->{state_class}->new;
my $obj = $params->{state_class}->new(%{$params->{state_args}});
$def->{name} = $state;
$def->{machine} = $self;
$fsa->{table}{$state} = $obj;
Expand Down
21 changes: 20 additions & 1 deletion t/base.t
Expand Up @@ -2,7 +2,7 @@

use strict;
#use Test::More 'no_plan';
use Test::More tests => 316;
use Test::More tests => 327;

my $CLASS;
BEGIN {
Expand Down Expand Up @@ -637,6 +637,25 @@ ok $foo = $fsa->states('foo'), 'Get "foo" state';
isa_ok $foo, 'FSA::Stately';
isa_ok $foo, 'FSA::State';

ok $fsa = $CLASS->new( { start => 1,
state_class => 'FSA::Stately',
state_args => { myarg => 'bar'} },
foo => { rules => [ bar => 1 ]},
bar => {},
),
"Construct with state_class";

ok $foo = $fsa->states('foo'), 'Get "foo" state';
isa_ok $foo, 'FSA::Stately';
isa_ok $foo, 'FSA::State';
isa_ok $fsa->curr_state, 'FSA::Stately';
is $fsa->curr_state->name, 'foo';
is $fsa->curr_state->{myarg}, 'bar';
ok $fsa->try_switch;
isa_ok $fsa->curr_state, 'FSA::Stately';
is $fsa->curr_state->name, 'bar';
is $fsa->curr_state->{myarg}, 'bar';

# test that messages get set even if a state dies
$fsa = $CLASS->new(
alpha => {
Expand Down

0 comments on commit 53b946b

Please sign in to comment.