File Coverage

blib/lib/Commandable/Finder/MethodAttributes.pm
Criterion Covered Total %
statement 14 30 46.6
branch 0 2 0.0
condition n/a
subroutine 5 8 62.5
pod 1 1 100.0
total 20 41 48.7


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-2024 -- leonerd@leonerd.org.uk
5              
6             package Commandable::Finder::MethodAttributes 0.14;
7              
8 2     2   234254 use v5.14;
  2         9  
9 2     2   17 use warnings;
  2         7  
  2         128  
10 2     2   633 use experimental qw( signatures );
  2         5353  
  2         16  
11 2     2   341 use base qw( Commandable::Finder::SubAttributes );
  2         6  
  2         1483  
12              
13 2     2   16 use Carp;
  2         4  
  2         645  
14              
15             =head1 NAME
16              
17             C - 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 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 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.
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 )
74 0     0 1   {
  0            
  0            
75 0 0         my $object = delete $args{object} or croak "Require 'object'";
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 )
86 0     0     {
  0            
  0            
87 0           my $object = $self->{object};
88              
89             return sub {
90 0     0     $object->$code( @_ );
91 0           };
92             }
93              
94             =head1 AUTHOR
95              
96             Paul Evans
97              
98             =cut
99              
100             0x55AA;