File Coverage

blib/lib/Music/Note/Role/Operators.pm
Criterion Covered Total %
statement 27 27 100.0
branch 2 2 100.0
condition n/a
subroutine 10 10 100.0
pod 0 6 0.0
total 39 45 86.6


line stmt bran cond sub pod time code
1             package Music::Note::Role::Operators;
2             $Music::Note::Role::Operators::VERSION = '0.02';
3             # ABSTRACT: Adds operator overloading and clone to Music::Note
4              
5 2     2   1879 use Storable ();
  2         4552  
  2         41  
6 2     2   8 use Role::Tiny;
  2         2  
  2         11  
7             requires 'format';
8              
9             use overload
10 2         10 '>' => 'gt',
11             '<' => 'lt',
12             '==' => 'eq',
13             '>=' => 'gte',
14             '<=' => 'lte',
15             fallback => 1,
16 2     2   248 ;
  2         2  
17              
18             =head1 NAME
19              
20             Music::Note::Role::Operators
21              
22             =head2 DESCRIPTION
23              
24             L to be applied on top L with comparison methods
25             added and overloaded operators. Also adds a clone method.
26              
27             =head2 SYNOPSIS
28              
29             If you're working with a L subclass:
30              
31             package Music::MyNote;
32             use parent 'Music::Note';
33             use Role::Tiny::With;
34             with 'Music::Note::Role::Operators';
35             # etc
36              
37             Or if you're working in a script and just want the behaviour:
38              
39             use Music::Note;
40             use Role::Tiny (); # Don't import R::T into current namespace for cleanliness
41             Role::Tiny->apply_roles_to_package('Music::Note', 'Music::Note::Role::Operators');
42              
43             =head2 SUMMARY
44              
45             Assuming you're working in a script:
46              
47             my $note = Music::Note->new('C#');
48             my $other = Music::Note->new('E');
49              
50             my $true = $other->gt($note);
51             $true = $other > $note;
52              
53             $true = $note->lt($other);
54             $true = $note < $other;
55              
56             $true = $note->eq($note->clone);
57             $true = $note == $note->clone;
58              
59             $true = $note->gte($note->clone);
60             $true = $note >= $note->clone;
61              
62             $true = $note->lte($note->clone);
63             $true = $note <= $note->clone;
64              
65             =head2 CAVEAT
66              
67             Don't try to do something like C<$note == 90>>. The overloading expects a
68             L on both sides. To perform comparisons versus note and not a
69             note you should be doing C<< $note->format('midi') == 90 >>.
70              
71             =head3 AUTHOR
72              
73             Kieren Diment L
74              
75             =head3 LICENSE
76              
77             This code can be redistributed on the same terms as perl itself
78              
79             =cut
80              
81             sub gt {
82 3     3 0 1251 my ($self, $other) = @_;
83 3         6 $self->_maybe_bail_on_comparison($other);
84 3         7 return $self->format('midinum') > $other->format('midinum');
85             }
86              
87             sub lt {
88 3     3 0 592 my ($self, $other) = @_;
89 3         6 $self->_maybe_bail_on_comparison($other);
90 2         4 return $self->format('midinum') < $other->format('midinum');
91             }
92              
93             sub eq {
94 4     4 0 758 my ($self, $other) = @_;
95 4         7 $self->_maybe_bail_on_comparison($other);
96 4         9 return $self->format('midinum') == $other->format('midinum');
97             }
98              
99             sub gte {
100 1     1 0 259 my ($self, $other) = @_;
101 1         2 $self->_maybe_bail_on_comparison($other);
102 1         2 return $self->format('midinum') >= $other->format('midinum');
103             }
104              
105             sub lte {
106 2     2 0 194 my ($self, $other) = @_;
107 2         3 $self->_maybe_bail_on_comparison($other);
108 2         4 return $self->format('midinum') <= $other->format('midinum');
109             }
110              
111             sub clone {
112 2     2 0 645 return Storable::dclone($_[0]);
113             }
114              
115             sub _maybe_bail_on_comparison {
116 13     13   8 my ($self, $other) = @_;
117 13 100       61 die "$other is not a Music::Note" unless $other->isa('Music::Note');
118             }
119              
120             1;