class MusicalScore::Score::Score

Attributes

credits[RW]
file_path[R]
identification[RW]
part_list[RW]
parts[RW]

Public Class Methods

create_by_hash(doc, file_path) click to toggle source
# File lib/musical_score/score/score.rb, line 76
def self.create_by_hash(doc, file_path)
    args = {}
    args[:file_path] = file_path

    if doc.has_key?("identification")
        identification = MusicalScore::Score::Identification::Identification.create_by_hash(doc["identification"][0])
        args[:identification] = identification
    end

    credits = Array.new
    doc["credit"].each do |element|
        if (element.has_key?("credit-words"))
            credits.push(element.dig("credit-words", 0, "content"))
        end
    end
    args[:credits] = credits

    part_list = Array.new
    doc.dig("part-list", 0, "score-part").each do |element|
        part_name         = element["part-name"][0]
        part_abbreviation = element["part-abbreviation"][0]
        part = MusicalScore::Score::Part::Part.new(part_name, part_abbreviation)
        part_list.push(part)
    end
    args[:part_list] = part_list

    parts = Array.new
    doc["part"].each do |element|
        part = MusicalScore::Part::Part.create_by_hash(element)
        parts.push(part)
    end
    args[:parts] = parts
    return MusicalScore::Score::Score.new(args)
end
create_by_xml(xml_doc, file_path) click to toggle source
# File lib/musical_score/score/score.rb, line 40
def self.create_by_xml(xml_doc, file_path)
    partwise = xml_doc.elements["//score-partwise"]

    args = {}
    args[:file_path] = file_path
    identification_doc = partwise.elements["//identification"]
    if (identification_doc)
        identification = MusicalScore::Score::Identification::Identification.create_by_xml(identification_doc)
        args[:identification] = identification
    end
    credits = Array.new
    partwise.elements.each("//credit/credit-words") do |element|
        credits.push(element.text)
    end
    args[:credits] = credits

    part_list = Array.new
    partwise.elements.each("//part-list/score-part") do |element|
        part_name         = element.elements["part-name"].text
        part_abbreviation = element.elements["part-abbreviation"].text
        part = MusicalScore::Score::Part::Part.new(part_name, part_abbreviation)
        part_list.push(part)
    end
    args[:part_list] = part_list

    parts = Array.new
    partwise.elements.each("//part") do |element|
        part = MusicalScore::Part::Part.create_by_xml(element)
        parts.push(part)
    end
    args[:parts] = parts

    return MusicalScore::Score::Score.new(args)
end
new( credits: nil, identification: nil, part_list:, parts:, file_path: nil ) click to toggle source
# File lib/musical_score/score/score.rb, line 23
def initialize(
    credits: nil,
    identification: nil,
    part_list:,
    parts:,
    file_path: nil
    )
    @credits        = credits
    @identification = identification
    @part_list      = part_list
    @parts          = parts
    @file_path      = file_path

    set_location
end

Public Instance Methods

export_xml() click to toggle source
# File lib/musical_score/score/score.rb, line 111
            def export_xml()
                doc = REXML::Document.new
                doc << REXML::XMLDecl.new('1.0', 'UTF-8')
                doc << REXML::Document.new(<<-EOS).doctype
                <!DOCTYPE score-partwise PUBLIC "-//Recordare//DTD MusicXML 3.0 Partwise//EN" "http://www.musicxml.org/dtds/partwise.dtd">
                EOS

                score_partwise = REXML::Element.new('score-partwise')
                if (@identification)
                    score_partwise.add_element(@identification.export_xml)
                end

                if (@credits)
                    @credits.each_with_index do |credit, index|
                        credit_element = REXML::Element.new('credit')
                        credit_element.add_attribute('page', index + 1)

                        credit_word = REXML::Element.new('credit-words')
                        credit_word.add_text(credit)

                        credit_element.add_element(credit_word)
                        score_partwise.add_element(credit_element)
                    end
                end
                part_list = REXML::Element.new('part-list')
                @part_list.each_with_index do |part, index|
                    part_list.add_element(part.export_xml(index + 1))
                end
                score_partwise.add_element(part_list)
                #
                @parts.each_with_index do |part, index|
                    score_partwise.add_element(part.export_xml(index + 1))
                end

                doc.add_element(score_partwise)

                return doc
            end
set_location() click to toggle source
# File lib/musical_score/score/score.rb, line 150
def set_location
    @parts.each do |part|
        part.set_location
    end
end