mementus 0.3.3 → 0.3.4

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 13343725a3e1defe38d65c9938c53a95755d223b
4
- data.tar.gz: 683c929e1e9cc54529498afefb199c005a75868f
3
+ metadata.gz: 68a2c93ad9bdaedbd2d5ca511da4ac9f7d37e032
4
+ data.tar.gz: 4c91f58f3e77c009595759c4fc895f496bf2e8d1
5
5
  SHA512:
6
- metadata.gz: 854588aedd67735d623bd05e92107b7ace162ebe81690f32cb35110316894f0e320b38eb4845ca620dfa7e3013398c0711ab10ee9df09f31cb92a81d502e65e2
7
- data.tar.gz: b0290808513880dba332a7c5f59c2471a0a11177d7bbe48d7ef6eab97fd295eacf157817e5ddb22e704e7c572fca9b2d8da7ad8981d4d8d627e504e9e5cab9ed
6
+ metadata.gz: a3f45b1dd11463fb0259b7c48d4024b0e4de41652acec66d0ea9e531474c26eedf221e4ea2985b8e6fbb6e45c654e3f3fe7bbb392402b25349ef4df204d38ec7
7
+ data.tar.gz: 822ede8244fb90e52fb8d489ab01b6822b9dadb2120911c1e26c184206405c574b75c1f53c6f84b62aac5d29e617e3024e68a9b86e81aa2bea290bbd0c16e7bf
data/lib/mementus/edge.rb CHANGED
@@ -1,30 +1,20 @@
1
1
  module Mementus
2
2
  class Edge
3
- attr_reader :from, :to, :label
3
+ attr_reader :from, :to, :label, :id
4
4
 
5
- def initialize(from=nil, to=nil, label=:edge)
6
- @from = from
7
- @to = to
8
- @label = label
9
- end
10
-
11
- def from=(node, label=:node)
12
- if node.is_a?(Node)
13
- @from = node
14
- else
15
- @from = Node.new(node, label)
16
- end
17
- end
5
+ def initialize(from, to, label=:edge)
6
+ @from = if from.is_a?(Integer)
7
+ Node.new(from)
8
+ else
9
+ from
10
+ end
18
11
 
19
- def to=(node)
20
- if node.is_a?(Node)
21
- @to = node
22
- else
23
- @to = Node.new(node, label)
24
- end
25
- end
12
+ @to = if to.is_a?(Integer)
13
+ Node.new(to)
14
+ else
15
+ to
16
+ end
26
17
 
27
- def label=(label)
28
18
  @label = label
29
19
  end
30
20
 
@@ -32,8 +22,14 @@ module Mementus
32
22
  [@from, @to]
33
23
  end
34
24
 
35
- def other(node)
36
- @from == node ? @to : @from
25
+ def ==(edge)
26
+ from.id == edge.from.id && to.id == edge.to.id && label == edge.label
27
+ end
28
+
29
+ alias :eql? :==
30
+
31
+ def hash
32
+ [from.id, to.id].hash
37
33
  end
38
34
  end
39
35
  end
@@ -40,10 +40,20 @@ module Mementus
40
40
  self
41
41
  end
42
42
 
43
+ def out_e
44
+ append_next(Pipes::OutgoingEdges.new)
45
+ self
46
+ end
47
+
43
48
  def in
44
49
  append_next(Pipes::Incoming.new)
45
50
  self
46
51
  end
52
+
53
+ def in_e
54
+ append_next(Pipes::IncomingEdges.new)
55
+ self
56
+ end
47
57
  end
48
58
 
49
59
  module Pipes
@@ -65,6 +75,14 @@ module Mementus
65
75
  end
66
76
  end
67
77
 
78
+ class OutgoingEdges
79
+ def process(graph, node)
80
+ graph.each_adjacent(node.id).map do |id|
81
+ Mementus::Edge.new(node, id)
82
+ end
83
+ end
84
+ end
85
+
68
86
  class Incoming
69
87
  def process(graph, node)
70
88
  incoming = []
@@ -78,5 +96,19 @@ module Mementus
78
96
  incoming
79
97
  end
80
98
  end
99
+
100
+ class IncomingEdges
101
+ def process(graph, node)
102
+ incoming = []
103
+
104
+ graph.each_node do |n|
105
+ graph.each_adjacent(n.id) do |id|
106
+ incoming << Mementus::Edge.new(n.id, id)
107
+ end
108
+ end
109
+
110
+ incoming
111
+ end
112
+ end
81
113
  end
82
114
  end
@@ -1,3 +1,3 @@
1
1
  module Mementus
2
- VERSION = '0.3.3'.freeze
2
+ VERSION = '0.3.4'.freeze
3
3
  end
data/lib/mementus.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  require_relative 'mementus/graph'
2
2
  require_relative 'mementus/structure/adjacency_list'
3
+ require_relative 'mementus/structure/incidence_list'
3
4
  require_relative 'mementus/node'
4
5
  require_relative 'mementus/edge'
5
6
  require_relative 'mementus/node_proxy'
data/spec/edge_spec.rb ADDED
@@ -0,0 +1,35 @@
1
+ require 'spec_helper'
2
+
3
+ describe Mementus::Edge do
4
+ it 'should initialize with integer ids' do
5
+ edge = Mementus::Edge.new(1, 2)
6
+
7
+ expect(edge.from).to be_a(Mementus::Node)
8
+ expect(edge.from.id).to eq(1)
9
+ expect(edge.to).to be_a(Mementus::Node)
10
+ expect(edge.to.id).to eq(2)
11
+ end
12
+
13
+ it 'should initialize with node instances' do
14
+ edge = Mementus::Edge.new(Mementus::Node.new(1), Mementus::Node.new(2))
15
+
16
+ expect(edge.from).to be_a(Mementus::Node)
17
+ expect(edge.from.id).to eq(1)
18
+ expect(edge.to).to be_a(Mementus::Node)
19
+ expect(edge.to.id).to eq(2)
20
+ end
21
+
22
+ it 'should initialize with label given' do
23
+ edge = Mementus::Edge.new(1, 2, :relationship)
24
+
25
+ expect(edge.label).to eq(:relationship)
26
+ end
27
+
28
+ it 'should test equality based on value' do
29
+ edge1 = Mementus::Edge.new(1, 2, :relationship)
30
+ edge2 = Mementus::Edge.new(1, 2, :relationship)
31
+
32
+ expect(edge1).to eq(edge2)
33
+ expect(edge1.hash).to eq(edge2.hash)
34
+ end
35
+ end
@@ -34,9 +34,21 @@ describe 'pipeline api' do
34
34
  expect(pipeline.all.first.id).to eq(2)
35
35
  end
36
36
 
37
+ it 'traverses to outgoing edges' do
38
+ pipeline = graph.n(1).out_e
39
+ expect(pipeline.all.count).to eq(1)
40
+ expect(pipeline.all.first.to.id).to eq(2)
41
+ end
42
+
37
43
  it 'traverses to incoming nodes' do
38
44
  pipeline = graph.n(2).in
39
45
  expect(pipeline.all.count).to eq(1)
40
46
  expect(pipeline.all.first.id).to eq(1)
41
47
  end
48
+
49
+ it 'traverses to incoming edges' do
50
+ pipeline = graph.n(2).in_e
51
+ expect(pipeline.all.count).to eq(1)
52
+ expect(pipeline.all.first.from.id).to eq(1)
53
+ end
42
54
  end
@@ -1,55 +1,84 @@
1
1
  require 'spec_helper'
2
2
 
3
- describe Mementus::Structure do
4
- let(:structure) do
5
- Mementus::Structure::AdjacencyList.new
3
+ shared_examples_for "a mutable graph data structure" do
4
+ describe '#new' do
5
+ it 'starts with empty node list' do
6
+ expect(structure.nodes_count).to eq(0)
7
+ end
8
+
9
+ it 'starts with empty edge list' do
10
+ expect(structure.edges_count).to eq(0)
11
+ end
6
12
  end
7
13
 
8
- specify '#new' do
9
- expect(structure.nodes_count).to eq(0)
10
- expect(structure.edges_count).to eq(0)
14
+ describe '#add_node' do
15
+ it 'adds a node object to the graph' do
16
+ structure.add_node(Mementus::Node.new(1, :node))
17
+
18
+ expect(structure.nodes_count).to eq(1)
19
+ expect(structure.edges_count).to eq(0)
20
+ end
11
21
  end
12
22
 
13
- specify '#add_node' do
14
- structure.add_node(Mementus::Node.new(1, :node))
23
+ describe '#add_edge' do
24
+ it 'adds an edge object to the graph' do
25
+ structure.add_edge(Mementus::Edge.new(Mementus::Node.new(1, :node), Mementus::Node.new(2, :node)))
15
26
 
16
- expect(structure.nodes_count).to eq(1)
17
- expect(structure.edges_count).to eq(0)
27
+ expect(structure.nodes_count).to eq(2)
28
+ expect(structure.edges_count).to eq(1)
29
+ end
18
30
  end
19
31
 
20
- specify '#add_edge' do
21
- structure.add_edge(Mementus::Edge.new(Mementus::Node.new(1, :node), Mementus::Node.new(2, :node)))
32
+ describe '#has_node?' do
33
+ it 'tests for the presence of a given node' do
34
+ node = Mementus::Node.new(1, :node)
35
+ structure.add_node(node)
22
36
 
23
- expect(structure.nodes_count).to eq(2)
24
- expect(structure.edges_count).to eq(1)
37
+ expect(structure.has_node?(node)).to be true
38
+ end
25
39
  end
26
40
 
27
- specify '#has_node?' do
28
- node = Mementus::Node.new(1, :node)
29
- structure.add_node(node)
41
+ describe '#has_edge?' do
42
+ it 'tests for the presence of a given edge' do
43
+ edge = Mementus::Edge.new(Mementus::Node.new(1, :node), Mementus::Node.new(2, :node))
44
+ structure.add_edge(edge)
30
45
 
31
- expect(structure.has_node?(node)).to be true
46
+ expect(structure.has_edge?(edge)).to be true
47
+ end
32
48
  end
33
49
 
34
- specify '#has_edge?' do
35
- edge = Mementus::Edge.new(Mementus::Node.new(1, :node), Mementus::Node.new(2, :node))
36
- structure.add_edge(edge)
50
+ describe '#node(id)' do
51
+ it 'finds a node by id' do
52
+ edge = Mementus::Edge.new(Mementus::Node.new(1, :node), Mementus::Node.new(2, :node))
53
+ structure.add_edge(edge)
37
54
 
38
- expect(structure.has_edge?(edge)).to be true
55
+ expect(structure.node(1).id).to eq(edge.from.id)
56
+ end
39
57
  end
40
58
 
41
- specify '#node(id)' do
42
- edge = Mementus::Edge.new(Mementus::Node.new(1, :node), Mementus::Node.new(2, :node))
43
- structure.add_edge(edge)
59
+ describe '#nodes' do
60
+ it 'lists all nodes in the graph' do
61
+ edge = Mementus::Edge.new(Mementus::Node.new(1, :node), Mementus::Node.new(2, :node))
62
+ structure.add_edge(edge)
63
+
64
+ expect(structure.nodes.first.id).to eq(edge.from.id)
65
+ expect(structure.nodes.last.id).to eq(edge.to.id)
66
+ end
67
+ end
68
+ end
44
69
 
45
- expect(structure.node(1).id).to eq(edge.from.id)
70
+ describe Mementus::Structure::AdjacencyList do
71
+ let(:structure) do
72
+ Mementus::Structure::AdjacencyList.new
46
73
  end
47
74
 
48
- specify '#nodes' do
49
- edge = Mementus::Edge.new(Mementus::Node.new(1, :node), Mementus::Node.new(2, :node))
50
- structure.add_edge(edge)
75
+ it_behaves_like "a mutable graph data structure"
76
+ end
51
77
 
52
- expect(structure.nodes.first.id).to eq(edge.from.id)
53
- expect(structure.nodes.last.id).to eq(edge.to.id)
78
+ describe Mementus::Structure::IncidenceList do
79
+ let(:structure) do
80
+ Mementus::Structure::IncidenceList.new
54
81
  end
82
+
83
+ it_behaves_like "a mutable graph data structure"
55
84
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mementus
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.3
4
+ version: 0.3.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - maetl
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-06-05 00:00:00.000000000 Z
11
+ date: 2016-06-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -85,6 +85,7 @@ files:
85
85
  - lib/mementus/version.rb
86
86
  - mementus.gemspec
87
87
  - spec/direction_spec.rb
88
+ - spec/edge_spec.rb
88
89
  - spec/graph_spec.rb
89
90
  - spec/integer_id_spec.rb
90
91
  - spec/node_proxy_spec.rb
@@ -119,6 +120,7 @@ specification_version: 4
119
120
  summary: Graph data structure toolkit for Ruby applications.
120
121
  test_files:
121
122
  - spec/direction_spec.rb
123
+ - spec/edge_spec.rb
122
124
  - spec/graph_spec.rb
123
125
  - spec/integer_id_spec.rb
124
126
  - spec/node_proxy_spec.rb