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 28 29 30 |
# File 'lib/rgraph/graph.rb', line 9 def initialize(csv) @nodes = [] @links = [] raise Exception.new("the file must be a .csv") unless File.extname(csv) == ".csv" CSV.foreach(csv, headers: true) do |row| #last because CSV#delete returns [column,value] source_id = row.delete('source').last target_id = row.delete('target').last unless source = get_node_by_id(source_id) source = Node.new(id: source_id) @nodes << source end unless target = get_node_by_id(target_id) target = Node.new(id: target_id) @nodes << target end @links << Link.new(source: source, target: target, weight: row['weight'], year: row['year']) 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
36 37 38 |
# File 'lib/rgraph/graph.rb', line 36 def each_link(&block) @links.each(&block) end |
#each_node(&block) ⇒ Object
32 33 34 |
# File 'lib/rgraph/graph.rb', line 32 def each_node(&block) @nodes.each(&block) end |