Skip to content

Commit

Permalink
add tune_body_spec and refactor tune_body and tune_header
Browse files Browse the repository at this point in the history
  • Loading branch information
domgetter committed Aug 7, 2014
1 parent 0479b40 commit 2be649f
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 7 deletions.
49 changes: 48 additions & 1 deletion lib/abc/tune_body.rb
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
18 changes: 12 additions & 6 deletions lib/abc/tune_header.rb
Original file line number Diff line number Diff line change
@@ -1,20 +1,26 @@

module ABC
class TuneHeader
attr_reader :reference, :titles, :meter, :key
attr_reader :reference, :titles, :meter, :key, :source, :notes, :rhythm
def initialize(header)
header.each_line do |line|
#binding.pry
case line
#when /^[A-Za-z]:/
# @fields ||= []
# @fields << InformationField.new(line)
when /^X:(?<reference>\d+)/ then @reference = $~[:reference].to_i
when /^T:(?<title>.*?)(?=\s*%.*)/
when /^T:(?<title>[^%]*)(%.*)?/
@titles ||= []
@titles << $~[:title]
@titles << $~[:title].strip
when /^C:/
when /^S:/
when /^S:(?<source>[^%]*)(%.*)?/ then @source = $~[:source].strip
when /^M:(?<meter>\S+)/ then @meter = $~[:meter]
when /^R:/
when /^N:/
when /^R:(?<rhythm>[^%]*)(%.*)?/
@rhythm = $~[:rhythm].strip
when /^N:(?<note>[^%]*)(%.*)?/
@notes ||= []
@notes << $~[:note].strip
when /^K:(?<key>\S+)/ then @key = $~[:key]
end
end
Expand Down
12 changes: 12 additions & 0 deletions spec/abc/tune_body_spec.rb
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

0 comments on commit 2be649f

Please sign in to comment.