class MusicXML::Node::Parser

Parses a node to get out the child nodes and attributes

Attributes

config[R]
node[R]
parsed[R]

Public Class Methods

new(args = {}) click to toggle source

Store the given options and build a hash of the parsed nodes

# File lib/musicxml/node/parser.rb, line 9
def initialize(args = {})
  @config = args[:config]
  @node   = args[:node]
  @parsed = {}
end

Public Instance Methods

parse() click to toggle source

Loop through each of the configured parsing options and store them

# File lib/musicxml/node/parser.rb, line 16
def parse
  %w[plural_attrs plural_nodes singular_attrs singular_nodes properties].each do |method_name|
    config.send(method_name).each(&method(method_name))
  end
  parsed
end

Private Instance Methods

find_class(name) click to toggle source
# File lib/musicxml/node/parser.rb, line 25
def find_class(name)
  ::MusicXML::Node.registry[name]
end
plural_attrs(name) click to toggle source
# File lib/musicxml/node/parser.rb, line 29
def plural_attrs(name)
  attr_nodes = node.search(symbol_to_node(name))
  set(name, attr_nodes.any? ? attr_nodes.map(&:content) : [])
end
plural_nodes(name) click to toggle source
# File lib/musicxml/node/parser.rb, line 34
def plural_nodes(name)
  clazz = find_class(name)
  node_list = node.search(symbol_to_node(name)).map do |child_node|
    clazz.new(child_node)
  end
  set(name, node_list.any? ? node_list : [])
end
properties(name) click to toggle source
# File lib/musicxml/node/parser.rb, line 42
def properties(name)
  attr = node.attributes[symbol_to_node(name)]
  set(name, attr.nil? ? '' : attr.value)
end
set(key, value) click to toggle source
# File lib/musicxml/node/parser.rb, line 47
def set(key, value)
  parsed[:"@#{key}"] = value
end
singular_attrs(name) click to toggle source
# File lib/musicxml/node/parser.rb, line 51
def singular_attrs(name)
  attr_node = node.at(symbol_to_node(name))
  set(name, attr_node.content) unless attr_node.nil?
end
singular_nodes(name) click to toggle source
# File lib/musicxml/node/parser.rb, line 56
def singular_nodes(name)
  found_node = node.at(symbol_to_node(name))
  set(name, find_class(name).new(found_node)) if found_node
end
symbol_to_node(name) click to toggle source
# File lib/musicxml/node/parser.rb, line 61
def symbol_to_node(name)
  name.to_s.tr('_', '-')
end