File Coverage

blib/lib/Trigger.pm
Criterion Covered Total %
statement 49 51 96.0
branch 13 18 72.2
condition 1 3 33.3
subroutine 6 7 85.7
pod 2 2 100.0
total 71 81 87.6


line stmt bran cond sub pod time code
1             package Trigger;
2              
3 3     3   1085 use strict;
  3         3  
  3         64  
4 3     3   29 use 5.8.1;
  3         6  
5             our $VERSION = '0.02_01';
6              
7 3     3   9 use base qw(Class::Accessor::Fast);
  3         9  
  3         1204  
8              
9             sub new {
10 2     2 1 52 my $class = shift;
11 2         21 my $self = $class->SUPER::new();
12 2         32 $self->mk_accessors(qw/heap _process _trigger _action _trigger_and_action _end/);
13            
14 2         328 my $args = {
15             #inline_states => {},
16             @_
17             };
18              
19 2 100       10 if( my $inline_states = delete $args->{inline_states} ){
20             $args = {
21             heap => {},
22             init => undef,
23 0     0   0 process => sub { 1 },
24             trigger_and_action => [],
25             end => undef,
26 1         4 %{$inline_states}
  1         4  
27             };
28             }
29              
30 2 50       11 $self->heap($args->{heap}) if defined $args->{heap};
31 2         18 $self->_process($args->{process});
32 2         8 $self->_trigger([]);
33 2         8 $self->_action([]);
34 2         9 $self->_end($args->{end});
35 2         6 $self->_trigger_and_action( scalar @{$args->{trigger_and_action}} );
  2         18  
36 2 50       9 $self->_trigger_and_action % 2 and die ;
37 2         24 while( @{$args->{trigger_and_action}} ){
  7         22  
38 5         6 my($trigger, $action) = splice @{$args->{trigger_and_action}}, 0, 2;
  5         9  
39 5         3 push @{$self->_trigger}, $trigger;
  5         7  
40 5         12 push @{$self->_action} , $action;
  5         9  
41             }
42 2         5 $self->_trigger_and_action( $self->_trigger_and_action / 2 - 1);
43 2 50 33     31 $args->{init}->($self) if defined $args->{init} and ref $args->{init};
44 2         251 return $self;
45             }
46              
47             sub eval{
48 4     4 1 516 my $self = shift;
49 4         4 my @action_re;
50 4         8 my @process_re = $self->_process->($self => @_);
51 4         36 for ( 0 .. $self->_trigger_and_action ){
52 12 100       82 if( my @trigger_re = $self->_trigger->[$_]->($self => @process_re) ){
53 3 100       34 if( ref $self->_action->[$_] eq 'CODE' ){
    50          
54 2         8 @action_re = $self->_action->[$_]->($self => @trigger_re) ;
55             }elsif( ref $self->_action->[$_] eq 'ARRAY' ){
56 1         10 for my $code( @{$self->_action->[$_]} ){
  1         1  
57 2         16 @action_re = $code->($self => @trigger_re);
58             }
59             }else{
60 0         0 die ref $self->_action->[$_];
61             }
62             }
63             }
64 4 100       63 @action_re ? return @action_re : return @process_re;
65             }
66              
67             sub DESTROY{
68 2     2   422 my $self = shift;
69 2 50       6 $self->_end->($self) if $self->_end;
70 2         156 delete $self->{$_} for (keys %{$self});
  2         81  
71             }
72              
73             1;
74             __END__