Commit | Line | Data |
2e3d0a0a |
1 | |
2 | =pod |
3 | |
4 | =head1 NAME |
5 | |
021b8139 |
6 | Moose::Cookbook::Roles::Recipe2 - Advanced Role Composition - method exclusion and aliasing |
2e3d0a0a |
7 | |
8 | =head1 SYNOPSIS |
9 | |
10 | package Restartable; |
11 | use Moose::Role; |
12 | |
13 | has 'is_paused' => ( |
14 | is => 'rw', |
9711d93b |
15 | isa => 'Bool', |
2e3d0a0a |
16 | default => 0, |
17 | ); |
18 | |
19 | requires 'save_state', 'load_state'; |
20 | |
21 | sub stop { ... } |
22 | |
23 | sub start { ... } |
24 | |
25 | package Restartable::ButUnreliable; |
26 | use Moose::Role; |
27 | |
59f5bbde |
28 | with 'Restartable' => { |
29 | alias => { |
30 | stop => '_stop', |
31 | start => '_start' |
32 | } |
33 | }; |
2e3d0a0a |
34 | |
35 | sub stop { |
36 | my $self = shift; |
37 | |
38 | $self->explode() if rand(1) > .5; |
39 | |
40 | $self->_stop(); |
41 | } |
42 | |
43 | sub start { |
44 | my $self = shift; |
45 | |
46 | $self->explode() if rand(1) > .5; |
47 | |
48 | $self->_start(); |
49 | } |
50 | |
51 | package Restartable::ButBroken; |
52 | use Moose::Role; |
53 | |
54 | with 'Restartable' => { excludes => [ 'stop', 'start' ] }; |
55 | |
56 | sub stop { |
57 | my $self = shift; |
58 | |
59 | $self->explode(); |
60 | } |
61 | |
62 | sub start { |
63 | my $self = shift; |
64 | |
65 | $self->explode(); |
66 | } |
67 | |
68 | =head1 DESCRIPTION |
69 | |
70 | Sometimes when you include a role in a class, you may want to leave |
71 | out some of its methods. In this example, we have a role C<Restartable> |
72 | which provides an C<is_paused> attribute, and two methods, C<stop> and |
73 | C<start>. The implementation of those two methods is irrelevant. |
74 | |
75 | Then we have two more roles which also implement the same interface, |
76 | each putting their own spin on the C<stop> and C<start> method. |
77 | |
78 | In the C<Restartable::ButUnreliable> role, we want to provide a new |
79 | implementation of C<stop> and C<start>, but still have access to the |
80 | original implementation. To do this, we alias the methods from |
81 | C<Restartable> to private methods, and provide wrappers around the |
82 | originals (1). |
83 | |
84 | In the C<Restartable::ButBroken> role, we want to provide an entirely |
85 | new behavior for C<stop> and C<start>, so we exclude them when |
86 | composing the C<Restartable> role into C<Restartable::ButBroken>. |
87 | |
88 | It's worth noting that the C<excludes> parameter also accepts a single |
89 | string as an argument if you just want to exclude one method. |
90 | |
91 | =head1 CONCLUSION |
92 | |
93 | Method exclusion and renaming can come in handy, especially when |
94 | building roles out of other roles. In this example, all of our roles |
546a7134 |
95 | implement the C<Restartable> role. Each role provides same API, but |
96 | each has a different implementation under the hood. |
2e3d0a0a |
97 | |
98 | You can also use the method aliasing and excluding features when |
99 | composing a role into a class. |
100 | |
101 | =head1 FOOTNOTES |
102 | |
103 | =over 4 |
104 | |
105 | =item (1) |
106 | |
107 | The mention of wrapper should tell you that we could do the same thing |
108 | using method modifiers, but for the sake of this example, we don't. |
109 | |
110 | =back |
111 | |
112 | =head1 AUTHOR |
113 | |
114 | Dave Rolsky E<lt>autarch@urth.orgE<gt> |
115 | |
116 | =head1 COPYRIGHT AND LICENSE |
117 | |
118 | Copyright 2006-2008 by Infinity Interactive, Inc. |
119 | |
120 | L<http://www.iinteractive.com> |
121 | |
122 | This library is free software; you can redistribute it and/or modify |
123 | it under the same terms as Perl itself. |
124 | |
125 | =cut |