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