rgraph 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
data/lib/rgraph/graph.rb CHANGED
@@ -9,22 +9,25 @@ class Graph
9
9
  def initialize(csv)
10
10
  @nodes = []
11
11
  @links = []
12
- unless not csv[-4..-1] == ".csv"
13
- CSV.foreach(csv, headers: true) do |row|
14
- unless source = @nodes.select{|n| n.id == row['source']}.first
15
- source = Node.new(id: row['source'])
16
- @nodes << source
17
- end
18
- unless target = @nodes.select{|n| n.id == row['target']}.first
19
- target = Node.new(id: row['target'])
20
- @nodes << target
21
- end
22
- @links << Link.new(source: source, target: target, weight: row['weight'], year: row['year'])
12
+ raise Exception.new("the file must be a .csv") unless File.extname(csv) == ".csv"
13
+
14
+ CSV.foreach(csv, headers: true) do |row|
15
+ #last because CSV#delete returns [column,value]
16
+ source_id = row.delete('source').last
17
+ target_id = row.delete('target').last
18
+
19
+ unless source = get_node_by_id(source_id)
20
+ source = Node.new(id: source_id)
21
+ @nodes << source
22
+ end
23
+ unless target = get_node_by_id(target_id)
24
+ target = Node.new(id: target_id)
25
+ @nodes << target
23
26
  end
24
- else
25
- raise Exception.new("the file must be a .csv")
27
+
28
+ @links << Link.new(source: source, target: target, weight: row['weight'], year: row['year'])
26
29
  end
27
- end
30
+ end
28
31
 
29
32
  def each_node(&block)
30
33
  @nodes.each(&block)
@@ -33,4 +36,10 @@ end
33
36
  def each_link(&block)
34
37
  @links.each(&block)
35
38
  end
39
+
40
+ private
41
+
42
+ def get_node_by_id(node_id)
43
+ @nodes.select{|n| n.id == node_id}.first
44
+ end
36
45
  end
data/lib/rgraph/link.rb CHANGED
@@ -1,28 +1,20 @@
1
1
  class Link
2
- #attr_accessor :source, :target
2
+ attr_accessor :source, :target
3
3
 
4
4
  def initialize(arg)
5
5
  @args = arg
6
- if @args[:source] == nil
7
- raise Exception.new("source cant be nil")
8
- end
6
+ @source = @args.delete(:source)
7
+ @target = @args.delete(:target)
9
8
 
10
- if @args[:target] == nil
11
- raise Exception.new("target cant be nil")
12
- end
13
-
14
- if not @args[:source].is_a? Node
15
- raise Exception.new("source must be of type Node")
16
- end
17
-
18
- if not @args[:target].is_a? Node
19
- raise Exception.new("target must be of type Node")
20
- end
9
+ raise Exception.new("source cant be nil") unless @source
10
+ raise Exception.new("target cant be nil") unless @target
11
+ raise Exception.new("source must be of type Node") unless @source.is_a? Node
12
+ raise Exception.new("target must be of type Node") unless @target.is_a? Node
21
13
 
22
14
  @args[:weight] ||= 1
23
15
 
24
- @args[:source].neighbours << @args[:target]
25
- @args[:target].neighbours << @args[:source]
16
+ @source.neighbours << @target
17
+ @target.neighbours << @source
26
18
  end
27
19
 
28
20
  def method_missing(name, *args)
@@ -1,3 +1,3 @@
1
1
  module Rgraph
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
3
3
  end
data/lib/rgraph.rb CHANGED
@@ -1,4 +1,7 @@
1
1
  require "rgraph/version"
2
+ require "rgraph/link"
3
+ require "rgraph/node"
4
+ require "rgraph/graph"
2
5
 
3
6
  module Rgraph
4
7
  # Your code goes here...
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rgraph
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -96,8 +96,7 @@ description: Ruby's Graph Library
96
96
  email:
97
97
  - aride.moulin@gmail.com
98
98
  - michel@michelboaventura.com
99
- executables:
100
- - guard
99
+ executables: []
101
100
  extensions: []
102
101
  extra_rdoc_files: []
103
102
  files:
@@ -109,7 +108,6 @@ files:
109
108
  - LICENSE
110
109
  - README.md
111
110
  - Rakefile
112
- - bin/guard
113
111
  - lib/rgraph.rb
114
112
  - lib/rgraph/graph.rb
115
113
  - lib/rgraph/link.rb
data/bin/guard DELETED
@@ -1,16 +0,0 @@
1
- #!/usr/bin/env ruby19
2
- #
3
- # This file was generated by Bundler.
4
- #
5
- # The application 'guard' is installed as part of a gem, and
6
- # this file is here to facilitate running it.
7
- #
8
-
9
- require 'pathname'
10
- ENV['BUNDLE_GEMFILE'] ||= File.expand_path("../../Gemfile",
11
- Pathname.new(__FILE__).realpath)
12
-
13
- require 'rubygems'
14
- require 'bundler/setup'
15
-
16
- load Gem.bin_path('guard', 'guard')