class Rattler::Util::Node

A Node is a node that can be used as-is to compose tree structures or as a base class for user-defined node types. It is the base class used for all tree structures in the Rattler framework.

Public Class Methods

[](*args) click to toggle source

@overload Node.[]()

@return [Node] a +Node+ object with no children or attributes

@overload Node.[](child...)

@return [Node] a +Node+ object with children and no attributes

@overload Node.[](attribute...)

@return [Node] a +Node+ object with attributes and no children

@overload Node.[](child…, attribute…)

@return [Node] a +Node+ object with children and attributes
# File lib/rattler/util/node.rb, line 19
def self.[](*args)
  self.new(*args)
end
new(*args) click to toggle source

@overload initialize()

Create a +Node+ object with no children or attributes.

@overload initialize(child…)

Create a +Node+ object with children and no attributes.

@overload initialize(attribute…)

Create a +Node+ object with attributes and no children.

@overload initialize(child…, attribute…)

Create a +Node+ object with children and attributes.
# File lib/rattler/util/node.rb, line 31
def initialize(*args)
  @attrs = args.last.respond_to?(:to_hash) ? args.pop : {}
  @__children__ = args
end

Public Instance Methods

[](*args) click to toggle source

Access the node’s children as if the node were an array of its children.

@overload [](index)

@param [Integer] index index of the child
@return the child at +index+

@overload [](start, length)

@param [Integer] start the index of the first child
@param [Integer] length the number of children to return
@return [Array] the node's children starting at +start+ and
  continuing for +length+ children

@overload [](range)

@param [Range] range the range of children
@return [Array] the node's children specified by +range+
# File lib/rattler/util/node.rb, line 126
def [](*args)
  children[*args]
end
attrs() click to toggle source

Return a the node’s attributes.

@return [Hash] the node’s attributes

# File lib/rattler/util/node.rb, line 64
def attrs
  @attrs ||= {}
end
child(index = 0) click to toggle source

Return the node’s child at index, or the first/only child if no index is given.

@param [Integer] index @return the child at index, or the first/only child if no index is

given
# File lib/rattler/util/node.rb, line 57
def child(index = 0)
  children[index]
end
children() click to toggle source

Return an array of the node’s children

@return [Array] the node’s children

# File lib/rattler/util/node.rb, line 39
def children
  @children ||= if @__children__
    if @__children__.size == 1 && @__children__.first.respond_to?(:to_ary)
      @__children__.first
    else
      @__children__
    end
  else
    []
  end
end
each() { |child| ... } click to toggle source

Call block once for each child, passing that child as an argument.

@yield [child]

# File lib/rattler/util/node.rb, line 79
def each # :yield: child
  block_given? ? children.each { |_| yield _ } : children.each
end
empty?() click to toggle source

Return true if the node has no children.

@return [Boolean] true if the node has no children

# File lib/rattler/util/node.rb, line 109
def empty?
  children.empty?
end
eql?(other) click to toggle source

Return true if the node has the same value as other, i.e. other is an instance of the same class and has equal children and attributes.

@return [Boolean] true the node has the same value as other

# File lib/rattler/util/node.rb, line 145
def eql?(other)
  self.class == other.class and
  self.same_contents?(other)
end
method_missing(symbol, *args) click to toggle source

Allow attributes to be accessed as methods.

Calls superclass method
# File lib/rattler/util/node.rb, line 151
def method_missing(symbol, *args)
  (args.empty? and attrs.has_key?(symbol)) ? attrs[symbol] : super
end
name() click to toggle source

Return the node’s name, which is the node’s name attribute if it has one, otherwise the name of the node’s class.

@return the node’s name

# File lib/rattler/util/node.rb, line 72
def name
  attrs.fetch(:name, self.class.name)
end
to_graphviz() click to toggle source

@return a new GraphViz digraph object representing the node

# File lib/rattler/util/node.rb, line 200
def to_graphviz
  Rattler::Util::GraphViz.digraph(self)
end
with_attrs(new_attrs) click to toggle source

@param (see new_attrs!) @return [Node] a new node with the new attributes added to the node’s

attributes
# File lib/rattler/util/node.rb, line 95
def with_attrs(new_attrs)
  self.with_attrs!(attrs.merge new_attrs)
end
with_attrs!(new_attrs) click to toggle source

@param [Hash] new_attrs a new set of attributes @return [Node] a new node with the new attributes replacing the node’s

attributes
# File lib/rattler/util/node.rb, line 102
def with_attrs!(new_attrs)
  self.class.new(children, new_attrs)
end
with_child(new_children)
Alias for: with_children
with_children(new_children) click to toggle source

@param [Array] new_children a new array of children @return [Node] a new code with the new children replacing the node’s

children
# File lib/rattler/util/node.rb, line 86
def with_children(new_children)
  self.class.new(new_children, attrs)
end
Also aliased as: with_child

Private Instance Methods

pretty_keys() click to toggle source
# File lib/rattler/util/node.rb, line 211
def pretty_keys
  @pretty_keys ||= attrs.keys.reject {|_| _ == :name }.sort_by {|_| _.to_s }
end
pretty_print_name(q) click to toggle source
# File lib/rattler/util/node.rb, line 206
def pretty_print_name(q)
  q.text(self.class.name)
  q.text("<#{self.name.inspect}>") if attrs.has_key?(:name)
end