File Coverage

blib/lib/Sub/HandlesVia/HandlerLibrary/Blessed.pm
Criterion Covered Total %
statement 23 24 95.8
branch 2 2 100.0
condition n/a
subroutine 8 9 88.8
pod 0 3 0.0
total 33 38 86.8


line stmt bran cond sub pod time code
1 1     1   23 use 5.008;
  1         5  
2 1     1   8 use strict;
  1         2  
  1         29  
3 1     1   5 use warnings;
  1         2  
  1         68  
4              
5             package Sub::HandlesVia::HandlerLibrary::Blessed;
6              
7             our $AUTHORITY = 'cpan:TOBYINK';
8             our $VERSION = '0.050000';
9              
10 1     1   8 use Sub::HandlesVia::HandlerLibrary;
  1         2  
  1         111  
11             our @ISA = 'Sub::HandlesVia::HandlerLibrary';
12              
13 1     1   8 use Sub::HandlesVia::Handler qw( handler );
  1         2  
  1         18  
14 1     1   127 use Types::Standard qw( is_Str );
  1         4  
  1         10  
15              
16             # Non-exhaustive list!
17             sub handler_names {
18 0     0 0 0 return;
19             }
20              
21             sub has_handler {
22 3     3 0 9 my ($me, $handler_name) = @_;
23 3         17 is_Str $handler_name;
24             }
25              
26             my $simple_method_name = qr/\A[^\W0-9]\w*\z/;
27             sub get_handler {
28 3     3 0 7 my ($me, $handler_name) = @_;
29            
30 3 100       20 if ( $handler_name =~ $simple_method_name ) {
31 2         16 return handler(
32             name => 'Blessed:' . $handler_name,
33             template => sprintf(
34             'use Scalar::Util (); ⸨q{$ATTRNAME is not a blessed object}⸩ unless Scalar::Util::blessed( $GET ); $GET->%s(@ARG)',
35             $handler_name,
36             ),
37             is_mutator => 0,
38             );
39             }
40             else {
41 1         10 return handler(
42             name => 'Blessed:' . $handler_name,
43             template => sprintf(
44             'use Scalar::Util (); ⸨q{$ATTRNAME is not a blessed object}⸩ unless Scalar::Util::blessed( $GET ); $GET->${\ %s }(@ARG)',
45             B::perlstring($handler_name),
46             ),
47             is_mutator => 0,
48             );
49             }
50             }
51              
52             1;
53              
54             __END__
55              
56             =head1 NAME
57              
58             Sub::HandlesVia::HandlerLibrary::Blessed - library of object-related methods
59              
60             =head1 SYNOPSIS
61              
62             package My::Class {
63             use Moo;
64             use Sub::HandlesVia;
65             use Types::Standard 'Object';
66             use HTTP::Tiny;
67             has http_ua => (
68             is => 'rwp',
69             isa => Object,
70             handles_via => 'Blessed',
71             handles => {
72             'http_get' => 'get',
73             'http_post' => 'post',
74             },
75             default => sub { HTTP::Tiny->new },
76             );
77             }
78              
79             =head1 DESCRIPTION
80              
81             This is a library of methods for L<Sub::HandlesVia>.
82              
83             =head1 DELEGATABLE METHODS
84              
85             Unlike the other libraries supplied by Sub::HandlesVia, this library allows
86             you to delegate to I<any> method name.
87              
88             It assumes that the attribute value is a blessed object, and calls the
89             correspondingly named method on it.
90              
91             L<Moo>, L<Moose>, L<Mouse>, and L<Mite> all have this kind of delegation
92             built-in anyway, but this module allows you to perform the delegation using
93             Sub::HandlesVia. This may be useful for L<Object::Pad> and L<Class::Tiny>,
94             which don't have a built-in delegation feature.
95              
96             =head1 BUGS
97              
98             Please report any bugs to
99             L<https://github.com/tobyink/p5-sub-handlesvia/issues>.
100              
101             =head1 SEE ALSO
102              
103             L<Sub::HandlesVia>.
104              
105             =head1 AUTHOR
106              
107             Toby Inkster E<lt>tobyink@cpan.orgE<gt>.
108              
109             =head1 COPYRIGHT AND LICENCE
110              
111             This software is copyright (c) 2022 by Toby Inkster.
112              
113             This is free software; you can redistribute it and/or modify it under
114             the same terms as the Perl 5 programming language system itself.
115              
116             =head1 DISCLAIMER OF WARRANTIES
117              
118             THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
119             WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
120             MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
121