| 1 | package MooseX::DeclareX::MethodPrefix; | ||
| 2 | |||
| 3 | BEGIN { | ||
| 4 | $MooseX::DeclareX::MethodPrefix::AUTHORITY = 'cpan:TOBYINK'; | ||
| 5 | $MooseX::DeclareX::MethodPrefix::VERSION = '0.009'; | ||
| 6 | } | ||
| 7 | |||
| 8 | use Moose; | ||
| 9 | |||
| 10 | sub prefix_keyword { | ||
| 11 | confess "this is an abstract base class!"; | ||
| 12 | } | ||
| 13 | |||
| 14 | sub install_method { | ||
| 15 | 1; # no-op | ||
| 16 | } | ||
| 17 | |||
| 18 | use Devel::Declare (); | ||
| 19 | use MooseX::Method::Signatures; | ||
| 20 | use Sub::Install qw/install_sub/; | ||
| 21 | use Devel::Declare::Context::Simple (); | ||
| 22 | |||
| 23 | has _dd_context => ( | ||
| 24 | is => 'ro', | ||
| 25 | isa => 'Devel::Declare::Context::Simple', | ||
| 26 | lazy => 1, | ||
| 27 | builder => '_build_dd_context', | ||
| 28 | handles => qr/.*/, | ||
| 29 | ); | ||
| 30 | |||
| 31 | has _dd_init_args => ( | ||
| 32 | is => 'rw', | ||
| 33 | isa => 'HashRef', | ||
| 34 | default => sub { +{} }, | ||
| 35 | ); | ||
| 36 | |||
| 37 | has class => ( | ||
| 38 | is => 'ro', | ||
| 39 | isa => 'ClassName', | ||
| 40 | required => 1, | ||
| 41 | ); | ||
| 42 | |||
| 43 | sub handle_has { +return } | ||
| 44 | |||
| 45 | sub BUILD | ||
| 46 | { | ||
| 47 | my $self = shift; | ||
| 48 | my $args = shift; | ||
| 49 | $self->_dd_init_args($args); | ||
| 50 | } | ||
| 51 | |||
| 52 | sub _build_dd_context | ||
| 53 | { | ||
| 54 | my $self = shift; | ||
| 55 | return Devel::Declare::Context::Simple::->new( | ||
| 56 | %{ $self->_dd_init_args } | ||
| 57 | ); | ||
| 58 | } | ||
| 59 | |||
| 60 | sub import | ||
| 61 | { | ||
| 62 | my $class = shift; | ||
| 63 | my $setup_class = caller(0); | ||
| 64 | $class->setup_for($setup_class); | ||
| 65 | } | ||
| 66 | |||
| 67 | sub setup_for | ||
| 68 | { | ||
| 69 | my ($class, $setup_class, $args) = @_; | ||
| 70 | $args ||= {}; | ||
| 71 | |||
| 72 | my $kw = $class->prefix_keyword; | ||
| 73 | |||
| 74 | Devel::Declare->setup_for($setup_class, { | ||
| 75 | $kw => { | ||
| 76 | const => sub { | ||
| 77 | my $self = $class->new({ class => $setup_class, %{ $args } }); | ||
| 78 | $self->init(@_); | ||
| 79 | return $self->parse; | ||
| 80 | }, | ||
| 81 | }, | ||
| 82 | }); | ||
| 83 | |||
| 84 | install_sub({ | ||
| 85 | code => sub {}, | ||
| 86 | into => $setup_class, | ||
| 87 | as => $kw, | ||
| 88 | }); | ||
| 89 | |||
| 90 | MooseX::Method::Signatures->setup_for($setup_class) | ||
| 91 | unless $setup_class->can('method'); | ||
| 92 | } | ||
| 93 | |||
| 94 | sub parse | ||
| 95 | { | ||
| 96 | my $self = shift; | ||
| 97 | my $kw = $self->prefix_keyword; | ||
| 98 | |||
| 99 | $self->skip_declarator; | ||
| 100 | $self->skipspace; | ||
| 101 | |||
| 102 | my $remaining = substr($self->get_linestr, $self->offset); | ||
| 103 | |||
| 104 | if ($remaining =~ m/^\s*has/s and defined $self->handle_has) | ||
| 105 | { | ||
| 106 | # THIS DOES NOT WORK! | ||
| 107 | my $handler = $self->handle_has; | ||
| 108 | $remaining =~ s/^\s*has/;$handler/; | ||
| 109 | my $line = $self->get_linestr; | ||
| 110 | substr($line, $self->offset) = $remaining; | ||
| 111 | $self->set_linestr($line); | ||
| 112 | return; | ||
| 113 | } | ||
| 114 | |||
| 115 | my $thing = $self->strip_name; | ||
| 116 | |||
| 117 | confess "expected 'method', got '${thing}'" | ||
| 118 | unless $thing eq 'method'; | ||
| 119 | |||
| 120 | $self->skipspace; | ||
| 121 | |||
| 122 | my $name = $self->strip_name; | ||
| 123 | confess "anonymous $kw methods not allowed" | ||
| 124 | unless defined $name && length $name; | ||
| 125 | |||
| 126 | my $pkg = $self->get_curstash_name; | ||
| 127 | my $proto = $self->strip_proto || ''; | ||
| 128 | my $proto_variant = MooseX::Method::Signatures::Meta::Method->wrap( | ||
| 129 | signature => "(${proto})", | ||
| 130 | package_name => $pkg, | ||
| 131 | name => $name, | ||
| 132 | ); | ||
| 133 | |||
| 134 | $self->inject_if_block( | ||
| 135 | $self->scope_injector_call . $proto_variant->injectable_code, | ||
| 136 | 'sub', | ||
| 137 | ); | ||
| 138 | |||
| 139 | $self->shadow(sub { | ||
| 140 | my $r = $proto_variant->reify(actual_body => $_[0]); | ||
| 141 | $self->install_method($r); | ||
| 142 | }); | ||
| 143 | } | ||
| 144 | |||
| 145 | 1; |