Method: Graph#initialize
- Defined in:
- lib/rgraph/graph.rb
#initialize(csv) ⇒ Graph
Returns a new instance of Graph.
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/rgraph/graph.rb', line 15 def initialize(csv) @nodes = {} @links = [] @distance = nil @infinity = 100_000 csv = CSV.new(File.open(csv), headers: true) if csv.is_a?(String) csv.each do |row| #last because CSV#delete returns [column,value] source_id = row.delete('source').last target_id = row.delete('target').last type = row.delete('type').last || 'undirected' weight = row.delete('weight').last || 1 source = get_node_by_id(source_id) || Node.new(id: source_id) target = get_node_by_id(target_id) || Node.new(id: target_id) maybe_add_to_nodes(source, target) @links << Link.new(source: source, target: target, weight: weight, type: type) end end |