Class: Graph
- Inherits:
-
Object
- Object
- Graph
- Defined in:
- lib/rgraph/graph.rb
Instance Attribute Summary collapse
-
#links ⇒ Object
Returns the value of attribute links.
-
#nodes ⇒ Object
Returns the value of attribute nodes.
Instance Method Summary collapse
- #each_link(&block) ⇒ Object
- #each_node(&block) ⇒ Object
-
#initialize(csv) ⇒ Graph
constructor
A new instance of Graph.
Constructor Details
#initialize(csv) ⇒ Graph
Returns a new instance of Graph.
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/rgraph/graph.rb', line 9 def initialize(csv) @nodes = [] @links = [] unless not csv[-4..-1] == ".csv" CSV.foreach(csv, headers: true) do |row| unless source = @nodes.select{|n| n.id == row['source']}.first source = Node.new(id: row['source']) @nodes << source end unless target = @nodes.select{|n| n.id == row['target']}.first target = Node.new(id: row['target']) @nodes << target end @links << Link.new(source: source, target: target, weight: row['weight'], year: row['year']) end else raise Exception.new("the file must be a .csv") end end |
Instance Attribute Details
#links ⇒ Object
Returns the value of attribute links.
7 8 9 |
# File 'lib/rgraph/graph.rb', line 7 def links @links end |
#nodes ⇒ Object
Returns the value of attribute nodes.
7 8 9 |
# File 'lib/rgraph/graph.rb', line 7 def nodes @nodes end |
Instance Method Details
#each_link(&block) ⇒ Object
33 34 35 |
# File 'lib/rgraph/graph.rb', line 33 def each_link(&block) @links.each(&block) end |
#each_node(&block) ⇒ Object
29 30 31 |
# File 'lib/rgraph/graph.rb', line 29 def each_node(&block) @nodes.each(&block) end |