File Coverage

blib/lib/Sub/HandlesVia/Declare.pm
Criterion Covered Total %
statement 22 24 91.6
branch 11 12 91.6
condition n/a
subroutine 6 6 100.0
pod n/a
total 39 42 92.8


line stmt bran cond sub pod time code
1 1     1   113394 use 5.008;
  1         13  
2 1     1   5 use strict;
  1         2  
  1         20  
3 1     1   7 use warnings;
  1         2  
  1         67  
4              
5              
6             our $AUTHORITY = 'cpan:TOBYINK';
7             our $VERSION = '0.045';
8              
9             use Sub::HandlesVia ();
10 1     1   417  
  1         5  
  1         232  
11             my %cache;
12              
13             my ( $class, $attr ) = ( shift, shift );
14             my ( $via, %delegations ) = ( @_ % 2 ) ? @_ : ( undef, @_ );
15 3     3   38
16 3 100       22 if ( not defined $via ) {
17             $via = 'Array' if $attr =~ /^@/;
18 3 100       11 $via = 'Hash' if $attr =~ /^%/;
19 2 100       10 if ( not defined $via ) {
20 2 100       10 require Sub::HandlesVia::Mite;
21 2 50       6 Sub::HandlesVia::Mite::croak(
22 0         0 'Expected usage: '.
23 0         0 'use Sub::HandlesVia::Declare ( $attr, $via, %delegations );'
24             );
25             }
26             }
27            
28             my $caller = caller;
29             if ( not $cache{$caller} ) {
30 3         7 'Sub::HandlesVia'->import(
31 3 100       13 {
32             into => $caller,
33             installer => sub { $cache{$caller} = $_[1][1] },
34             },
35 1     1   76 qw( delegations ),
36             );
37 1         11 }
38            
39             $cache{$caller}->(
40             attribute => $attr,
41 3         27 handles_via => $via,
42             handles => \%delegations,
43             );
44             }
45              
46             1;
47              
48              
49             =pod
50              
51             =encoding utf-8
52              
53             =head1 NAME
54              
55             Sub::HandlesVia::Declare - declare delegations at compile-time
56              
57             =head1 SYNOPSIS
58              
59             use Sub::HandlesVia::Declare( $attr, $via => %delegations );
60              
61             This is roughly equivalent to the following:
62              
63             use Sub::HandlesVia qw(delegations);
64            
65             BEGIN {
66             delegations(
67             attribute => $attr,
68             handles_via => $via,
69             handles => \%delegations,
70             );
71             };
72              
73             Except it doesn't import the C<delegations> function into your namespace.
74              
75             =head1 DESCRIPTION
76              
77             Useful for L<Object::Pad> and kind of nice for L<Class::Tiny>. Basically
78             any class builder than does its stuff at compile time.
79              
80             =head2 Object::Pad
81              
82             use Object::Pad;
83            
84             class Kitchen {
85             has @foods;
86             use Sub::HandlesVia::Declare '@foods', Array => (
87             all_foods => 'all',
88             add_food => 'push',
89             );
90             }
91              
92             If an attribute begins with a '@' or '%', C<< $via >> can be omitted.
93              
94             use Object::Pad;
95            
96             class Kitchen {
97             has @foods;
98             use Sub::HandlesVia::Declare '@foods', (
99             all_foods => 'all',
100             add_food => 'push',
101             );
102             }
103              
104             =head2 Class::Tiny
105              
106             package Kitchen;
107             use Class::Tiny {
108             foods => sub { [] },
109             drinks => sub { [ 'water' ] },
110             };
111             use Sub::HandlesVia::Declare 'foods', Array => (
112             all_foods => 'all',
113             add_food => 'push',
114             );
115             use Sub::HandlesVia::Declare 'drinks', Array => (
116             all_drinks => 'all',
117             add_drink => 'push',
118             );
119              
120             =head1 BUGS
121              
122             Please report any bugs to
123             L<https://github.com/tobyink/p5-sub-handlesvia/issues>.
124              
125             =head1 SEE ALSO
126              
127             L<Sub::HandlesVia>.
128              
129             =head1 AUTHOR
130              
131             Toby Inkster E<lt>tobyink@cpan.orgE<gt>.
132              
133             =head1 COPYRIGHT AND LICENCE
134              
135             This software is copyright (c) 2022 by Toby Inkster.
136              
137             This is free software; you can redistribute it and/or modify it under
138             the same terms as the Perl 5 programming language system itself.
139              
140             =head1 DISCLAIMER OF WARRANTIES
141              
142             THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
143             WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
144             MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
145