| 1 | # You may distribute under the terms of either the GNU General Public License | ||||||||||
| 2 | # or the Artistic License (the same terms as Perl itself) | ||||||||||
| 3 | # | ||||||||||
| 4 | # (C) Paul Evans, 2022-2024 -- leonerd@leonerd.org.uk | ||||||||||
| 5 | |||||||||||
| 6 | package Commandable::Finder::MethodAttributes 0.14; | ||||||||||
| 7 | |||||||||||
| 8 | 2 | ▶ | use v5.14; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 2 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | 2 | ▶ | use experimental qw( signatures ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | 2 | ▶ | use base qw( Commandable::Finder::SubAttributes ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 12 | |||||||||||
| 13 | 2 | ▶ | use Carp; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 14 | |||||||||||
| 15 | =head1 NAME | ||||||||||
| 16 | |||||||||||
| 17 | C<Commandable::Finder::MethodAttributes> - find commands stored as methods with attributes | ||||||||||
| 18 | |||||||||||
| 19 | =head1 SYNOPSIS | ||||||||||
| 20 | |||||||||||
| 21 | use Commandable::Finder::MethodAttributes; | ||||||||||
| 22 | |||||||||||
| 23 | my $object = SomeClass->new( ... ); | ||||||||||
| 24 | |||||||||||
| 25 | my $finder = Commandable::Finder::MethodAttributes->new( | ||||||||||
| 26 | object => $object, | ||||||||||
| 27 | ); | ||||||||||
| 28 | |||||||||||
| 29 | my $help_command = $finder->find_command( "help" ); | ||||||||||
| 30 | |||||||||||
| 31 | foreach my $command ( $finder->find_commands ) { | ||||||||||
| 32 | ... | ||||||||||
| 33 | } | ||||||||||
| 34 | |||||||||||
| 35 | =head1 DESCRIPTION | ||||||||||
| 36 | |||||||||||
| 37 | This subclass of L<Commandable::Finder::SubAttributes> looks for methods that | ||||||||||
| 38 | define commands, where each command is provided by an individual method in a | ||||||||||
| 39 | given class. It stores the object instance and arranges that each discovered | ||||||||||
| 40 | command method will capture it, passing it as the first argument when invoked. | ||||||||||
| 41 | |||||||||||
| 42 | The attributes on each method are those given by | ||||||||||
| 43 | C<Commandable::Finder::SubAttributes> and are used in the same way here. | ||||||||||
| 44 | |||||||||||
| 45 | =cut | ||||||||||
| 46 | |||||||||||
| 47 | =head1 CONSTRUCTOR | ||||||||||
| 48 | |||||||||||
| 49 | =cut | ||||||||||
| 50 | |||||||||||
| 51 | =head2 new | ||||||||||
| 52 | |||||||||||
| 53 | $finder = Commandable::Finder::MethodAttributes->new( %args ) | ||||||||||
| 54 | |||||||||||
| 55 | Constructs a new instance of C<Commandable::Finder::MethodAttributes>. | ||||||||||
| 56 | |||||||||||
| 57 | Takes the following named arguments: | ||||||||||
| 58 | |||||||||||
| 59 | =over 4 | ||||||||||
| 60 | |||||||||||
| 61 | =item object => OBJ | ||||||||||
| 62 | |||||||||||
| 63 | An object reference. Its class will be used for searching for command methods. | ||||||||||
| 64 | The instance itself is stored by the finder object and used to wrap each | ||||||||||
| 65 | command method. | ||||||||||
| 66 | |||||||||||
| 67 | =back | ||||||||||
| 68 | |||||||||||
| 69 | Any additional arguments are passed to the superclass constructor. | ||||||||||
| 70 | |||||||||||
| 71 | =cut | ||||||||||
| 72 | |||||||||||
| 73 | 0 | ▶ | sub new ( $class, %args ) | ||||||||
|
Subroutine
new: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||
| 74 | { | ||||||||||
| 75 | 0 | ▶ | my $object = delete $args{object} or croak "Require 'object'"; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 76 | 0 | $args{package} = ref $object; | |||||||||
| 77 | |||||||||||
| 78 | 0 | my $self = $class->SUPER::new( %args ); | |||||||||
| 79 | |||||||||||
| 80 | 0 | $self->{object} = $object; | |||||||||
| 81 | |||||||||||
| 82 | 0 | return $self; | |||||||||
| 83 | } | ||||||||||
| 84 | |||||||||||
| 85 | 0 | ▶ | sub _wrap_code ( $self, $code ) | ||||||||
|
Subroutine
_wrap_code: not called CC=1 SCAR=6.9
| |||||||||||
| 86 | { | ||||||||||
| 87 | 0 | my $object = $self->{object}; | |||||||||
| 88 | |||||||||||
| 89 | 0 | ▶ | return sub { | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 90 | 0 | $object->$code( @_ ); | |||||||||
| 91 | 0 | }; | |||||||||
| 92 | } | ||||||||||
| 93 | |||||||||||
| 94 | =head1 AUTHOR | ||||||||||
| 95 | |||||||||||
| 96 | Paul Evans <leonerd@leonerd.org.uk> | ||||||||||
| 97 | |||||||||||
| 98 | =cut | ||||||||||
| 99 | |||||||||||
| 100 | 2 | 0x55AA; | |||||||||