| 1 | package Getopt::Chain::Declare::under; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | use base qw/Getopt::Chain::Declare::Branch/; | ||
| 7 | |||
| 8 | 1; | ||
| 9 | |||
| 10 | __END__ | ||
| 11 | |||
| 12 | use Moose(); | ||
| 13 | use Moose::Exporter; | ||
| 14 | |||
| 15 | our @IMPORT; | ||
| 16 | |||
| 17 | { | ||
| 18 | my ($import, $unimport) = Moose::Exporter->build_import_methods( | ||
| 19 | with_caller => [qw/ start on rewrite under /], | ||
| 20 | also => [qw/ Moose /], | ||
| 21 | ); | ||
| 22 | |||
| 23 | sub import { | ||
| 24 | warn "@_"; | ||
| 25 | shift; | ||
| 26 | |||
| 27 | local @IMPORT = @_; # Urgh | ||
| 28 | |||
| 29 | goto &$import; | ||
| 30 | } | ||
| 31 | |||
| 32 | no warnings 'once'; | ||
| 33 | *unimport = $unimport; | ||
| 34 | } | ||
| 35 | |||
| 36 | sub init_meta { | ||
| 37 | shift; | ||
| 38 | warn @IMPORT; | ||
| 39 | return Moose->init_meta( @_, base_class => 'Getopt::Chain', metaclass => 'Getopt::Chain::Meta::Class::Branch' ); | ||
| 40 | } | ||
| 41 | |||
| 42 | sub start { | ||
| 43 | my $caller = shift; | ||
| 44 | $caller->meta->start( @_ ); | ||
| 45 | } | ||
| 46 | |||
| 47 | sub on { | ||
| 48 | my $caller = shift; | ||
| 49 | $caller->meta->on( @_ ); | ||
| 50 | } | ||
| 51 | |||
| 52 | sub under { | ||
| 53 | my $caller = shift; | ||
| 54 | $caller->meta->under( @_ ); | ||
| 55 | } | ||
| 56 | |||
| 57 | sub rewrite { | ||
| 58 | my $caller = shift; | ||
| 59 | $caller->meta->rewrite( @_ ); | ||
| 60 | } | ||
| 61 | |||
| 62 | 1; | ||
| 63 | |||
| 64 | package Getopt::Chain::Meta::Class::Branch; | ||
| 65 | |||
| 66 | use Moose; | ||
| 67 | use MooseX::AttributeHelpers; | ||
| 68 | |||
| 69 | extends qw/Moose::Meta::Class/; | ||
| 70 | |||
| 71 | has getopt_chain_parent_class => qw/is ro lazy_build 1/; | ||
| 72 | sub _build_getopt_chain_parent_class { | ||
| 73 | my $self = shift; | ||
| 74 | my ($class) = $self->linearized_isa; | ||
| 75 | my @class = split m/::/, $class; | ||
| 76 | pop @class; | ||
| 77 | join '::', @class; | ||
| 78 | } | ||
| 79 | |||
| 80 | sub getopt_chain_parent_meta { | ||
| 81 | return shift->getopt_chain_parent_class->meta; | ||
| 82 | } | ||
| 83 | |||
| 84 | has recorder => qw/is ro lazy_build 1/, handles => [qw/ do_or_record /]; | ||
| 85 | sub _build_recorder { | ||
| 86 | require Getopt::Chain::Declare; | ||
| 87 | return Getopt::Chain::Declare::Recorder->new; | ||
| 88 | } | ||
| 89 | |||
| 90 | sub replay { | ||
| 91 | my $self = shift; | ||
| 92 | my $parent_builder = shift; | ||
| 93 | |||
| 94 | # We *could* redispatch here, but ... | ||
| 95 | $parent_builder->under( 'help' => sub { | ||
| 96 | $self->recorder->replay( $parent_builder ); | ||
| 97 | } ); | ||
| 98 | } | ||
| 99 | |||
| 100 | has registered => qw/is rw default 0/; | ||
| 101 | before do_or_record => sub { | ||
| 102 | my $self = shift; | ||
| 103 | return if $self->registered; | ||
| 104 | $self->getopt_chain_parent_meta->recorder->record( $self ); # "Register" ourself | ||
| 105 | $self->registered( 1 ); | ||
| 106 | }; | ||
| 107 | |||
| 108 | sub start { shift->do_or_record( start => @_ ) } | ||
| 109 | sub on { shift->do_or_record( on => @_ ) } | ||
| 110 | sub under { shift->do_or_record( under => @_ ) } | ||
| 111 | sub rewrite { shift->do_or_record( rewrite => @_ ) } | ||
| 112 | |||
| 113 | 1; |