| 1 | package Gapp::App::Hook; | ||
| 2 | { | ||
| 3 | $Gapp::App::Hook::VERSION = '0.007'; | ||
| 4 | } | ||
| 5 | |||
| 6 | use Moose; | ||
| 7 | use MooseX::StrictConstructor; | ||
| 8 | use MooseX::SemiAffordanceAccessor; | ||
| 9 | |||
| 10 | #with 'Gapp::App::Role::HasApp'; | ||
| 11 | |||
| 12 | has 'action' => ( | ||
| 13 | is => 'rw', | ||
| 14 | isa => 'Str', | ||
| 15 | default => 'aggregate', | ||
| 16 | ); | ||
| 17 | |||
| 18 | has '_callbacks' => ( | ||
| 19 | is => 'rw', | ||
| 20 | isa => 'ArrayRef', | ||
| 21 | traits => [qw( Array )], | ||
| 22 | handles => { | ||
| 23 | pop => 'pop', | ||
| 24 | callbacks => 'elements', | ||
| 25 | }, | ||
| 26 | default => sub { [ ] }, | ||
| 27 | ); | ||
| 28 | |||
| 29 | has 'closure' => ( | ||
| 30 | is => 'rw', | ||
| 31 | isa => 'Maybe[CodeRef]', | ||
| 32 | default => undef, | ||
| 33 | ); | ||
| 34 | |||
| 35 | has 'name' => ( | ||
| 36 | is => 'rw', | ||
| 37 | isa => 'Str', | ||
| 38 | default => '', | ||
| 39 | ); | ||
| 40 | |||
| 41 | |||
| 42 | |||
| 43 | |||
| 44 | sub call { | ||
| 45 | my ( $self, $app, @params ) = @_; | ||
| 46 | |||
| 47 | my @results; | ||
| 48 | for my $cb ( $self->callbacks ) { | ||
| 49 | my ( $code, $data ) = @$cb; | ||
| 50 | |||
| 51 | push @results, $code->($app, \@params, $data ); | ||
| 52 | } | ||
| 53 | |||
| 54 | if ( $self->closure ) { | ||
| 55 | @results = $self->closure->( $self, $app, \@params, \@results ); | ||
| 56 | } | ||
| 57 | |||
| 58 | return @results; | ||
| 59 | } | ||
| 60 | |||
| 61 | sub push { | ||
| 62 | my ( $self, $code, $data ) = @_; | ||
| 63 | push @{$self->_callbacks}, [ $code, $data ]; | ||
| 64 | } | ||
| 65 | |||
| 66 | 1; | ||
| 67 | |||
| 68 | |||
| 69 | __END__ |