File Coverage

blib/lib/Reflex/Callback/Method.pm
Criterion Covered Total %
statement 6 6 100.0
branch n/a
condition n/a
subroutine 2 2 100.0
pod 1 1 100.0
total 9 9 100.0


line stmt bran cond sub pod time code
1             package Reflex::Callback::Method;
2             # vim: ts=2 sw=2 noexpandtab
3             $Reflex::Callback::Method::VERSION = '0.100';
4 13     13   52 use Moose;
  13         16  
  13         80  
5             extends 'Reflex::Callback';
6              
7             has method_name => (
8             is => 'ro',
9             isa => 'Str',
10             required => 1,
11             );
12              
13             sub deliver {
14 73     73 1 85 my ($self, $event) = @_;
15 73         2013 my $method_name = $self->method_name();
16 73         1691 $self->object()->$method_name($event);
17             }
18              
19             __PACKAGE__->meta->make_immutable;
20              
21             1;
22              
23             __END__
24              
25             =pod
26              
27             =encoding UTF-8
28              
29             =for :stopwords Rocco Caputo
30              
31             =head1 NAME
32              
33             Reflex::Callback::Method - Callback adapter for class and object methods
34              
35             =head1 VERSION
36              
37             This document describes version 0.100, released on April 02, 2017.
38              
39             =head1 SYNOPSIS
40              
41             Used within Reflex:
42              
43             package MethodHandler;
44             use Moose;
45             extends 'Reflex::Base';
46             use Reflex::Callbacks qw(cb_method);
47             use ExampleHelpers qw(eg_say);
48              
49             has ticker => (
50             isa => 'Maybe[Reflex::Interval]',
51             is => 'rw',
52             );
53              
54             sub BUILD {
55             my $self = shift;
56             $self->ticker(
57             Reflex::Interval->new(
58             interval => 1 + rand(),
59             auto_repeat => 1,
60             on_tick => cb_method($self, "callback"),
61             )
62             );
63             }
64              
65             sub callback {
66             eg_say("method callback triggered");
67             }
68              
69             MethodHandler->new()->run_all();
70              
71             Low-level usage:
72              
73             {
74             package Object;
75             use Moose;
76              
77             sub callback {
78             my ($self, $arg) = @_;
79             print "$self says: hello, $arg->{name}\n";
80             }
81             }
82              
83             my $object = Object->new();
84              
85             my $cb = Reflex::Callback::Method->new(
86             object => $object,
87             method_name => "callback"
88             );
89              
90             $cb->deliver(greet => { name => "world" });
91              
92             =head1 DESCRIPTION
93              
94             Reflex::Callback::Method maps the generic Reflex::Callback interface
95             to object and class method callbacks. Reflex::Callbacks' cb_method()
96             function simplifies callback creation. cb_object(), also supplied by
97             Reflex::Callbacks, is shorthand for setting several callbacks at once
98             on a single object or class. Other syntactic sweeteners are in
99             development.
100              
101             =head2 new
102              
103             Reflex::Callback::Method's constructor takes two named parameters.
104             "object" and "method_name" define the object and method that will be
105             invoked to handle the callback.
106              
107             Despite its name, "object" may also handle class names. In these
108             cases, "method_name" will be invoked as a class method rather than on
109             a particular instance of the class.
110              
111             =head2 deliver
112              
113             Reflex::Callback::Method's deliver() method invokes the object (or
114             class) and method as defined during the callback's construction.
115             deliver() takes two positional parameters: an event name (which is not
116             currently used for method callbacks), and a hashref of named
117             parameters to be passed to the callback.
118              
119             deliver() returns whatever the coderef does.
120              
121             =head1 SEE ALSO
122              
123             Please see those modules/websites for more information related to this module.
124              
125             =over 4
126              
127             =item *
128              
129             L<Reflex|Reflex>
130              
131             =item *
132              
133             L<Reflex>
134              
135             =item *
136              
137             L<Reflex::Callback>
138              
139             =item *
140              
141             L<Reflex::Callbacks>
142              
143             =item *
144              
145             L<Reflex/ACKNOWLEDGEMENTS>
146              
147             =item *
148              
149             L<Reflex/ASSISTANCE>
150              
151             =item *
152              
153             L<Reflex/AUTHORS>
154              
155             =item *
156              
157             L<Reflex/BUGS>
158              
159             =item *
160              
161             L<Reflex/BUGS>
162              
163             =item *
164              
165             L<Reflex/CONTRIBUTORS>
166              
167             =item *
168              
169             L<Reflex/COPYRIGHT>
170              
171             =item *
172              
173             L<Reflex/LICENSE>
174              
175             =item *
176              
177             L<Reflex/TODO>
178              
179             =back
180              
181             =head1 BUGS AND LIMITATIONS
182              
183             You can make new bug reports, and view existing ones, through the
184             web interface at L<http://rt.cpan.org/Public/Dist/Display.html?Name=Reflex>.
185              
186             =head1 AUTHOR
187              
188             Rocco Caputo <rcaputo@cpan.org>
189              
190             =head1 COPYRIGHT AND LICENSE
191              
192             This software is copyright (c) 2017 by Rocco Caputo.
193              
194             This is free software; you can redistribute it and/or modify it under
195             the same terms as the Perl 5 programming language system itself.
196              
197             =head1 AVAILABILITY
198              
199             The latest version of this module is available from the Comprehensive Perl
200             Archive Network (CPAN). Visit L<http://www.perl.com/CPAN/> to find a CPAN
201             site near you, or see L<https://metacpan.org/module/Reflex/>.
202              
203             =head1 DISCLAIMER OF WARRANTY
204              
205             BECAUSE THIS SOFTWARE IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
206             FOR THE SOFTWARE, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT
207             WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER
208             PARTIES PROVIDE THE SOFTWARE "AS IS" WITHOUT WARRANTY OF ANY KIND,
209             EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE
210             IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
211             PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE
212             SOFTWARE IS WITH YOU. SHOULD THE SOFTWARE PROVE DEFECTIVE, YOU ASSUME
213             THE COST OF ALL NECESSARY SERVICING, REPAIR, OR CORRECTION.
214              
215             IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
216             WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
217             REDISTRIBUTE THE SOFTWARE AS PERMITTED BY THE ABOVE LICENCE, BE LIABLE
218             TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL, OR
219             CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE
220             SOFTWARE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING
221             RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A
222             FAILURE OF THE SOFTWARE TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF
223             SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
224             DAMAGES.
225              
226             =cut