-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add tune_body_spec and refactor tune_body and tune_header
- Loading branch information
Showing
3 changed files
with
72 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,54 @@ | ||
|
||
module ABC | ||
class TuneBody | ||
class Note | ||
attr_reader :name, :raw | ||
def initialize(note) | ||
@raw = note | ||
@name = note | ||
end | ||
end | ||
class Measure | ||
attr_reader :notes, :raw | ||
def initialize(measure = "") | ||
@raw = measure | ||
@notes = [] | ||
measure.each_char do |char| | ||
@notes << Note.new(char) if char =~ /[A-G]/i | ||
end | ||
end | ||
end | ||
class TuneBody | ||
attr_reader :notes, :measures, :raw | ||
def initialize(body) | ||
@raw = body | ||
@notes = [] | ||
@measures = [] | ||
body.each_line do |line| | ||
#binding.pry | ||
unless line.match(/^[A-Z]:/i) | ||
line.chomp.split(/\|+/).each do |measure| | ||
@measures << Measure.new(measure) | ||
end | ||
if line.match(/:\|$/) | ||
line.chomp.split(/\|+/).each do |measure| | ||
@measures << Measure.new(measure) | ||
end | ||
end | ||
end | ||
#repeat = (line.match(/:\|$/)) ? 2 : 1 | ||
#repeat.times do | ||
# unless line.match(/^[A-Z]:/i) | ||
# line.each_char do |char| | ||
# repeat_line = false | ||
# @measures << Measure.new if char =~ /\|/ | ||
# @notes << Note.new(char) if char =~ /[A-G]/i | ||
# end | ||
# end | ||
#end | ||
end | ||
@measures.each do |measure| | ||
@notes += measure.notes | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
require 'spec_helper' | ||
|
||
describe ABC::TuneBody do | ||
let(:body) {ABC::File.new("spec/fixtures/english.abc").tunes[0].body} | ||
it "should start with a B" do | ||
expect(body.notes[0].name).to eq "B" | ||
end | ||
it "should have 24 measures" do | ||
expect(body.measures.size).to eq 24 | ||
end | ||
it "should repeat the first 4 measures twice" | ||
end |