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