File Coverage

blib/lib/Commandable/Finder/MethodAttributes.pm
Criterion Covered Total %
statement 11 23 47.8
branch 0 2 0.0
condition n/a
subroutine 4 7 57.1
pod 1 1 100.0
total 16 33 48.4


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