ChordBot/Opus.pm

filter: Statementstmt 93.7% 45 / 48 Branchbran 75.0% 6 / 8 Conditioncond 0.0% 0 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 92.8% 13 / 14 Podpod 100% 7 / 7 totaltotal 88.7% 71 / 80 CC 7 SCAR 6.9
CC
7
Cov
88%
_wrapup
4.0
export
3.0
json
2.0
CRAP 2.0
« ChordBot.pm ↑ Summary ChordBot/Opus/Section/Chord.pm »
1 #! perl
2  
3 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 5 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 package Music::ChordBot::Opus;
8  
9 =head1 NAME
10  
11 Music::ChordBot::Opus - API for generating ChordBot songs.
12  
13 =cut
14  
15 5 our $VERSION = 0.01;
16  
17 5 ▶use parent 'Music::ChordBot::Opus::Base';
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 =head1 SYNOPSIS
20  
21 use Music::ChordBot::Opus;
22 my $song = Music::ChordBot::Opus->new;
23 $song->name("Perl Song");
24 $song->tempo(120);
25 $song->add_section(...);
26 print $song->json, "\n";
27  
28 =head1 METHODS
29  
30 =head2 new [ args ]
31  
32 Creates a new Music::ChordBot::Opus object.
33  
34 Initial attributes may be passed as a hash.
35  
36 Attributes:
37  
38 =over 4
39  
40 =item name
41  
42 The name of the song.
43  
44 =item editMode
45  
46 Zero if the song has a single style, one if some sections have their
47 own styles.
48  
49 =item tempo
50  
51 The tempo in beats per minute.
52  
53 =item fileType
54  
55 This must be the literal string 'chordbot-song'.
56  
57 =back
58  
59 =cut
60  
61 5 ▶sub new {
Subroutine new: called CC=2 SCAR=6.9
Pod documented
62 5 my ( $pkg, %init ) = @_;
63 5 ▶ $init{songName} = delete $init{name} if exists $init{name};
Branch: 1 branch
BranchTrueFalseTotal
if exists $init{"name"} 3 2 5
64 5 my $data = { fileType => "chordbot-song",
65 editMode => 0,
66 tempo => 80,
67 sections => [],
68 %init };
69 5 bless { data => $data }, $pkg;
70 }
71  
72 =head2 name [ value ]
73  
74 Sets or gets the name of the song.
75  
76 =cut
77  
78 2 ▶sub name { shift->_setget( "songName", @_ ) }
Subroutine name: called CC=1 SCAR=0.0
Pod documented
79  
80 =head2 tempo [ value ]
81  
82 Sets or gets the tempo of the song.
83  
84 =cut
85  
86 3 ▶sub tempo { shift->_setget( "tempo", @_ ) }
Subroutine tempo: called CC=1 SCAR=0.0
Pod documented
87  
88 =head2 editmode [ value ]
89  
90 Sets or gets attribute editMode. This is not normally necessary since
91 it is dealt with automatically.
92  
93 =cut
94  
95 2 ▶sub editmode { shift->_setget( "editMode", @_ ) }
Subroutine editmode: called CC=1 SCAR=0.0
Pod documented
96  
97 =head2 add_section section
98  
99 Adds a new section to the song. See L<Music::ChordBot::Opus::Section>
100 for details on sections.
101  
102 =cut
103  
104 6 ▶sub add_section {
Subroutine add_section: called CC=1 SCAR=0.0
Pod documented
105 6 my ( $self, $section ) = @_;
106 6 push( @{$self->{data}->{sections}}, $section->{data} );
107 }
108  
109 # head2 _wrapup
110 #
111 # Internal helper to fix some attributes.
112  
113 8 ▶sub _wrapup {
Subroutine _wrapup: called CC=4 SCAR=13.9
114 8 my ( $self ) = @_;
115  
116 # If there are more than one sections with a style associated,
117 # editMode most be set to 1.
118 8 my $styles = 0;
119 8 foreach ( @{ $self->{data}->{sections} } ) {
120 10 ▶ if ( exists $_->{style} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $_->{"style"}) 10 0 10
121 10 ▶ if ( ++$styles > 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (++$styles > 1) 2 8 10
122 2 $self->editmode(1);
123 2 last;
124 }
125 }
126 }
127 }
128  
129 =head2 export [ dumpname ]
130  
131 Produces a string representing the song, in Data::Dumper format.
132  
133 =cut
134  
135 0 ▶sub export {
Subroutine export: not called CC=2 SCAR=10.9
Pod documented
136 0 my ( $self, $pretty ) = @_;
137 0 $self->_wrapup;
138 5 ▶ use Data::Dumper ();
Subroutine BEGIN: called CC=1 SCAR=0.0
139 0 ▶ Data::Dumper->Dump( [ $self->{data} ], [ $pretty || "opus" ] );
Condition0%0/2
$pretty || 'opus'
l!l
00
MC/DC0%0/1
$pretty || 'opus'
$pretty
Truth table0%0/2
A
A= $pretty
Aresult
11
00
140 }
141  
142 =head2 json [ pretty ]
143  
144 Produces a string representing the song, in JSON format, suitable
145 for import into the ChordBot app.
146  
147 If argument is true, the JSON is pretty-printed for readability.
148 ChordBot doesn't mind.
149  
150 =cut
151  
152 8 ▶sub json {
Subroutine json: called CC=2 SCAR=6.9
Pod documented
153 8 my ( $self, $pretty ) = @_;
154 8 $self->_wrapup;
155 5 ▶ use JSON ();
Subroutine BEGIN: called CC=1 SCAR=0.0
156 8 my $json = JSON->new;
157 8 $json->canonical(1);
158 8 ▶ $json = $json->pretty if $pretty;
Branch: 1 branch
BranchTrueFalseTotal
if $pretty 0 8 8
159 8 $json->encode($self->{data});
160 }
161  
162 5 1;
163  
164 =head1 SEE ALSO
165  
166 L<Music::ChordBot> for general information.
167  
168 L<Music::ChordBot::Song> for an easy to use API.
169  
170 =head1 DISCLAIMER
171  
172 There is currently NO VALIDATION of argument values. Illegal values
173 will result in program crashes and songs that cannot be imported, or
174 played, by ChordBot.
175  
176 =head1 AUTHOR, COPYRIGHT & LICENSE
177  
178 See L<Music::ChordBot>.
179  
180 =cut
181  
182 5 1;
« ChordBot.pm ↑ Summary ChordBot/Opus/Section/Chord.pm »