Class: Graph::EdgeArray

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

Overview

An array of Edge objects

Instance Method Summary collapse

Constructor Details

#initialize(li) ⇒ EdgeArray

Create a new EdgeArray from an existing Array.

Parameters:

  • li (Array<Edge, Hash>)


169
170
171
172
173
# File 'lib/graph.rb', line 169

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

Instance Method Details

#push(e) ⇒ EdgeArray

Add the given edge at the end of the list

Parameters:

Returns:



188
189
190
191
192
193
194
195
196
# File 'lib/graph.rb', line 188

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

    e = Edge.new(e) if (e.is_a?(Hash))

    super(e.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 edges’s ‘created-at’ value to ‘2012-05-03’

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

Parameters:

  • dict (Hash)


180
181
182
183
# File 'lib/graph.rb', line 180

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