class Rip::Parser::Node

Attributes

extra[R]
location[R]
parent[R]
type[R]

Public Class Methods

new(location:, parent: nil, type:, **extra) click to toggle source
# File source/rip/parser/node.rb, line 8
def initialize(location:, parent: nil, type:, **extra)
  @location = location
  @parent = parent
  @type = type
  @extra = extra.inject({}) do |memo, (key, value)|
    memo.merge(key => self.class.try_convert(value, self))
  end
end

Private Class Methods

try_convert(value, parent) click to toggle source
# File source/rip/parser/node.rb, line 97
def self.try_convert(value, parent)
  case value
  when Array
    value.map do |v|
      try_convert(v, parent)
    end
  when Hash
    new(value.merge(parent: parent))
  when self
    new(value.to_h.merge(parent: parent))
  else
    value
  end
end
try_convert_to_h(value, include_location) click to toggle source
# File source/rip/parser/node.rb, line 112
def self.try_convert_to_h(value, include_location)
  case value
  when Array
    value.map do |v|
      try_convert_to_h(v, include_location)
    end
  when self
    value.to_h(include_location: include_location)
  else
    value
  end
end

Public Instance Methods

==(other) click to toggle source
# File source/rip/parser/node.rb, line 17
def ==(other)
  to_h == other.to_h
end
[](key) click to toggle source
# File source/rip/parser/node.rb, line 21
def [](key)
  case key.to_sym
    when :location then location
    when :type     then type
    else                extra[key.to_sym]
  end
end
key?(key) click to toggle source
# File source/rip/parser/node.rb, line 29
def key?(key)
  extra.key?(key.to_sym)
end
keys() click to toggle source
# File source/rip/parser/node.rb, line 33
def keys
  extra.keys
end
length() click to toggle source
# File source/rip/parser/node.rb, line 41
def length
  location.length
end
merge(other) click to toggle source
# File source/rip/parser/node.rb, line 45
def merge(other)
  self.class.new(extra.merge(other.to_h).merge(location: location, type: other[:type] || type))
end
to_h(include_location: true) click to toggle source
# File source/rip/parser/node.rb, line 49
def to_h(include_location: true)
  _extra = extra.map do |key, value|
    [ key, self.class.try_convert_to_h(value, include_location) ]
  end.to_h

  if include_location
    { location: location, type: type }
  else
    { type: type }
  end.merge(_extra)
end
traverse(&callback) click to toggle source
# File source/rip/parser/node.rb, line 61
def traverse(&callback)
  _extra = extra.map do |key, value|
    _value = case value
    when Array
      value.map do |v|
        v.traverse(&callback)
      end
    when self.class
      value.traverse(&callback)
    else
      value
    end

    [ key, _value ]
  end.to_h

  callback.call(merge(_extra.merge(parent: parent)))
end
values() click to toggle source
# File source/rip/parser/node.rb, line 37
def values
  extra.values
end

Private Instance Methods

method_missing(missing_method, *args, &block) click to toggle source
Calls superclass method
# File source/rip/parser/node.rb, line 82
def method_missing(missing_method, *args, &block)
  case
  when key?(missing_method)
    extra[missing_method]
  when missing_method.to_s.end_with?('?')
    missing_method.to_s.sub(/\?\z/, '').to_sym == type
  else
    super
  end
end
respond_to_missing?(name, include_all) click to toggle source
# File source/rip/parser/node.rb, line 93
def respond_to_missing?(name, include_all)
  key?(name) || name.to_s.end_with?('?')
end