Class: Musa::MusicXML::Builder::Internal::Attributes Private
- Extended by:
- Extension::AttributeBuilder
- Includes:
- Extension::With, Helper::ToXML
- Defined in:
- lib/musa-dsl/musicxml/builder/attributes.rb
Overview
This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.
Musical attributes container.
Attributes represents the <attributes> element in MusicXML, which
contains key signatures, time signatures, clefs, and timing divisions.
This element typically appears at the beginning of a measure to establish
or change the musical context.
Timing Resolution (Divisions)
The divisions parameter sets the timing resolution for note durations in the measure. It represents how many divisions per quarter note:
- divisions=1: Quarter note = 1 unit (limited precision)
- divisions=2: Eighth notes possible
- divisions=4: Sixteenth notes possible
- divisions=8: Thirty-second notes possible
- divisions=24: Common choice (supports triplets and quintuplets)
Note durations are expressed as multiples of this division unit.
Single-Staff vs Multi-Staff
For single-staff instruments (violin, flute), one key/time/clef suffices. For multi-staff instruments (piano, organ, harp), different attributes can be specified per staff using the staff number parameter.
The <staves> element is automatically generated based on the maximum
number of keys, times, or clefs defined.
Usage Styles
Two equivalent approaches:
Constructor parameters (convenient for simple cases):
Attributes.new(
divisions: 4,
key_fifths: 0,
time_beats: 4, time_beat_type: 4,
clef_sign: 'G', clef_line: 2
)
DSL with explicit elements (flexible for multi-staff):
Attributes.new do
divisions 4
key fifths: 0
time beats: 4, beat_type: 4
clef sign: 'G', line: 2
end
Class Method Summary collapse
-
.attr_complex_adder_to_array(name, klass, plural: nil, variable: nil) ⇒ Object
extended
from Extension::AttributeBuilder
Creates methods for adding complex objects (with multiple parameters) to an array.
-
.attr_complex_adder_to_custom(name, plural: nil, variable: nil) { ... } ⇒ Object
extended
from Extension::AttributeBuilder
Creates methods for adding complex objects with custom construction logic.
-
.attr_complex_builder(name, klass, variable: nil, first_parameter: nil) ⇒ Object
extended
from Extension::AttributeBuilder
Creates a getter/setter DSL method for complex objects with multiple parameters.
-
.attr_simple_builder(name, klass = nil, variable: nil) ⇒ Object
extended
from Extension::AttributeBuilder
Creates a simple getter/setter DSL method for a single value.
-
.attr_tuple_adder_to_array(name, klass, plural: nil, variable: nil) ⇒ Object
extended
from Extension::AttributeBuilder
Creates methods for adding id/value tuples to an array collection.
-
.attr_tuple_adder_to_hash(name, klass, plural: nil, variable: nil) ⇒ Object
extended
from Extension::AttributeBuilder
Creates methods for adding id/value tuples to a hash collection.
-
.attr_tuple_builder(name, klass, variable: nil) ⇒ Object
extended
from Extension::AttributeBuilder
Creates a getter/setter DSL method for a single id/value tuple.
Instance Method Summary collapse
-
#_to_xml(io, indent:, tabs:) ⇒ void
private
Generates the attributes XML element.
-
#clef { ... } ⇒ Clef
private
Adds a clef.
-
#divisions ⇒ Object
private
Timing divisions builder/setter.
-
#initialize(divisions: nil, key_cancel: nil, key_fifths: nil, key_mode: nil, time_senza_misura: nil, time_beats: nil, time_beat_type: nil, clef_sign: nil, clef_line: nil, clef_octave_change: nil) { ... } ⇒ Attributes
constructor
private
Creates a musical attributes container.
-
#key { ... } ⇒ Key
private
Adds a key signature.
-
#time { ... } ⇒ Time
private
Adds a time signature.
-
#to_xml(io = nil, indent: nil) ⇒ IO, StringIO
included
from Helper::ToXML
private
Converts the object to MusicXML format.
-
#with(*value_parameters, keep_block_context: nil, **key_parameters, &block) ⇒ Object
included
from Extension::With
Executes a block with flexible context and parameter handling.
Constructor Details
#initialize(divisions: nil, key_cancel: nil, key_fifths: nil, key_mode: nil, time_senza_misura: nil, time_beats: nil, time_beat_type: nil, clef_sign: nil, clef_line: nil, clef_octave_change: nil) { ... } ⇒ Attributes
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Creates a musical attributes container.
506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 |
# File 'lib/musa-dsl/musicxml/builder/attributes.rb', line 506 def initialize(divisions: nil, key_cancel: nil, key_fifths: nil, key_mode: nil, time_senza_misura: nil, time_beats: nil, time_beat_type: nil, clef_sign: nil, clef_line: nil, clef_octave_change: nil, &block) @divisions = divisions @keys = [] @times = [] @clefs = [] add_key cancel: key_cancel, fifths: key_fifths, mode: key_mode if key_fifths add_time senza_misura: time_senza_misura, beats: time_beats, beat_type: time_beat_type if time_senza_misura || (time_beats && time_beat_type) add_clef sign: clef_sign, line: clef_line, octave_change: clef_octave_change if clef_sign with &block if block_given? end |
Class Method Details
.attr_complex_adder_to_array(name, klass, plural: nil, variable: nil) ⇒ Object Originally defined in module Extension::AttributeBuilder
Creates methods for adding complex objects (with multiple parameters) to an array.
Supports both positional and keyword arguments when creating instances.
.attr_complex_adder_to_custom(name, plural: nil, variable: nil) { ... } ⇒ Object Originally defined in module Extension::AttributeBuilder
Creates methods for adding complex objects with custom construction logic.
The block receives parameters and should construct and add the object.
.attr_complex_builder(name, klass, variable: nil, first_parameter: nil) ⇒ Object Originally defined in module Extension::AttributeBuilder
Creates a getter/setter DSL method for complex objects with multiple parameters.
Supports optional first_parameter that's automatically prepended when constructing.
.attr_simple_builder(name, klass = nil, variable: nil) ⇒ Object Originally defined in module Extension::AttributeBuilder
Creates a simple getter/setter DSL method for a single value.
.attr_tuple_adder_to_array(name, klass, plural: nil, variable: nil) ⇒ Object Originally defined in module Extension::AttributeBuilder
Creates methods for adding id/value tuples to an array collection.
Similar to attr_tuple_adder_to_hash but stores items in an array instead of hash. Useful when order matters or duplicates are allowed.
.attr_tuple_adder_to_hash(name, klass, plural: nil, variable: nil) ⇒ Object Originally defined in module Extension::AttributeBuilder
Creates methods for adding id/value tuples to a hash collection.
Generates:
add_#{name}(id, parameter)→ creates instance and adds to hash#{plural}(**parameters)→ batch add or retrieve hash
.attr_tuple_builder(name, klass, variable: nil) ⇒ Object Originally defined in module Extension::AttributeBuilder
Creates a getter/setter DSL method for a single id/value tuple.
Instance Method Details
#_to_xml(io, indent:, tabs:) ⇒ void
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
This method returns an undefined value.
Generates the attributes XML element.
Automatically determines the number of staves based on the maximum count of keys, times, or clefs. Outputs divisions, keys, times, staves count (if > 1), and clefs.
609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 |
# File 'lib/musa-dsl/musicxml/builder/attributes.rb', line 609 def _to_xml(io, indent:, tabs:) io.puts "#{tabs}<attributes>" io.puts "#{tabs}\t<divisions>#{@divisions.to_i}</divisions>" if @divisions @keys.each do |key| key.to_xml(io, indent: indent + 1) end @times.each do |time| time.to_xml(io, indent: indent + 1) end staves = [@keys.size, @times.size, @clefs.size].max io.puts "#{tabs}\t<staves>#{staves}</staves>" if staves > 1 @clefs.each do |clef| clef.to_xml(io, indent: indent + 1) end io.puts "#{tabs}</attributes>" end |
#clef { ... } ⇒ Clef
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Adds a clef.
Multiple clefs are needed for multi-staff parts (piano, organ, harp).
595 |
# File 'lib/musa-dsl/musicxml/builder/attributes.rb', line 595 attr_complex_adder_to_array :clef, Clef |
#divisions(value) ⇒ Object #divisions=(value) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Timing divisions builder/setter.
Sets or updates the divisions per quarter note. Higher values provide finer timing resolution for complex rhythms.
539 |
# File 'lib/musa-dsl/musicxml/builder/attributes.rb', line 539 attr_simple_builder :divisions |
#key { ... } ⇒ Key
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Adds a key signature.
Multiple keys can be added for multi-staff parts where each staff has a different key signature.
558 |
# File 'lib/musa-dsl/musicxml/builder/attributes.rb', line 558 attr_complex_adder_to_array :key, Key |
#time { ... } ⇒ Time
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Adds a time signature.
Multiple time signatures can be added for multi-staff parts where each staff has a different meter (polyrhythm).
577 |
# File 'lib/musa-dsl/musicxml/builder/attributes.rb', line 577 attr_complex_adder_to_array :time, Time |
#to_xml(io = nil, indent: nil) ⇒ IO, StringIO Originally defined in module Helper::ToXML
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Converts the object to MusicXML format.
This method sets up the IO stream and indentation, then delegates to
the private _to_xml method for actual XML generation.
#with(*value_parameters, keep_block_context: nil, **key_parameters, &block) ⇒ Object Originally defined in module Extension::With
The _ parameter is special: when present, it signals "keep caller's context"
and receives self (the object) as its value.
Uses SmartProcBinder internally to handle parameter matching.
Executes a block with flexible context and parameter handling.