1 package Moose::Cookbook::Roles::Comparable_CodeReuse;
3 # ABSTRACT: Using roles for code reuse
18 my ( $self, $other ) = @_;
19 not $self->equal_to($other);
30 my ( $self, $other ) = @_;
31 $self->compare($other) == 0;
35 my ( $self, $other ) = @_;
36 $self->compare($other) == 1;
40 my ( $self, $other ) = @_;
41 $self->compare($other) == -1;
44 sub greater_than_or_equal_to {
45 my ( $self, $other ) = @_;
46 $self->greater_than($other) || $self->equal_to($other);
49 sub less_than_or_equal_to {
50 my ( $self, $other ) = @_;
51 $self->less_than($other) || $self->equal_to($other);
62 with 'Comparable', 'Printable';
64 has 'amount' => ( is => 'rw', isa => 'Num', default => 0 );
67 my ( $self, $other ) = @_;
68 $self->amount <=> $other->amount;
73 sprintf '$%0.2f USD' => $self->amount;
78 Roles have two primary purposes: as interfaces, and as a means of code
79 reuse. This recipe demonstrates the latter, with roles that define
80 comparison and display code for objects.
82 Let's start with C<Eq>. First, note that we've replaced C<use Moose>
83 with C<use Moose::Role>. We also have a new sugar function, C<requires>:
87 This says that any class which consumes this role must provide an
88 C<equal_to> method. It can provide this method directly, or by
89 consuming some other role.
91 The C<Eq> role defines its C<not_equal_to> method in terms of the
92 required C<equal_to> method. This lets us minimize the methods that
93 consuming classes must provide.
95 The next role, C<Comparable>, builds on the C<Eq> role. We include
96 C<Eq> in C<Comparable> using C<with>, another new sugar function:
100 The C<with> function takes a list of roles to consume. In our example,
101 the C<Comparable> role provides the C<equal_to> method required by
102 C<Eq>. However, it could opt not to, in which case a class that
103 consumed C<Comparable> would have to provide its own C<equal_to>. In
104 other words, a role can consume another role I<without> providing any
107 The C<Comparable> role requires a method, C<compare>:
111 The C<Comparable> role also provides a number of other methods, all of
112 which ultimately rely on C<compare>.
115 my ( $self, $other ) = @_;
116 $self->compare($other) == 0;
120 my ( $self, $other ) = @_;
121 $self->compare($other) == 1;
125 my ( $self, $other ) = @_;
126 $self->compare($other) == -1;
129 sub greater_than_or_equal_to {
130 my ( $self, $other ) = @_;
131 $self->greater_than($other) || $self->equal_to($other);
134 sub less_than_or_equal_to {
135 my ( $self, $other ) = @_;
136 $self->less_than($other) || $self->equal_to($other);
139 Finally, we define the C<Printable> role. This role exists solely to
140 provide an interface. It has no methods, just a list of required methods.
141 In this case, it just requires a C<to_string> method.
143 An interface role is useful because it defines both a method and a
144 I<name>. We know that any class which does this role has a
145 C<to_string> method, but we can also assume that this method has the
146 semantics we want. Presumably, in real code we would define those
147 semantics in the documentation for the C<Printable> role. (1)
149 Finally, we have the C<US::Currency> class which consumes both the
150 C<Comparable> and C<Printable> roles.
152 with 'Comparable', 'Printable';
154 It also defines a regular Moose attribute, C<amount>:
156 has 'amount' => ( is => 'rw', isa => 'Num', default => 0 );
158 Finally we see the implementation of the methods required by our
159 roles. We have a C<compare> method:
162 my ( $self, $other ) = @_;
163 $self->amount <=> $other->amount;
166 By consuming the C<Comparable> role and defining this method, we gain
167 the following methods for free: C<equal_to>, C<greater_than>,
168 C<less_than>, C<greater_than_or_equal_to> and
169 C<less_than_or_equal_to>.
171 Then we have our C<to_string> method:
175 sprintf '$%0.2f USD' => $self->amount;
180 Roles can be very powerful. They are a great way of encapsulating
181 reusable behavior, as well as communicating (semantic and interface)
182 information about the methods our classes provide.
190 Consider two classes, C<Runner> and C<Process>, both of which define a
191 C<run> method. If we just require that an object implements a C<run>
192 method, we still aren't saying anything about what that method
193 I<actually does>. If we require an object that implements the
194 C<Executable> role, we're saying something about semantics.
200 ok( US::Currency->does('Comparable'), '... US::Currency does Comparable' );
201 ok( US::Currency->does('Eq'), '... US::Currency does Eq' );
202 ok( US::Currency->does('Printable'), '... US::Currency does Printable' );
204 my $hundred = US::Currency->new( amount => 100.00 );
205 isa_ok( $hundred, 'US::Currency' );
207 ok( $hundred->DOES("US::Currency"), "UNIVERSAL::DOES for class" );
208 ok( $hundred->DOES("Comparable"), "UNIVERSAL::DOES for role" );
210 can_ok( $hundred, 'amount' );
211 is( $hundred->amount, 100, '... got the right amount' );
213 can_ok( $hundred, 'to_string' );
214 is( $hundred->to_string, '$100.00 USD',
215 '... got the right stringified value' );
217 ok( $hundred->does('Comparable'), '... US::Currency does Comparable' );
218 ok( $hundred->does('Eq'), '... US::Currency does Eq' );
219 ok( $hundred->does('Printable'), '... US::Currency does Printable' );
221 my $fifty = US::Currency->new( amount => 50.00 );
222 isa_ok( $fifty, 'US::Currency' );
224 can_ok( $fifty, 'amount' );
225 is( $fifty->amount, 50, '... got the right amount' );
227 can_ok( $fifty, 'to_string' );
228 is( $fifty->to_string, '$50.00 USD', '... got the right stringified value' );
230 ok( $hundred->greater_than($fifty), '... 100 gt 50' );
231 ok( $hundred->greater_than_or_equal_to($fifty), '... 100 ge 50' );
232 ok( !$hundred->less_than($fifty), '... !100 lt 50' );
233 ok( !$hundred->less_than_or_equal_to($fifty), '... !100 le 50' );
234 ok( !$hundred->equal_to($fifty), '... !100 eq 50' );
235 ok( $hundred->not_equal_to($fifty), '... 100 ne 50' );
237 ok( !$fifty->greater_than($hundred), '... !50 gt 100' );
238 ok( !$fifty->greater_than_or_equal_to($hundred), '... !50 ge 100' );
239 ok( $fifty->less_than($hundred), '... 50 lt 100' );
240 ok( $fifty->less_than_or_equal_to($hundred), '... 50 le 100' );
241 ok( !$fifty->equal_to($hundred), '... !50 eq 100' );
242 ok( $fifty->not_equal_to($hundred), '... 50 ne 100' );
244 ok( !$fifty->greater_than($fifty), '... !50 gt 50' );
245 ok( $fifty->greater_than_or_equal_to($fifty), '... !50 ge 50' );
246 ok( !$fifty->less_than($fifty), '... 50 lt 50' );
247 ok( $fifty->less_than_or_equal_to($fifty), '... 50 le 50' );
248 ok( $fifty->equal_to($fifty), '... 50 eq 50' );
249 ok( !$fifty->not_equal_to($fifty), '... !50 ne 50' );
251 ## ... check some meta-stuff
255 my $eq_meta = Eq->meta;
256 isa_ok( $eq_meta, 'Moose::Meta::Role' );
258 ok( $eq_meta->has_method('not_equal_to'), '... Eq has_method not_equal_to' );
259 ok( $eq_meta->requires_method('equal_to'),
260 '... Eq requires_method not_equal_to' );
264 my $comparable_meta = Comparable->meta;
265 isa_ok( $comparable_meta, 'Moose::Meta::Role' );
267 ok( $comparable_meta->does_role('Eq'), '... Comparable does Eq' );
269 foreach my $method_name (
271 equal_to not_equal_to
272 greater_than greater_than_or_equal_to
273 less_than less_than_or_equal_to
276 ok( $comparable_meta->has_method($method_name),
277 '... Comparable has_method ' . $method_name );
280 ok( $comparable_meta->requires_method('compare'),
281 '... Comparable requires_method compare' );
285 my $printable_meta = Printable->meta;
286 isa_ok( $printable_meta, 'Moose::Meta::Role' );
288 ok( $printable_meta->requires_method('to_string'),
289 '... Printable requires_method to_string' );
293 my $currency_meta = US::Currency->meta;
294 isa_ok( $currency_meta, 'Moose::Meta::Class' );
296 ok( $currency_meta->does_role('Comparable'),
297 '... US::Currency does Comparable' );
298 ok( $currency_meta->does_role('Eq'), '... US::Currency does Eq' );
299 ok( $currency_meta->does_role('Printable'),
300 '... US::Currency does Printable' );
302 foreach my $method_name (
305 equal_to not_equal_to
307 greater_than greater_than_or_equal_to
308 less_than less_than_or_equal_to
312 ok( $currency_meta->has_method($method_name),
313 '... US::Currency has_method ' . $method_name );