Class: Rattler::Util::Node

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/rattler/util/node.rb

Overview

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

Author:

  • Jason Arhart

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeNode #initialize(child...) ⇒ Node #initialize(attribute...) ⇒ Node #initialize(child..., attribute...) ⇒ Node

Create a Node object.

Overloads:

  • #initializeNode
  • #initialize(child...) ⇒ Node
  • #initialize(attribute...) ⇒ Node
  • #initialize(child..., attribute...) ⇒ Node


46
47
48
49
# File 'lib/rattler/util/node.rb', line 46

def initialize(*args)
  @attrs = args.last.respond_to?(:to_hash) ? args.pop : {}
  @__children__ = args
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(symbol, *args) ⇒ Object

Allow attributes to be accessed as methods.



161
162
163
# File 'lib/rattler/util/node.rb', line 161

def method_missing(symbol, *args)
  (args.empty? and attrs.has_key?(symbol)) ? attrs[symbol] : super
end

Class Method Details

.Node.[]Node .Node.[](child...) ⇒ Node .Node.[](attribute...) ⇒ Node .Node.[](child..., attribute...) ⇒ Node

Create a Node object.

Overloads:

  • .Node.[]Node

    Returns:

  • .Node.[](child...) ⇒ Node

    Returns:

  • .Node.[](attribute...) ⇒ Node

    Returns:

  • .Node.[](child..., attribute...) ⇒ Node

    Returns:

Returns:



32
33
34
# File 'lib/rattler/util/node.rb', line 32

def self.[](*args)
  self.new(*args)
end

Instance Method Details

#==(other) ⇒ Boolean

Return true if the node is equal to other. Normally this means other is an instance of the same class or a subclass and has equal children and attributes.

Returns:

  • (Boolean)

    true if the node is equal to other



145
146
147
148
149
# File 'lib/rattler/util/node.rb', line 145

def ==(other) #:nodoc:
  self.class === other and
  other.can_equal?(self) and
  self.same_contents?(other)
end

#[](index) ⇒ Object #[](start, length) ⇒ Array #[](range) ⇒ Array

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

Overloads:

  • #[](index) ⇒ Object

    Return the node’s child at index.

    Parameters:

    • index (Integer)

      index of the child

    Returns:

    • the child at index

  • #[](start, length) ⇒ Array

    Return an array of the node’s children starting at start and continuing for length children.

    Parameters:

    • start (Integer)

      the index of the first child

    • length (Integer)

      the number of children to return

    Returns:

    • (Array)

      the node’s children starting at start and continuing for length children

  • #[](range) ⇒ Array

    Return an array of the node’s children specified by range.

    Parameters:

    • range (Range)

      the range of children

    Returns:

    • (Array)

      the node’s children specified by range



136
137
138
# File 'lib/rattler/util/node.rb', line 136

def [](*args)
  children[*args]
end

#attrsHash

Return a the node’s attributes.

Returns:

  • (Hash)

    the node’s attributes



79
80
81
# File 'lib/rattler/util/node.rb', line 79

def attrs
  @attrs ||= {}
end

#can_equal?(other) ⇒ Boolean

Returns:

  • (Boolean)


171
172
173
# File 'lib/rattler/util/node.rb', line 171

def can_equal?(other) #:nodoc:
  self.class == other.class
end

#child(index = 0) ⇒ Object

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

Parameters:

  • index (Integer) (defaults to: 0)

Returns:

  • the child at index, or the first/only child if no index is given



72
73
74
# File 'lib/rattler/util/node.rb', line 72

def child(index = 0)
  children[index]
end

#childrenArray

Return an array of the node’s children

Returns:

  • (Array)

    the node’s children



54
55
56
57
58
59
60
61
62
63
64
# File 'lib/rattler/util/node.rb', line 54

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| ... } ⇒ Object

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

Yields:



94
95
96
# File 'lib/rattler/util/node.rb', line 94

def each # :yield: child
  block_given? ? children.each { |_| yield _ } : children.each
end

#empty?Boolean

Return true if the node has no children.

Returns:

  • (Boolean)

    true if the node has no children



115
116
117
# File 'lib/rattler/util/node.rb', line 115

def empty?
  children.empty?
end

#eql?(other) ⇒ Boolean

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.

Returns:

  • (Boolean)

    true the node has the same value as other



155
156
157
158
# File 'lib/rattler/util/node.rb', line 155

def eql?(other)
  self.class == other.class and
  self.same_contents?(other)
end

#inspectObject



182
183
184
185
186
187
# File 'lib/rattler/util/node.rb', line 182

def inspect #:nodoc:
  "#{self.class}[" +
  (children.map {|_| _.inspect } +
    attrs.map {|k, v| k.inspect + '=>' + v.inspect}).join(',') +
  ']'
end

#nameObject

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

Returns:

  • the node’s name



87
88
89
# File 'lib/rattler/util/node.rb', line 87

def name
  attrs.fetch(:name, self.class.name)
end

#respond_to?(symbol) ⇒ Boolean

Returns:

  • (Boolean)


166
167
168
# File 'lib/rattler/util/node.rb', line 166

def respond_to?(symbol) #:nodoc:
  super || @attrs.has_key?(symbol)
end

#same_contents?(other) ⇒ Boolean

Returns:

  • (Boolean)


176
177
178
179
# File 'lib/rattler/util/node.rb', line 176

def same_contents?(other) #:nodoc:
  self.children == other.children and
  self.attrs == other.attrs
end

#to_graphvizObject



189
190
191
# File 'lib/rattler/util/node.rb', line 189

def to_graphviz
  Rattler::Util::GraphViz.digraph(self)
end

#with_attrs(new_attrs) ⇒ Object



104
105
106
# File 'lib/rattler/util/node.rb', line 104

def with_attrs(new_attrs)
  self.with_attrs!(attrs.merge new_attrs)
end

#with_attrs!(new_attrs) ⇒ Object



108
109
110
# File 'lib/rattler/util/node.rb', line 108

def with_attrs!(new_attrs)
  self.class.new(children, new_attrs)
end

#with_children(new_children) ⇒ Object Also known as: with_child



98
99
100
# File 'lib/rattler/util/node.rb', line 98

def with_children(new_children)
  self.class.new(new_children, attrs)
end