blib/lib/Music/Note/Role/Operators.pm

filter: Statementstmt 100% 48 / 48 Branchbran 83.3% 5 / 6 Conditioncond 83.3% 10 / 12 MC/DCmcdc 57.1% 4 / 7 Subroutinesub 100% 13 / 13 Podpod 12.5% 1 / 8 totaltotal 86.1% 81 / 94 CC 10 SCAR 14.8
CC
10
Cov
95%
get_interval
9.1
_maybe_bail_on_comparison
2.0
BEGIN
1.0
CRAP 4.4
↑ Summary
1 package Music::Note::Role::Operators;
2 2 $Music::Note::Role::Operators::VERSION = '0.04';
3 # ABSTRACT: Adds operator overloading, clone and interval calculation to Music::Note
4  
5 2 ▶use Storable ();
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use Role::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use Music::Intervals;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 2 requires 'format';
10  
11 use overload
12 2 ▶ '>' => 'gt',
Subroutine BEGIN: called CC=1 SCAR=0.0
13 '<' => 'lt',
14 '==' => 'eq',
15 '>=' => 'gte',
16 '<=' => 'lte',
17 '-' => 'subtract',
18 fallback => 1,
19 2 ;
20  
21 =head1 NAME
22  
23 Music::Note::Role::Operators
24  
25 =head2 DESCRIPTION
26  
27 L<Role::Tiny> to be applied on top L<Music::Note> with comparison methods
28 added and overloaded operators. Also adds a clone method and a way to
29 generate Music::Interval objects via a Music::Note.
30  
31 =head2 SYNOPSIS
32  
33 If you're working with a L<Music::Note> subclass:
34  
35 package Music::MyNote;
36 use parent 'Music::Note';
37 use Role::Tiny::With;
38 with 'Music::Note::Role::Operators';
39 # etc
40  
41 Or if you're working in a script and just want the behaviour:
42  
43 use Music::Note;
44 use Role::Tiny (); # Don't import R::T into current namespace for cleanliness
45 Role::Tiny->apply_roles_to_package('Music::Note', 'Music::Note::Role::Operators');
46  
47 =head2 SUMMARY
48  
49 Assuming you're working in a script:
50  
51 my $note = Music::Note->new('C#');
52 my $other = Music::Note->new('E');
53  
54 my $true = $other->gt($note);
55 $true = $other > $note;
56  
57 $true = $note->lt($other);
58 $true = $note < $other;
59  
60 $true = $note->eq($note->clone);
61 $true = $note == $note->clone;
62  
63 $true = $note->gte($note->clone);
64 $true = $note >= $note->clone;
65  
66 $true = $note->lte($note->clone);
67 $true = $note <= $note->clone;
68  
69 my $interval = $note->interval($other);
70 my $conveneince_interval = $note->interval(%args_for_music_interval);
71  
72 =head2 CAVEAT
73  
74 Don't try to do something like C<$note == 90>>. The overloading expects a
75 L<Music::Note> on both sides. To perform comparisons versus note and not a
76 note you should be doing C<< $note->format('midi') == 90 >>.
77  
78 =head3 AUTHOR
79  
80 Kieren Diment L<zarquon@cpan.org>
81  
82 =head3 LICENSE
83  
84 This code can be redistributed on the same terms as perl itself
85  
86 =cut
87  
88 3 ▶sub gt {
Subroutine gt: called CC=1 SCAR=0.0
Pod undocumented
89 3 my ($self, $other) = @_;
90 3 $self->_maybe_bail_on_comparison($other);
91 3 return $self->format('midinum') > $other->format('midinum');
92 }
93  
94 3 ▶sub lt {
Subroutine lt: called CC=1 SCAR=0.0
Pod undocumented
95 3 my ($self, $other) = @_;
96 3 $self->_maybe_bail_on_comparison($other);
97 2 return $self->format('midinum') < $other->format('midinum');
98 }
99  
100 4 ▶sub eq {
Subroutine eq: called CC=1 SCAR=0.0
Pod undocumented
101 4 my ($self, $other) = @_;
102 4 $self->_maybe_bail_on_comparison($other);
103 4 return $self->format('midinum') == $other->format('midinum');
104 }
105  
106 1 ▶sub gte {
Subroutine gte: called CC=1 SCAR=0.0
Pod undocumented
107 1 my ($self, $other) = @_;
108 1 $self->_maybe_bail_on_comparison($other);
109 1 return $self->format('midinum') >= $other->format('midinum');
110 }
111  
112 2 ▶sub lte {
Subroutine lte: called CC=1 SCAR=0.0
Pod undocumented
113 2 my ($self, $other) = @_;
114 2 $self->_maybe_bail_on_comparison($other);
115 2 return $self->format('midinum') <= $other->format('midinum');
116 }
117  
118 4 ▶sub subtract {
Subroutine subtract: called CC=1 SCAR=0.0
Pod undocumented
119 4 my ($self, $other) = @_;
120 4 $self->_maybe_bail_on_comparison($other);
121 4 return $self->format('midinum') - $other->format('midinum');
122 }
123  
124 =head2 get_interval
125  
126 If called with a single Music::Note as argument is returns a
127 Music::Interval object
128  
129 my $interval = $self->get_interval($other);
130  
131 If called with an arguments hash
132  
133 my $interval = $self->get_interval(%args_for_music_interval)
134  
135 Note that this will default to 1 for the following constructor attributes,
136 so if you don't want these values you'll have to explicitly set them to
137 something else in the constructor.
138  
139 NOTE: It would be nice to have the subtract method return a Music::Interval
140 but it's a complex module, and only seems to deal with intervals inside a
141 single octave.
142  
143 =cut
144  
145 2 ▶sub get_interval {
Subroutine get_interval: called CC=9 SCAR=22.1
Pod documented
146 2 my $self = shift;
147 2 my ($other, %args);
148 2 ▶ if (ref $_[0] && $_[0]->isa('Music::Note') ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0] and $_[0]->isa('Music::Note')) { } 1 1 2
Condition66%2/3
ref $_[0] and $_[0]->isa("Music::Note")
!ll&&!rl&&r
101
MC/DC50%1/2
ref $_[0] and $_[0]->isa("Music::Note")
ref $_[0] $_[0]->isa("Music::Note")
Truth table66%2/3
A and B
A= ref $_[0]B= $_[0]->isa("Music::Note")
ABresult
0X0
100
111
149 1 ($other, %args) = @_;
150 }
151 else {
152 1 %args = @_;
153 }
154  
155 2 ▶ $self->_maybe_bail_on_comparison($other) unless $other || $args{notes};
Branch: 1 branch
BranchTrueFalseTotal
unless $other or $args{"notes"} 0 2 2
Condition66%2/3
$other or $args{"notes"}
l!l&&r!l&&!r
110
MC/DC0%0/2
$other or $args{"notes"}
$other $args{"notes"}
Truth table66%2/3
A or B
A= $otherB= $args{"notes"}
ABresult
1X1
011
000
156 2 ▶ $args{notes} ||= [ $self->format('isobase'), $other->format('isobase') ];
Condition100%2/2
$args{"notes"} ||= [$self->format("isobase"), $other->format("isobase")]
l!l
11
MC/DC100%1/1
$args{"notes"} ||= [$self->format("isobase"), $other->format("isobase")]
$args{"notes"}
Truth table100%2/2
A
A= $args{"notes"}
Aresult
11
00
157 2 ▶ $args{$_} ||= 1 for qw/chords equalt freqs interval cents prime integer/;
Condition100%2/2
$args{$_} ||= 1
l!l
77
MC/DC100%1/1
$args{$_} ||= 1
$args{$_}
Truth table100%2/2
A
A= $args{$_}
Aresult
11
00
158 2 ▶ $args{size} ||= scalar @{$args{notes}};
Condition100%2/2
$args{"size"} ||= scalar @{$args{"notes"};}
l!l
11
MC/DC100%1/1
$args{"size"} ||= scalar @{$args{"notes"};}
$args{"size"}
Truth table100%2/2
A
A= $args{"size"}
Aresult
11
00
159 2 my $interval = Music::Intervals->new(%args);
160 2 return $interval;
161 }
162  
163 2 ▶sub clone {
Subroutine clone: called CC=1 SCAR=0.0
Pod undocumented
164 2 return Storable::dclone($_[0]);
165 }
166  
167 17 ▶sub _maybe_bail_on_comparison {
Subroutine _maybe_bail_on_comparison: called CC=2 SCAR=6.9
168 17 my ($self, $other) = @_;
169 17 ▶ die "$other is not a Music::Note" unless $other->isa('Music::Note');
Branch: 1 branch
BranchTrueFalseTotal
unless $other->isa("Music::Note") 1 16 17
170 }
171  
172 2 1;
↑ Summary