Skip to content
Newer
Older
100644 94 lines (69 sloc) 2.11 KB
bc6cecf @rcaputo Initial implementation of cb_role() syntax.
authored Feb 13, 2010
1 #!/usr/bin/env perl
e0d6b99 @rcaputo Explicitly export watches() and emits() from their corresponding Refl…
authored May 31, 2011
2 # vim: ts=2 sw=2 noexpandtab
bc6cecf @rcaputo Initial implementation of cb_role() syntax.
authored Feb 13, 2010
3
4 # This example illustrates explicit callbacks via Reflex roles. An
5 # object is assigned a role to play in its owner. Event names are
6 # mapped to methods by joining a prefix ("on" by default), the role
7 # name, and the event name. For example, a DNS resolver might have
8 # the role "resolver". The invocant's on_resolver_answer() would be
9 # called by default to invoke the resolver's "answer" event.
10
11 # Reflex::Callbacks and the Reflex::Callback helper classes will
12 # abstract callbacks to fulfill a number of goals. The goals are
5f76cdd @rcaputo requirements.otl becomes TODO.otl, as it ought to be.
authored Aug 5, 2010
13 # detailed in docs/TODO.otl and summarized in eg/eg-20-rcb-callback.pl
bc6cecf @rcaputo Initial implementation of cb_role() syntax.
authored Feb 13, 2010
14
15 use warnings;
16 use strict;
15add25 @rcaputo Fix library paths to find common test helper libraries.
authored Jun 15, 2010
17 use lib qw(t/lib);
bc6cecf @rcaputo Initial implementation of cb_role() syntax.
authored Feb 13, 2010
18
0b515ca @rcaputo Convert the callback examples into tests.
authored Jun 15, 2010
19 use Test::More tests => 4;
20
bc6cecf @rcaputo Initial implementation of cb_role() syntax.
authored Feb 13, 2010
21 # Create a thing that will invoke callbacks. cb_role() defines the
22 # thing's role within the RoleHandlerObject.
23 #
24 # There is no nonambiguous implicit syntax at this time. Suggestions
25 # for one are welcome.
26
27 {
28 package RoleHandlerObject;
29 use Moose;
30
31 use Reflex::Callbacks qw(cb_role);
32 use ThingWithCallbacks;
33
34 has callback_thing => ( is => 'rw', isa => 'ThingWithCallbacks' );
35
36 sub BUILD {
37 my $self = shift;
38
39 $self->callback_thing(
40 ThingWithCallbacks->new( cb_role($self, "thing") )
41 );
42 }
43
44 sub on_thing_event {
45 my ($self, $arg) = @_;
0b515ca @rcaputo Convert the callback examples into tests.
authored Jun 16, 2010
46 Test::More::pass("$self - role object handled event");
bc6cecf @rcaputo Initial implementation of cb_role() syntax.
authored Feb 13, 2010
47 }
48
49 sub run_thing {
50 my $self = shift;
51 $self->callback_thing()->run();
52 }
53 }
54
0b515ca @rcaputo Convert the callback examples into tests.
authored Jun 16, 2010
55 my $rho = RoleHandlerObject->new();
56 $rho->run_thing();
57
58 pass("$rho - role handler object ran to completion");
bc6cecf @rcaputo Initial implementation of cb_role() syntax.
authored Feb 13, 2010
59
60 # This form invokes a class methods.
61
62 {
63 package RoleHandlerClass;
64 use Moose;
65
66 use Reflex::Callbacks qw(cb_role);
67 use ThingWithCallbacks;
68
69 has callback_thing => ( is => 'rw', isa => 'ThingWithCallbacks' );
70
71 sub BUILD {
72 my $self = shift;
73
74 $self->callback_thing(
75 ThingWithCallbacks->new( cb_role(__PACKAGE__, "thing") )
76 );
77 }
78
79 sub on_thing_event {
80 my ($self, $arg) = @_;
0b515ca @rcaputo Convert the callback examples into tests.
authored Jun 16, 2010
81 Test::More::pass("$self - role class handled event");
bc6cecf @rcaputo Initial implementation of cb_role() syntax.
authored Feb 13, 2010
82 }
83
84 sub run_thing {
85 my $self = shift;
86 $self->callback_thing()->run();
87 }
88 }
89
0b515ca @rcaputo Convert the callback examples into tests.
authored Jun 16, 2010
90 my $rhc = RoleHandlerClass->new();
91 $rhc->run_thing();
92
93 pass("$rhc - role handler class ran to completion");
Something went wrong with that request. Please try again.