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.



101
102
103
104
105
# File 'lib/graph.rb', line 101

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:



118
119
120
121
122
123
124
# File 'lib/graph.rb', line 118

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

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


111
112
113
114
# File 'lib/graph.rb', line 111

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