Class: MusicalScore::Score::Score

Inherits:
ElementBase show all
Includes:
Contracts
Defined in:
lib/musical_score/score/score.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(credits: nil, identification: nil, part_list:, parts:, file_path: nil) ⇒ Score

Returns a new instance of Score.



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# 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

Instance Attribute Details

#creditsObject

Returns the value of attribute credits.



15
16
17
# File 'lib/musical_score/score/score.rb', line 15

def credits
  @credits
end

#file_pathObject (readonly)

Returns the value of attribute file_path.



14
15
16
# File 'lib/musical_score/score/score.rb', line 14

def file_path
  @file_path
end

#identificationObject

Returns the value of attribute identification.



15
16
17
# File 'lib/musical_score/score/score.rb', line 15

def identification
  @identification
end

#part_listObject

Returns the value of attribute part_list.



15
16
17
# File 'lib/musical_score/score/score.rb', line 15

def part_list
  @part_list
end

#partsObject

Returns the value of attribute parts.



15
16
17
# File 'lib/musical_score/score/score.rb', line 15

def parts
  @parts
end

Class Method Details

.create_by_hash(doc, file_path) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# 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) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# 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

Instance Method Details

#export_xmlObject



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# 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_locationObject



150
151
152
153
154
# File 'lib/musical_score/score/score.rb', line 150

def set_location
    @parts.each do |part|
        part.set_location
    end
end