Class: Graph::NodeArray

Inherits:
Array
  • Object
show all
Defined in:
lib/graph.rb

Overview

An array of Node objects

Instance Method Summary collapse

Constructor Details

#initialize(li) ⇒ NodeArray

Create a new NodeArray from an existing Array.

Parameters:

  • li (Array)


132
133
134
135
136
# File 'lib/graph.rb', line 132

def initialize(li)
    nodes = li.map { |n| n.is_a?(Node) ? n : Node.new(n) }
    super(nodes)
    @defaults = {}
end

Instance Method Details

#push(n) ⇒ NodeArray

Add the given node at the end of the list

Parameters:

Returns:



152
153
154
155
156
157
158
159
160
# File 'lib/graph.rb', line 152

def push(n)
    if (!n.is_a?(Hash) && !n.is_a?(Node))
        raise TypeError.new "#{n.inspect} is not an Hash nor a Node!"
    end

    n = Node.new(n) if (n.is_a?(Hash))

    super(n.clone.update(@defaults))
end

#set_default(dict) ⇒ NodeArray

Note:

This method can be called multiple times.

Set some default values for current elements.

Examples:

Set all nodes’s ‘created-at’ value to ‘2012-05-03’

myNodeList.set_default({'created-at'=>'2012-05-03'})

Parameters:

  • dict (Hash)

Returns:



144
145
146
147
# File 'lib/graph.rb', line 144

def set_default(dict)
    @defaults.update(dict)
    self.map! { |e| e.update(@defaults) }
end