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