| 1 | package Gapp::Actions::Base; | ||
| 2 | { | ||
| 3 | $Gapp::Actions::Base::VERSION = '0.60'; | ||
| 4 | } | ||
| 5 | use Moose; | ||
| 6 | |||
| 7 | use Carp::Clan qw( ^Gapp::Actions ); | ||
| 8 | use Gapp::Actions::Util; | ||
| 9 | use Gapp::Action::Undefined; | ||
| 10 | use Gapp::Action::Registry; | ||
| 11 | use Sub::Exporter qw( build_exporter ); | ||
| 12 | use Moose::Util::TypeConstraints; | ||
| 13 | |||
| 14 | use namespace::clean -except => [qw( meta )]; | ||
| 15 | |||
| 16 | my $UndefMsg = q{Unable to find action '%s' in library '%s'}; | ||
| 17 | |||
| 18 | sub import { | ||
| 19 | my ($class, @args) = @_; | ||
| 20 | |||
| 21 | # filter or create options hash for S:E | ||
| 22 | my $options = (@args and (ref($args[0]) eq 'HASH')) ? $args[0] : undef; | ||
| 23 | unless ($options) { | ||
| 24 | $options = {foo => 23}; | ||
| 25 | unshift @args, $options; | ||
| 26 | } | ||
| 27 | |||
| 28 | # all actions known to us | ||
| 29 | my @actions = $class->action_names; | ||
| 30 | |||
| 31 | # determine the wrapper, -into is supported for compatibility reasons | ||
| 32 | my $wrapper = $options->{ -wrapper } || 'Gapp::Actions'; | ||
| 33 | $args[0]->{into} = $options->{ -into } | ||
| 34 | if exists $options->{ -into }; | ||
| 35 | |||
| 36 | my (%ex_spec, %ex_util); | ||
| 37 | |||
| 38 | # create the functions for export | ||
| 39 | for my $action_short (@actions) { | ||
| 40 | # the action itself | ||
| 41 | push @{ $ex_spec{exports} }, | ||
| 42 | $action_short, | ||
| 43 | sub { | ||
| 44 | bless $wrapper->action_export_generator($class, $action_short), | ||
| 45 | 'Gapp::Actions::EXPORTED_ACTION'; | ||
| 46 | }; | ||
| 47 | |||
| 48 | push @{ $ex_spec{exports} }, | ||
| 49 | 'do_' . $action_short, | ||
| 50 | sub { | ||
| 51 | $wrapper->perform_export_generator($class, $action_short) | ||
| 52 | }; | ||
| 53 | } | ||
| 54 | |||
| 55 | # create S:E exporter and increase export level unless specified explicitly | ||
| 56 | my $exporter = build_exporter \%ex_spec; | ||
| 57 | $options->{into_level}++ | ||
| 58 | unless $options->{into}; | ||
| 59 | |||
| 60 | |||
| 61 | # and on to the real exporter | ||
| 62 | my @new_args = ( @args, map { 'do_' . $_ } @actions ); #, keys %add); | ||
| 63 | return $class->$exporter(@new_args); | ||
| 64 | } | ||
| 65 | |||
| 66 | sub action_names { | ||
| 67 | my ( $class ) = @_; | ||
| 68 | return ACTION_REGISTRY( $class )->action_list | ||
| 69 | } | ||
| 70 | |||
| 71 | sub get_action { | ||
| 72 | my ($class, $name) = @_; | ||
| 73 | |||
| 74 | croak "Unknown action '$name' in library '$class'" unless | ||
| 75 | $class->has_action( $name ); | ||
| 76 | |||
| 77 | # return real name of the action | ||
| 78 | return ACTION_REGISTRY( $class )->action( $name ); | ||
| 79 | } | ||
| 80 | |||
| 81 | sub declare_action { | ||
| 82 | my ( $class, $name ) = @_; | ||
| 83 | return if ACTION_REGISTRY( $class )->has_action( $name ); | ||
| 84 | |||
| 85 | my $action = Gapp::Action::Undefined->new( name => $name ); | ||
| 86 | ACTION_REGISTRY( $class )->add_action( $action ); | ||
| 87 | } | ||
| 88 | |||
| 89 | sub has_action { | ||
| 90 | my ( $class, $name ) = @_; | ||
| 91 | return ACTION_REGISTRY( $class )->has_action( $name ); | ||
| 92 | } | ||
| 93 | |||
| 94 | |||
| 95 | |||
| 96 | |||
| 97 | |||
| 98 | 1; |