| 1 | package Gapp::Actions; | ||
| 2 | { | ||
| 3 | $Gapp::Actions::VERSION = '0.60'; | ||
| 4 | } | ||
| 5 | use Moose; | ||
| 6 | |||
| 7 | use Gapp::Actions::Base; | ||
| 8 | use Gapp::Actions::Util; | ||
| 9 | use Carp::Clan qw( ^Gapp::Actions ); | ||
| 10 | use Sub::Name; | ||
| 11 | |||
| 12 | use namespace::clean -except => [qw( meta )]; | ||
| 13 | |||
| 14 | sub import { | ||
| 15 | my ($class, %args) = @_; | ||
| 16 | my $callee = caller; | ||
| 17 | |||
| 18 | strict->import; | ||
| 19 | warnings->import; | ||
| 20 | |||
| 21 | # inject base class into new library | ||
| 22 | { no strict 'refs'; | ||
| 23 | unshift @{ $callee . '::ISA' }, 'Gapp::Actions::Base'; | ||
| 24 | } | ||
| 25 | |||
| 26 | # generate predeclared action helpers | ||
| 27 | if (my @orig_declare = @{ $args{ -declare } || [] }) { | ||
| 28 | my @to_export; | ||
| 29 | |||
| 30 | for my $action (@orig_declare) { | ||
| 31 | |||
| 32 | croak q[could not create action ($action): actions may not contain '::'] | ||
| 33 | if $action =~ /::/; | ||
| 34 | |||
| 35 | # add action to library and remember to export | ||
| 36 | $callee->declare_action( $action ); | ||
| 37 | push @to_export, $action; | ||
| 38 | } | ||
| 39 | |||
| 40 | $callee->import({ -full => 1, -into => $callee }, @to_export); | ||
| 41 | } | ||
| 42 | |||
| 43 | Gapp::Actions::Util->import({ into => $callee }); | ||
| 44 | |||
| 45 | 1; | ||
| 46 | } | ||
| 47 | |||
| 48 | |||
| 49 | sub action_export_generator { | ||
| 50 | my ( $class, $caller, $name ) = @_; | ||
| 51 | |||
| 52 | return subname "__ACTION__::" . $caller . "::" . "$name" => sub { | ||
| 53 | return ACTION_REGISTRY( $caller )->action( $name ); | ||
| 54 | }; | ||
| 55 | } | ||
| 56 | |||
| 57 | sub perform_export_generator { | ||
| 58 | my ( $class, $caller, $name ) = @_; | ||
| 59 | |||
| 60 | return sub { | ||
| 61 | my $action = ACTION_REGISTRY( $caller )->action( $name ); | ||
| 62 | return $action->code->( $action, @_ ); | ||
| 63 | }; | ||
| 64 | } | ||
| 65 | |||
| 66 | 1; | ||
| 67 | |||
| 68 | |||
| 69 | __END__ |