Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 145 lines (109 sloc) 3.193 kb
ce814c8 @rcaputo Implement abstract class method callbacks.
authored
1 #!/usr/bin/env perl
e0d6b99 @rcaputo Explicitly export watches() and emits() from their corresponding Refl…
authored
2 # vim: ts=2 sw=2 noexpandtab
ce814c8 @rcaputo Implement abstract class method callbacks.
authored
3
4 # This example illustrates explicit callbacks via classes, where
5 # callback events are mapped to class methods by name. Methods may be
6 # named after the events they handle, or they may differ.
7
8 # Reflex::Callbacks and the Reflex::Callback helper classes will
9 # abstract callbacks to fulfill a number of goals. The goals are
5f76cdd @rcaputo requirements.otl becomes TODO.otl, as it ought to be.
authored
10 # detailed in docs/TODO.otl and summarized in eg/eg-20-rcb-callback.pl
ce814c8 @rcaputo Implement abstract class method callbacks.
authored
11
12 use warnings;
13 use strict;
15add25 @rcaputo Fix library paths to find common test helper libraries.
authored
14 use lib qw(t/lib);
ce814c8 @rcaputo Implement abstract class method callbacks.
authored
15
0b515ca @rcaputo Convert the callback examples into tests.
authored
16 use Test::More tests => 6;
17
ce814c8 @rcaputo Implement abstract class method callbacks.
authored
18 # Create a thing that will invoke callbacks. This syntax uses
19 # explicitly specified cb_class() callbacks and a scalar for the
20 # methods list. cb_method() would be slightly more efficient in this
21 # case, but cb_class() also works.
22 #
23 # There is no nonambiguous implicit syntax at this time. Suggestions
24 # for one are welcome.
25
26 {
27 package ScalarHandlerClass;
28 use Moose;
29
30 use Reflex::Callbacks qw(cb_class);
31 use ThingWithCallbacks;
32
33 has callback_thing => ( is => 'rw', isa => 'ThingWithCallbacks' );
34
35 sub BUILD {
36 my $self = shift;
37
38 $self->callback_thing(
39 ThingWithCallbacks->new(
72d32d4 @rcaputo Bump the revision for release. Make little fixes here and there so a…
authored
40 cb_class(__PACKAGE__, "event"),
ce814c8 @rcaputo Implement abstract class method callbacks.
authored
41 )
42 );
43 }
44
72d32d4 @rcaputo Bump the revision for release. Make little fixes here and there so a…
authored
45 sub event {
ce814c8 @rcaputo Implement abstract class method callbacks.
authored
46 my ($self, $arg) = @_;
0b515ca @rcaputo Convert the callback examples into tests.
authored
47 Test::More::pass("$self - scalar class handled event");
ce814c8 @rcaputo Implement abstract class method callbacks.
authored
48 }
49
50 sub run_thing {
51 my $self = shift;
52 $self->callback_thing()->run();
53 }
54 }
55
56 my $sho = ScalarHandlerClass->new();
57 $sho->run_thing();
58
0b515ca @rcaputo Convert the callback examples into tests.
authored
59 pass("$sho - scalar handler object ran to completion");
60
ce814c8 @rcaputo Implement abstract class method callbacks.
authored
61 # In this case, a class handles a list of callbacks. Each callback
62 # method is named after the event it handles.
63 #
64 # There is no nonambiguous implicit syntax for this either, but
65 # suggestions are welcome.
66
67 {
68 package ArrayHandlerClass;
69 use Moose;
70
71 use Reflex::Callbacks qw(cb_class);
72 use ThingWithCallbacks;
73
74 has callback_thing => ( is => 'rw', isa => 'ThingWithCallbacks' );
75
76 sub BUILD {
77 my $self = shift;
78
79 $self->callback_thing(
80 ThingWithCallbacks->new(
72d32d4 @rcaputo Bump the revision for release. Make little fixes here and there so a…
authored
81 cb_class(__PACKAGE__, ["event"]),
ce814c8 @rcaputo Implement abstract class method callbacks.
authored
82 )
83 );
84 }
85
72d32d4 @rcaputo Bump the revision for release. Make little fixes here and there so a…
authored
86 sub event {
ce814c8 @rcaputo Implement abstract class method callbacks.
authored
87 my ($self, $arg) = @_;
0b515ca @rcaputo Convert the callback examples into tests.
authored
88 Test::More::pass("$self - array class handled event");
ce814c8 @rcaputo Implement abstract class method callbacks.
authored
89 }
90
91 sub run_thing {
92 my $self = shift;
93 $self->callback_thing()->run();
94 }
95 }
96
97 my $aho = ArrayHandlerClass->new();
98 $aho->run_thing();
99
0b515ca @rcaputo Convert the callback examples into tests.
authored
100 pass("$aho - array handler object ran to completion");
101
ce814c8 @rcaputo Implement abstract class method callbacks.
authored
102 # In this case, a class handles a hash of callbacks. Hash keys are
103 # event names, and the values are the corresponding handler method
104 # names. The hash gives classes flexibility in the methods they use.
105 #
106 # There is no nonambiguous implicit syntax for this either, but
107 # suggestions are welcome.
108
109 {
110 package HashHandlerClass;
111 use Moose;
112
113 use Reflex::Callbacks qw(cb_class);
114 use ThingWithCallbacks;
115
116 has callback_thing => ( is => 'rw', isa => 'ThingWithCallbacks' );
117
118 sub BUILD {
119 my $self = shift;
120
121 $self->callback_thing(
122 ThingWithCallbacks->new(
6da75c7 @rcaputo Get all callbacks working in an approximation of a "good" way.
authored
123 cb_class(__PACKAGE__, { event => "handle_event" }),
ce814c8 @rcaputo Implement abstract class method callbacks.
authored
124 )
125 );
126 }
127
128 sub handle_event {
129 my ($self, $arg) = @_;
0b515ca @rcaputo Convert the callback examples into tests.
authored
130 Test::More::pass("$self - hash class handled event");
ce814c8 @rcaputo Implement abstract class method callbacks.
authored
131 }
132
133 sub run_thing {
134 my $self = shift;
135 $self->callback_thing()->run();
136 }
137 }
138
139 my $hho = HashHandlerClass->new();
140 $hho->run_thing();
0b515ca @rcaputo Convert the callback examples into tests.
authored
141
142 pass("$hho - hash handler object ran to completion");
143
144 exit;
Something went wrong with that request. Please try again.