cecelia 0.0.3 → 0.0.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/Gemfile.lock CHANGED
@@ -1,11 +1,25 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- cecelia (0.0.1)
4
+ cecelia (0.0.4)
5
+ rspec
6
+ sequel
7
+ sqlite3
5
8
 
6
9
  GEM
7
10
  remote: http://rubygems.org/
8
11
  specs:
12
+ diff-lcs (1.1.3)
13
+ rspec (2.11.0)
14
+ rspec-core (~> 2.11.0)
15
+ rspec-expectations (~> 2.11.0)
16
+ rspec-mocks (~> 2.11.0)
17
+ rspec-core (2.11.1)
18
+ rspec-expectations (2.11.3)
19
+ diff-lcs (~> 1.1.3)
20
+ rspec-mocks (2.11.2)
21
+ sequel (3.38.0)
22
+ sqlite3 (1.3.6)
9
23
 
10
24
  PLATFORMS
11
25
  ruby
data/lib/cecelia/graph.rb CHANGED
@@ -1,20 +1,30 @@
1
1
  require 'sequel'
2
+ require 'yaml'
2
3
 
3
4
  class Graph
4
- def initialize(db_name)
5
- @db = Sequel.sqlite(db_name)
5
+ def initialize(db_name = "sqlite:/")
6
+ @db = Sequel.connect(db_name)
6
7
  require 'cecelia/graph_model.rb'
7
8
  end
8
9
 
9
- def add_vertex (attributes="")
10
- Vertices.create(:attributes => attributes)
10
+ def add_vertex (label = "", attributes = {})
11
+ if Vertices.find(:label => label) == nil
12
+ Vertices.create(:label=> label, :attributes => YAML.dump(attributes))
13
+ end
11
14
  end
12
15
 
13
- def add_edge(source,target,attributes="")
14
- unless Vertices.find(:id => source) == nil && Vertices.find(:id => target) == nil
15
- if ((Edges.filter(:source => source).filter(:target => target)).all).empty?
16
- Edges.create(:source => source, :target => target, :attributes => attributes)
16
+ def add_edge(source, target, attributes = {})
17
+ if source.class == String && target.class == String
18
+ begin
19
+ source_id = Vertices.find(:label => source)[:id]
20
+ target_id = Vertices.find(:label => target)[:id]
21
+ rescue => exc
22
+ exc
23
+ ensure
24
+ add_edge_id(source_id, target_id, attributes = {})
17
25
  end
26
+ else
27
+ add_edge_id(source, target, attributes = {})
18
28
  end
19
29
  end
20
30
 
@@ -27,6 +37,51 @@ class Graph
27
37
  end
28
38
 
29
39
  def neighbors(id)
30
- Edges.filter(:source => id).all
40
+ dataset = nil
41
+ if id.class == Integer
42
+ dataset = Edges.filter(:source => id).all
43
+ elsif id.class == String
44
+ label = Vertices.find(:label => id)[:id]
45
+ dataset = Edges.filter(:source => label).all
46
+ end
47
+ dataset
48
+ end
49
+
50
+ def find_vertex(args = {})
51
+ Vertices.find(args)
52
+ end
53
+
54
+ def find_edge(args = {})
55
+ Edges.find(args)
56
+ end
57
+
58
+ def filter_vertex(args = {})
59
+ Vertices.filter(args)
60
+ end
61
+
62
+ def filter_edge(args = {})
63
+ Edges.filter(args)
64
+ end
65
+
66
+ def remove_vertex(label)
67
+ if label.class == String
68
+ Vertices.find(:label => label).delete
69
+ end
70
+ end
71
+
72
+ def remove_edge(id)
73
+ id = id.to_i
74
+ Edges.find(:id => id).delete
75
+ end
76
+
77
+ private
78
+ def add_edge_id(source, target, attributes = {})
79
+ unless Vertices.find(:id => source) == nil && Vertices.find(:id => target) == nil
80
+ if ((Edges.filter(:source => source).filter(:target => target)).all).empty?
81
+ Edges.create(:source => source, :target => target, :attributes => YAML.dump(attributes))
82
+ end
83
+ end
31
84
  end
32
85
  end
86
+
87
+
@@ -6,6 +6,7 @@ class Vertices < Sequel::Model
6
6
  unless table_exists?
7
7
  set_schema do
8
8
  primary_key :id, :autoincrement => true
9
+ string :label
9
10
  string :attributes
10
11
  end
11
12
  create_table
@@ -1,3 +1,3 @@
1
1
  module Cecelia
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.5"
3
3
  end
@@ -0,0 +1,126 @@
1
+ #encoding: utf-8
2
+ require 'rspec'
3
+ require './lib/cecelia/graph.rb'
4
+
5
+
6
+ describe Graph,"Iniaialize method" do
7
+ it "Graph.new()に空引数" do
8
+ Graph.new().should be_true
9
+ end
10
+
11
+ it "Graph.new()にSQLiteを指定" do
12
+ Graph.new("sqlite://hoge.db").should be_true
13
+ end
14
+
15
+ after(:all) do
16
+ #File.delete("./hoge.db")
17
+ end
18
+ end
19
+
20
+ describe Graph, "ノード追加に関するテスト" do
21
+ let(:g){Graph.new}
22
+
23
+ it "空グラフのノード数" do
24
+ g.vertices.size.should == 0
25
+ end
26
+
27
+ it "ノード一つ追加" do
28
+ g.add_vertex("0").should be_true
29
+ end
30
+
31
+ it "グラフのノード数確認" do
32
+ g.vertices.size.should == 1
33
+ end
34
+
35
+ it "グラフに複数ノード追加" do
36
+ 10.times{|i| g.add_vertex(i.to_s)}.should be_true
37
+ end
38
+
39
+ it "グラフの複数ノード数確認" do
40
+ g.vertices.size.should == 10
41
+ end
42
+
43
+ it "同名ノードの追加" do
44
+ 10.times{|i|
45
+ g.add_vertex(i.to_s)
46
+ }.should be_true
47
+ end
48
+
49
+ it "同名ノードの禁止" do
50
+ g.vertices.size.should == 10
51
+ end
52
+ end
53
+
54
+ describe Graph,"エッジの追加に関するテスト" do
55
+ let(:g){Graph.new}
56
+
57
+ #before(:all){10.times{|i| g.add_vertex(i.to_s)}}
58
+
59
+ it "空グラフのエッジ数" do
60
+ g.edges.size.should == 0
61
+ end
62
+
63
+ it "エッジを1本作成" do
64
+ g.add_vertex("1")
65
+ g.add_vertex("2")
66
+ g.add_edge("1","2")
67
+ g.edges.size.should == 1
68
+ end
69
+
70
+ it "存在しないノード間エッジは禁止" do
71
+ g.add_edge("100","200").should raise_error
72
+ end
73
+
74
+ it "複数ノードの追加" do
75
+ 10.times{|i|
76
+ g.add_vertex(i.to_s)
77
+ }.should be_true
78
+ end
79
+
80
+ it "複数エッジの追加" do
81
+ g.add_edge("3","4")
82
+ g.add_edge("5","6")
83
+ g.add_edge("6","7")
84
+ g.add_edge("1","6")
85
+
86
+ g.edges.size.should == 5
87
+ end
88
+ end
89
+
90
+ describe Graph,"ノード削除に関するテスト" do
91
+ let(:g){Graph.new}
92
+
93
+ before(:all){
94
+ 10.times do |i|
95
+ g.add_vertex(i.to_s)
96
+ end
97
+ }
98
+
99
+
100
+ it "ノードを1つ削除" do
101
+ size = g.vertices.size
102
+ g.remove_vertex("1")
103
+ (size - g.vertices.size).should == 1
104
+ end
105
+ end
106
+
107
+ describe Graph,"エッジ削除に関するテスト" do
108
+
109
+ let(:g){Graph.new}
110
+
111
+ before(:all){
112
+ 10.times do |i|
113
+ g.add_vertex(i.to_s)
114
+ end
115
+
116
+ g.add_edge("1","2")
117
+ g.add_edge("3","4")
118
+ g.add_edge("5","6")
119
+ }
120
+
121
+ it "エッジを1つ削除" do
122
+ size = g.edges.size
123
+ g.remove_edge("1")
124
+ (size - g.edges.size).should == 1
125
+ end
126
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cecelia
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.5
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-08-22 00:00:00.000000000 Z
12
+ date: 2012-09-25 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: sqlite3
@@ -76,6 +76,7 @@ files:
76
76
  - lib/cecelia/graph_model.rb
77
77
  - lib/cecelia/version.rb
78
78
  - pkg/cecelia-0.0.1.gem
79
+ - spec/graph_spec.rb
79
80
  homepage: https://github.com/onodes/cecelia
80
81
  licenses: []
81
82
  post_install_message:
@@ -100,5 +101,6 @@ rubygems_version: 1.8.24
100
101
  signing_key:
101
102
  specification_version: 3
102
103
  summary: cecelia
103
- test_files: []
104
+ test_files:
105
+ - spec/graph_spec.rb
104
106
  has_rdoc: