blib/lib/Badger/Comparable.pm untested

filter: Statementstmt 0.0% 0 / 12 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 8 Podpod 0.0% 0 / 7 totaltotal 0.0% 0 / 27 CC 1 SCAR 6.9
CC
1
Cov
0%
compare
2.0
equal
2.0
not_equal
2.0
CRAP 2.0
1 package Badger::Comparable;
2  
3 use Badger::Class
4 version => 0.01,
5 debug => 0,
6 import => 'CLASS',
7 base => 'Badger::Base',
8 utils => 'numlike is_object',
9 methods => {
10 eq => \&equal,
11 ne => \&not_equal,
12 lt => \&before,
13 gt => \&after,
14 le => \&not_after,
15 ge => \&not_before,
16 cmp => \&compare,
17 },
18 overload => {
19 '==' => \&equal,
20 '!=' => \&not_equal,
21 '<' => \&before,
22 '>' => \&after,
23 '<=' => \&not_after,
24 '>=' => \&not_before,
25 '<=>' => \&compare,
26 fallback => 1,
27 };
28  
29  
30 sub compare {
31 my $self = shift;
32 shift->not_implemented;
33 }
34  
35  
36 sub equal {
37 shift->compare(@_) == 0;
38 }
39  
40  
41 sub not_equal {
42 shift->compare(@_) != 0;
43 }
44  
45  
46 sub before {
47 shift->compare(@_) == -1;
48 }
49  
50  
51 sub after {
52 shift->compare(@_) == 1;
53 }
54  
55  
56 sub not_before {
57 shift->compare(@_) >= 0;
58 }
59  
60  
61 sub not_after {
62 shift->compare(@_) <= 0;
63 }
64  
65  
66 1;
67  
68  
69 =head1 NAME
70  
71 Badger::Comparable - base class for comparable objects
72  
73 =head1 SYNOPSIS
74  
75 package Your::Comparable::Object;
76 use base 'Badger::Comparable';
77  
78 # You must define a compare method that returns -1, 0 or +1
79 # if the object is less than, equal to, or greater than the
80 # object passed as an argument.
81  
82 sub compare {
83 my ($this, $that) = @_;
84
85 # for example: comparing by a surname field
86 return $this->surname
87 cmp $that->surname;
88 }
89  
90 package main;
91  
92 # assume $obj1 and $obj2 are instance of above object class
93 if ($obj1 < $obj2) {
94 # do something
95 }
96  
97 =head1 DESCRIPTION
98  
99 This module implements a base class for comparable objects. Subclasses need
100 only define a L<compare()> method and can inherit all the other methods
101 provided. Overloaded comparison operators are also defined.
102  
103 =head1 METHODS
104  
105 =head2 compare($that)
106  
107 This method must be defined by subclasses. It received the implicit C<$self>
108 object reference as the first argument and the object it is being compared to
109 as the second.
110  
111 The method can do whatever is necessary to compare the two objects. It should
112 return C<-1> if the C<$self> object should be ordered I<before> the C<$that>
113 object, C<+1> if it should be ordered I<after>, or 0 if the two objects are
114 considered the same.
115  
116 =head2 equal($that)
117  
118 Wrapper around L<compare()> that returns true if the two objects are equal
119 (L<compare()> returns C<0>).
120  
121 =head2 not_equal($that)
122  
123 Wrapper around L<compare()> that returns true if the two objects are not
124 equal (L<compare()> returns any non-zero value).
125  
126 =head2 before($that)
127  
128 Wrapper around L<compare()> that returns true if the C<$self> object is ordered
129 before the C<$that> object passed as an argument (L<compare()> returns C<-1>).
130  
131 =head2 not_before($that)
132  
133 Wrapper around L<compare()> that returns the logical opposite of the
134 L<before()> method, returning a true value if the C<$self> object is greater
135 than or equal to the L<$that> object passed as an argument (L<compare()>
136 returns C<0> or C<+1>).
137  
138 =head2 after($that)
139  
140 Wrapper around L<compare()> that returns true if the C<$self> object is ordered
141 after the C<$that> object passed as an argument (L<compare()> returns C<+1>).
142  
143 =head2 not_after($that)
144  
145 Wrapper around L<compare()> that returns the logical opposite of the
146 L<after()> method, returning a true value if the C<$self> object is less
147 than or equal to the L<$that> object passed as an argument (L<compare()>
148 returns C<-1> or C<0>).
149  
150 =head1 OVERLOADED OPERATORS
151  
152 =head2 ==
153  
154 This is mapped to the L<equal()> method.
155  
156 if ($obja == $objb) {
157 # do something
158 }
159  
160 =head2 !=
161  
162 This is mapped to the L<not_equal()> method.
163  
164 if ($obja != $objb) {
165 # do something
166 }
167  
168 =head2 <
169  
170 This is mapped to the L<before()> method.
171  
172 if ($obja < $objb) {
173 # do something
174 }
175  
176 =head2 >
177  
178 This is mapped to the L<after()> method.
179  
180 if ($obja > $objb) {
181 # do something
182 }
183  
184 =head2 <=
185  
186 This is mapped to the L<not_after()> method.
187  
188 if ($obja <= $objb) {
189 # do something
190 }
191  
192 =head2 >=
193  
194 This is mapped to the L<not_before()> method.
195  
196 if ($obja >= $objb) {
197 # do something
198 }
199  
200 =head1 AUTHOR
201  
202 Andy Wardley L<http://wardley.org>
203  
204 =head1 COPYRIGHT
205  
206 Copyright (C) 2013 Andy Wardley. All Rights Reserved.
207  
208 This module is free software; you can redistribute it and/or modify it
209 under the same terms as Perl itself.
210  
211 =cut