Module: Musa::MusicXML::Builder::Internal::Helper::ToXML Private

Included in:
Attributes, Backup, Clef, Forward, Musa::MusicXML::Builder::Internal::Harmonic, Key, Measure, Part, Time, ScorePartwise
Defined in:
lib/musa-dsl/musicxml/builder/helper.rb

Overview

This module is part of a private API. You should avoid using this module if possible, as it may be removed or be changed in the future.

Mixin for XML serialization capability.

Provides the public to_xml interface that handles IO and indentation setup, delegating to the private _to_xml method for actual XML generation.

Usage

Classes including this module must implement _to_xml(io, indent:, tabs:).

Examples:

Including in a class

class MyElement
  include Musa::MusicXML::Builder::Internal::Helper::ToXML

  private

  def _to_xml(io, indent:, tabs:)
    io.puts "#{tabs}<my-element />"
  end
end

element = MyElement.new
element.to_xml  # => StringIO with XML content

Instance Method Summary collapse

Instance Method Details

#to_xml(io = nil, indent: nil) ⇒ IO, StringIO

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.

Examples:

Writing to file

File.open('output.xml', 'w') do |f|
  element.to_xml(f)
end

Getting XML as string

xml_string = element.to_xml.string

Parameters:

  • io (IO, StringIO, nil) (defaults to: nil)

    output stream (creates StringIO if nil)

  • indent (Integer, nil) (defaults to: nil)

    indentation level (default: 0)

Returns:

  • (IO, StringIO)

    the io parameter, containing the XML output



160
161
162
163
164
165
166
167
168
169
# File 'lib/musa-dsl/musicxml/builder/helper.rb', line 160

def to_xml(io = nil, indent: nil)
  io ||= StringIO.new
  indent ||= 0

  tabs = "\t" * indent

  _to_xml(io, indent: indent, tabs: tabs)

  io
end