Created
October 24, 2011 16:55
-
-
Save codabrink/1309503 to your computer and use it in GitHub Desktop.
NMLParse.rb
This file contains hidden or 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
require "rexml/document" | |
require "NMLParse/song" | |
class NMLParse | |
attr_accessor :songs | |
def initialize | |
@songs = [] | |
end | |
def self.read(path) | |
nml = self.new | |
file = File.new(path) | |
doc = REXML::Document.new file | |
doc.elemens.each("entry") { |entry| | |
next if !entry.attributes["title"] && !entry.attributes["artist"] #skips blank lines | |
nml.add_song do |song| | |
song.title = entry.attributes["title"] | |
song.artist = entry.attributes["artist"] | |
end | |
} | |
nml | |
end | |
def add_song(&block) | |
new_song = NMLParse::Song.new | |
yield(new_song) | |
@songs << new_song | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment