Commit | Line | Data |
a7d0cd00 |
1 | |
2 | =pod |
3 | |
4 | =head1 NAME |
5 | |
6 | Moose::Cookbook::Recipe6 - The Moose::Role example |
7 | |
8 | =head1 SYNOPSIS |
9e93dd19 |
9 | |
446e850f |
10 | package Eq; |
a7d0cd00 |
11 | use strict; |
12 | use warnings; |
13 | use Moose::Role; |
14 | |
446e850f |
15 | requires 'equal_to'; |
a7d0cd00 |
16 | |
446e850f |
17 | sub not_equal_to { |
18 | my ($self, $other) = @_; |
9e93dd19 |
19 | not $self->equal_to($other); |
a7d0cd00 |
20 | } |
21 | |
9e93dd19 |
22 | package Comparable; |
a7d0cd00 |
23 | use strict; |
24 | use warnings; |
25 | use Moose::Role; |
26 | |
446e850f |
27 | with 'Eq'; |
a7d0cd00 |
28 | |
446e850f |
29 | requires 'compare'; |
a7d0cd00 |
30 | |
446e850f |
31 | sub equal_to { |
32 | my ($self, $other) = @_; |
33 | $self->compare($other) == 0; |
34 | } |
a7d0cd00 |
35 | |
446e850f |
36 | sub greater_than { |
37 | my ($self, $other) = @_; |
38 | $self->compare($other) == 1; |
39 | } |
a7d0cd00 |
40 | |
446e850f |
41 | sub less_than { |
42 | my ($self, $other) = @_; |
43 | $self->compare($other) == -1; |
a7d0cd00 |
44 | } |
45 | |
446e850f |
46 | sub greater_than_or_equal_to { |
47 | my ($self, $other) = @_; |
48 | $self->greater_than($other) || $self->equal_to($other); |
49 | } |
a7d0cd00 |
50 | |
446e850f |
51 | sub less_than_or_equal_to { |
52 | my ($self, $other) = @_; |
53 | $self->less_than($other) || $self->equal_to($other); |
9e93dd19 |
54 | } |
55 | |
56 | package Printable; |
57 | use strict; |
58 | use warnings; |
59 | use Moose::Role; |
60 | |
61 | requires 'to_string'; |
a7d0cd00 |
62 | |
446e850f |
63 | package US::Currency; |
a7d0cd00 |
64 | use strict; |
65 | use warnings; |
66 | use Moose; |
67 | |
9e93dd19 |
68 | with 'Comparable', 'Printable'; |
a7d0cd00 |
69 | |
9e93dd19 |
70 | has 'amount' => (is => 'rw', isa => 'Num', default => 0); |
446e850f |
71 | |
72 | sub compare { |
73 | my ($self, $other) = @_; |
74 | $self->amount <=> $other->amount; |
75 | } |
a7d0cd00 |
76 | |
9e93dd19 |
77 | sub to_string { |
78 | my $self = shift; |
79 | sprintf '$%0.2f USD' => $self->amount |
80 | } |
81 | |
a7d0cd00 |
82 | =head1 DESCRIPTION |
83 | |
84 | Coming Soon. |
85 | |
a7d0cd00 |
86 | =head1 AUTHOR |
87 | |
88 | Stevan Little E<lt>stevan@iinteractive.comE<gt> |
89 | |
90 | =head1 COPYRIGHT AND LICENSE |
91 | |
92 | Copyright 2006 by Infinity Interactive, Inc. |
93 | |
94 | L<http://www.iinteractive.com> |
95 | |
96 | This library is free software; you can redistribute it and/or modify |
97 | it under the same terms as Perl itself. |
98 | |
99 | =cut |
100 | |