-
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.
added specs for ABC::File and added tune_spec
- Loading branch information
Showing
7 changed files
with
100 additions
and
38 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 |
---|---|---|
|
@@ -5,7 +5,8 @@ Gem::Specification.new do |spec| | |
spec.version = '0.0.1' | ||
spec.date = '2014-08-05' | ||
spec.summary = 'ABC file parser' | ||
spec.description = 'Converts ABC music files into Ruby objects for programatic use.' | ||
spec.description = 'Converts ABC music files into Ruby objects for ' + | ||
'programatic use.' | ||
spec.authors = ["Dominic Muller"] | ||
spec.email = '[email protected]' | ||
spec.files = [ | ||
|
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,17 +1,43 @@ | ||
|
||
module ABC | ||
# An abc file consists of one or more abc tune transcriptions, | ||
# optionally interspersed with free text and typeset text | ||
# annotations. It may optionally start with a file header to | ||
# set up default values for processing the file. | ||
# | ||
# The file header, abc tunes and text annotations are separated | ||
# from each other by empty lines (also known as blank lines). | ||
# | ||
# An abc file with more than one tune in it is called an abc | ||
# tunebook. | ||
# | ||
class File | ||
|
||
attr_reader :parsed, :version, :tunes, :header | ||
def initialize(filepath) | ||
@filepath = filepath | ||
@file = ::File.open(@filepath, "r") {|f| f.read} | ||
@parsed = false | ||
parse | ||
end | ||
def parsed? | ||
parsed | ||
end | ||
private | ||
def parse | ||
@sections = @file.split(/\n(?=\n)/) | ||
@sections.each do |section| | ||
case section_type(section) | ||
when :tune | ||
@tunes ||= [] | ||
@tunes << Tune.new(section) | ||
when :file_header | ||
@header = FileHeader.new(section) | ||
else | ||
end | ||
end | ||
@parsed = true | ||
end | ||
def section_type(section) | ||
type = :unknown | ||
section.each_line do |line| | ||
if m = line.match(/%abc(-(?<version>[\d.]+))?/) | ||
@version ||= m[:version].to_f | ||
end | ||
case line | ||
when /^H:|^O:/ then type = :file_header | ||
when /^X:|^T:|^K:/ then type = :tune | ||
end | ||
end | ||
type | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,7 +18,9 @@ module ABC | |
# distributed. | ||
# | ||
class FileHeader | ||
|
||
def initialize(header) | ||
|
||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,8 @@ | ||
|
||
module ABC | ||
# An abc tune itself consists of a tune header and a tune body, | ||
# terminated by an empty line or the end of the file. It may | ||
# also contain comment lines or stylesheet directives. | ||
# | ||
# The tune header is composed of several information field lines, | ||
# which are further discussed in information fields. The tune | ||
# header should start with an X:(reference number) field followed | ||
# by a T:(title) field and finish with a K:(key) field. | ||
# | ||
# The tune body, which contains the music code, follows immediately | ||
# after. Certain fields may also be used inside the tune body - see | ||
# use of fields within the tune body. | ||
# | ||
# It is legal to write an abc tune without a tune body. This | ||
# feature can be used to document tunes without transcribing them. | ||
# | ||
# Abc music code lines are those lines in the tune body which give | ||
# notes, bar lines and other musical symbols - see the tune body | ||
# for details. In effect, music code is the contents of any line | ||
# which is not an information field, stylesheet directive or | ||
# comment line. | ||
# | ||
class Tune | ||
def initialize(tune) | ||
|
||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
|
||
require 'pry' | ||
require 'abc/file' | ||
require 'abc/file_header' | ||
require 'abc/tune' |
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,5 +1,31 @@ | ||
require 'spec_helper' | ||
|
||
# An abc file consists of one or more abc tune transcriptions, | ||
# optionally interspersed with free text and typeset text | ||
# annotations. It may optionally start with a file header to | ||
# set up default values for processing the file. | ||
# | ||
# The file header, abc tunes and text annotations are separated | ||
# from each other by empty lines (also known as blank lines). | ||
# | ||
# An abc file with more than one tune in it is called an abc | ||
# tunebook. | ||
# | ||
describe ABC::File do | ||
it "loads the file without error" | ||
let(:file) {ABC::File.new("spec/fixtures/english.abc")} | ||
it "should load without error" do | ||
expect {file}.not_to raise_error | ||
end | ||
it "should be parsed" do | ||
expect(file).to be_parsed | ||
end | ||
it "should have a format version of 2.1" do | ||
expect(file.version).to eq 2.1 | ||
end | ||
it "should have a file header" do | ||
expect(file.header) | ||
end | ||
it "should have 3 tunes" do | ||
expect(file.tunes.count).to eq 3 | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
require 'spec_helper' | ||
|
||
# An abc tune itself consists of a tune header and a tune body, | ||
# terminated by an empty line or the end of the file. It may | ||
# also contain comment lines or stylesheet directives. | ||
# | ||
# The tune header is composed of several information field lines, | ||
# which are further discussed in information fields. The tune | ||
# header should start with an X:(reference number) field followed | ||
# by a T:(title) field and finish with a K:(key) field. | ||
# | ||
# The tune body, which contains the music code, follows immediately | ||
# after. Certain fields may also be used inside the tune body - see | ||
# use of fields within the tune body. | ||
# | ||
# It is legal to write an abc tune without a tune body. This | ||
# feature can be used to document tunes without transcribing them. | ||
# | ||
# Abc music code lines are those lines in the tune body which give | ||
# notes, bar lines and other musical symbols - see the tune body | ||
# for details. In effect, music code is the contents of any line | ||
# which is not an information field, stylesheet directive or | ||
# comment line. | ||
# | ||
describe ABC::Tune do | ||
|
||
end |