File Coverage

blib/lib/MooseX/CurriedDelegation.pm
Criterion Covered Total %
statement 15 15 100.0
branch n/a
condition n/a
subroutine 7 7 100.0
pod 1 1 100.0
total 23 23 100.0


line stmt bran cond sub pod time code
1             #
2             # This file is part of MooseX-CurriedDelegation
3             #
4             # This software is Copyright (c) 2012 by Chris Weyl.
5             #
6             # This is free software, licensed under:
7             #
8             # The GNU Lesser General Public License, Version 2.1, February 1999
9             #
10             package MooseX::CurriedDelegation;
11             BEGIN {
12 1     1   1003047 $MooseX::CurriedDelegation::AUTHORITY = 'cpan:RSRCHBOY';
13             }
14             # git description: 0.001-7-ga5f859f
15             $MooseX::CurriedDelegation::VERSION = '0.002';
16              
17             # ABSTRACT: Curry your delegations with methods
18              
19 1     1   10 use strict;
  1         3  
  1         38  
20 1     1   6 use warnings;
  1         1  
  1         30  
21              
22 1     1   5 use Moose ( );
  1         2  
  1         23  
23 1     1   5 use Moose::Exporter;
  1         2  
  1         9  
24              
25             my $trait = 'MooseX::CurriedDelegation::Trait::Attribute';
26              
27             Moose::Exporter->setup_import_methods(
28             trait_aliases => [ $trait => 'CurriedDelegation' ],
29              
30             as_is => [ qw{ curry_to_self } ],
31             class_metaroles => { attribute => [ $trait ] },
32             role_metaroles => { applied_attribute => [ $trait ] },
33             );
34              
35 2     2 1 7 sub curry_to_self() { sub { shift } }
  1     1   5883  
36              
37             !!42;
38              
39             __END__
40              
41             =pod
42              
43             =encoding UTF-8
44              
45             =for :stopwords Chris Weyl
46              
47             =for :stopwords Wishlist flattr flattr'ed gittip gittip'ed
48              
49             =head1 NAME
50              
51             MooseX::CurriedDelegation - Curry your delegations with methods
52              
53             =head1 VERSION
54              
55             This document describes version 0.002 of MooseX::CurriedDelegation - released May 19, 2014 as part of MooseX-CurriedDelegation.
56              
57             =head1 SYNOPSIS
58              
59             use Moose;
60             use MooseX::CurriedDelegation;
61              
62             has one => (is => 'ro', isa => 'Str', default => 'default');
63              
64             has foo => (
65              
66             is => 'rw',
67             isa => 'TestClass::Delagatee', # has method curried()
68             default => sub { TestClass::Delagatee->new },
69              
70             handles => {
71              
72             # method-curry
73             # Note the hashref, not arrayref, we employ
74             # first arg is the remote method to delegate to
75             # second is an arrayref comprising:
76             # coderef to call as a method on the instance, followed by
77             # "static" curry args
78             #
79             # so, essentially:
80             # $self->foo->remote_method($self->$coderef(), @remaining_args);
81             #
82             # foo_del_one => {
83             # remote_method => [ sub { ... }, qw{ static args } ],
84             # },
85              
86             foo_del_one => { curried => [ sub { shift->one }, qw{ more curry args } ] },
87              
88             # curry_to_self() always returns: sub { shift }
89             foo_del_two => { other_method => [ curry_to_self ] },
90             },
91             );
92              
93             =head1 DESCRIPTION
94              
95             Method delegation is awfully handy -- but sometimes it'd be awfully handier if
96             it was a touch more dynamic. This is an attribute trait that provides for a
97             delegated method to be curried.
98              
99             =head1 USAGE
100              
101             Using this package will cause the relevant attribute trait to be applied
102             without requiring further intervention. We use the standard
103              
104             handles => { local_method => delegate_info, ... }
105              
106             delegation methodology, however our currying is invoked when delegate_info is
107             a hashref. We expect delegate info to look like:
108              
109             { remote_method => [ $coderef, @more_curry_args ] }
110              
111             Only $coderef is ever executed by the delegation. This means that it is safe
112             to have any number of additional coderefs in @more_curry_args: they will be
113             passed through to remote_method without additional manipulation.
114              
115             =head1 ADDITIONAL SUGAR
116              
117             In addition, we export a number of helper functions.
118              
119             =head2 curry_to_self()
120              
121             This function always returns a coderef like "sub { shift }". That is, this:
122              
123             local => { remote => [ curry_to_self ] }
124              
125             is equivalent to:
126              
127             local => { remote => [ sub { shift } ] }
128              
129             =head1 TRAIT ALIASES
130              
131             =head2 CurriedDelegation
132              
133             Resolves out to the full name of our attribute trait; you can use it as:
134              
135             has foo => (traits => [CurriedDelegation], ...)
136              
137             =head1 SEE ALSO
138              
139             Please see those modules/websites for more information related to this module.
140              
141             =over 4
142              
143             =item *
144              
145             L<Moose>
146              
147             =item *
148              
149             L<Moose::Meta::Method::Delegation>
150              
151             =back
152              
153             =head1 SOURCE
154              
155             The development version is on github at L<http://https://github.com/RsrchBoy/moosex-currieddelegation>
156             and may be cloned from L<git://https://github.com/RsrchBoy/moosex-currieddelegation.git>
157              
158             =head1 BUGS
159              
160             Please report any bugs or feature requests on the bugtracker website
161             https://github.com/RsrchBoy/moosex-currieddelegation/issues
162              
163             When submitting a bug or request, please include a test-file or a
164             patch to an existing test-file that illustrates the bug or desired
165             feature.
166              
167             =head1 AUTHOR
168              
169             Chris Weyl <cweyl@alumni.drew.edu>
170              
171             =head2 I'm a material boy in a material world
172              
173             =begin html
174              
175             <a href="https://www.gittip.com/RsrchBoy/"><img src="https://raw.githubusercontent.com/gittip/www.gittip.com/master/www/assets/%25version/logo.png" /></a>
176             <a href="http://bit.ly/rsrchboys-wishlist"><img src="http://wps.io/wp-content/uploads/2014/05/amazon_wishlist.resized.png" /></a>
177             <a href="https://flattr.com/submit/auto?user_id=RsrchBoy&url=https%3A%2F%2Fgithub.com%2FRsrchBoy%2Fmoosex-currieddelegation&title=RsrchBoy's%20CPAN%20MooseX-CurriedDelegation&tags=%22RsrchBoy's%20MooseX-CurriedDelegation%20in%20the%20CPAN%22"><img src="http://api.flattr.com/button/flattr-badge-large.png" /></a>
178              
179             =end html
180              
181             Please note B<I do not expect to be gittip'ed or flattr'ed for this work>,
182             rather B<it is simply a very pleasant surprise>. I largely create and release
183             works like this because I need them or I find it enjoyable; however, don't let
184             that stop you if you feel like it ;)
185              
186             L<Flattr this|https://flattr.com/submit/auto?user_id=RsrchBoy&url=https%3A%2F%2Fgithub.com%2FRsrchBoy%2Fmoosex-currieddelegation&title=RsrchBoy's%20CPAN%20MooseX-CurriedDelegation&tags=%22RsrchBoy's%20MooseX-CurriedDelegation%20in%20the%20CPAN%22>,
187             L<gittip me|https://www.gittip.com/RsrchBoy/>, or indulge my
188             L<Amazon Wishlist|http://bit.ly/rsrchboys-wishlist>... If you so desire.
189              
190             =head1 COPYRIGHT AND LICENSE
191              
192             This software is Copyright (c) 2012 by Chris Weyl.
193              
194             This is free software, licensed under:
195              
196             The GNU Lesser General Public License, Version 2.1, February 1999
197              
198             =cut