docs-n-attr-refactor
[gitmo/Moose.git] / lib / Moose / Cookbook / Recipe6.pod
1
2 =pod
3
4 =head1 NAME
5
6 Moose::Cookbook::Recipe6 - The Moose::Role example
7
8 =head1 SYNOPSIS
9
10   package Eq;
11   use strict;
12   use warnings;
13   use Moose::Role;
14   
15   requires 'equal_to';
16   
17   sub not_equal_to { 
18       my ($self, $other) = @_;
19       not $self->equal_to($other);
20   }
21   
22   package Comparable;
23   use strict;
24   use warnings;
25   use Moose::Role;
26   
27   with 'Eq';
28   
29   requires 'compare';
30   
31   sub equal_to {
32       my ($self, $other) = @_;
33       $self->compare($other) == 0;
34   }    
35   
36   sub greater_than {
37       my ($self, $other) = @_;
38       $self->compare($other) == 1;
39   }    
40   
41   sub less_than {
42       my ($self, $other) = @_;
43       $self->compare($other) == -1;
44   }
45   
46   sub greater_than_or_equal_to {
47       my ($self, $other) = @_;
48       $self->greater_than($other) || $self->equal_to($other);
49   }        
50   
51   sub less_than_or_equal_to {
52       my ($self, $other) = @_;
53       $self->less_than($other) || $self->equal_to($other);
54   }  
55   
56   package Printable;
57   use strict;
58   use warnings;
59   use Moose::Role;
60   
61   requires 'to_string';    
62   
63   package US::Currency;
64   use strict;
65   use warnings;
66   use Moose;
67   
68   with 'Comparable', 'Printable';
69   
70   has 'amount' => (is => 'rw', isa => 'Num', default => 0);
71   
72   sub compare {
73       my ($self, $other) = @_;
74       $self->amount <=> $other->amount;
75   }
76   
77   sub to_string {
78       my $self = shift;
79       sprintf '$%0.2f USD' => $self->amount
80   }
81   
82 =head1 DESCRIPTION
83
84 Coming Soon. 
85
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