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

Returns a new instance of NodeArray.



117
118
119
120
121
# File 'lib/graph.rb', line 117

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

Instance Method Details

#push(n) ⇒ Object

Add the given node at the end of the list

Parameters:



134
135
136
137
138
139
140
141
142
# File 'lib/graph.rb', line 134

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) ⇒ Object

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'})


127
128
129
130
# File 'lib/graph.rb', line 127

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