mementus 0.1.6 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,15 +1,7 @@
1
1
  ---
2
- !binary "U0hBMQ==":
3
- metadata.gz: !binary |-
4
- ZWVhNTIyY2Q4OGJjNTI0ZGQyNWUyMGEwMmU0NTNlZTJhOGIwZGI1Mg==
5
- data.tar.gz: !binary |-
6
- NGZhYTBkZjM3OWJiYzFiYWJkNjY0MGVlYTIyMmEwMjYwZDk1ZjhjMQ==
2
+ SHA1:
3
+ metadata.gz: 6ed27745c387e4e0efed9720fed30792a3535e45
4
+ data.tar.gz: 34ce7eccc734f7485125cf3568fb56f52cc0ffbc
7
5
  SHA512:
8
- metadata.gz: !binary |-
9
- NGQxYzEzMDk0NTU3YmViNTY5OWYwZTJhZGVkMTUyNTVhNDIzZmQ2NGE4OWJh
10
- ZWUzNzQzYTNkNTM1YTMzNjhjODI2OGYxMjA0ODY2MDVhMzRhMTIzZWZhZGJi
11
- ZTQwZDdlMTM1OGI3ODAyYjA0ZmIyZDVlOGYxZWZmYzcyZjhmYTc=
12
- data.tar.gz: !binary |-
13
- YmJhOThhNTUxOTgzMzY5ZjM1M2FjZjFkYmExNGZiNWYwOWIwYjc3MTJkM2U0
14
- MjdlNTM4MzU2MDQ0Y2E4NmRkYTZiNjIzMmRlMjM1YjZlM2VkYTA0YWZiY2Q1
15
- NzA5ODk0NGQ5NjU1N2I5YWFjMWZjZjNhMDg1YjI5OWIxNTIyMzA=
6
+ metadata.gz: 74239d3b58b80ec179dd5dfd7f7013ed42ec23e616f9050da61c1ce6f580aa1256d4ae4958b4280fa1030c9ccfa58638da89971bd44ef3ce0604f82fb2a1c8c5
7
+ data.tar.gz: 68306108232cae430a41f3e51f7df7d14c272c0fa1bc1cc9e5e226bc237c1c00e1cd1c4457053138194bffe7eab15fada9ee1ca1fc32a9f2925ef7b20c6d78d1
@@ -0,0 +1,18 @@
1
+ module Mementus
2
+ class Edge
3
+ attr_reader :from, :to
4
+
5
+ def initialize(from, to)
6
+ @from = from
7
+ @to = to
8
+ end
9
+
10
+ def nodes
11
+ [@from, @to]
12
+ end
13
+
14
+ def other(node)
15
+ @from == node ? @to : @from
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,64 @@
1
+ require 'set'
2
+
3
+ module Mementus
4
+ class Graph
5
+ def initialize(is_directed=true)
6
+ @index = {}
7
+ @is_directed = is_directed
8
+ end
9
+
10
+ def nodes_count
11
+ @index.size
12
+ end
13
+
14
+ def edges_count
15
+ c = 0
16
+ each_edge { c += 1 }
17
+ c
18
+ end
19
+
20
+ def directed?
21
+ @is_directed
22
+ end
23
+
24
+ def add_node(node)
25
+ @index[node] ||= Set.new
26
+ end
27
+
28
+ def add_edge(edge)
29
+ add_node(edge.from)
30
+ add_node(edge.to)
31
+
32
+ @index[edge.from].add(edge.to)
33
+ @index[edge.to].add(edge.from) unless directed?
34
+ end
35
+
36
+ def has_node?(node)
37
+ @index.key?(node)
38
+ end
39
+
40
+ def has_edge?(edge)
41
+ has_node?(edge.from) && @index[edge.from].include?(edge.to)
42
+ end
43
+
44
+ def each_node(&blk)
45
+ @index.each_key(&blk)
46
+ end
47
+
48
+ def each_adjacent(node, &blk)
49
+ @index[node].each(&blk)
50
+ end
51
+
52
+ def each_edge(&blk)
53
+ if directed?
54
+ each_node do |from|
55
+ each_adjacent(from) do |to|
56
+ yield Edge.new(from, to)
57
+ end
58
+ end
59
+ else
60
+ raise 'Edge traversal unsupported for undirected graphs'
61
+ end
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,10 @@
1
+ module Mementus
2
+ class Node
3
+ attr_reader :id, :label
4
+
5
+ def initialize(id, label)
6
+ @id = id
7
+ @label = label
8
+ end
9
+ end
10
+ end
@@ -1,3 +1,3 @@
1
1
  module Mementus
2
- VERSION = "0.1.6"
2
+ VERSION = "0.2.0"
3
3
  end
data/lib/mementus.rb CHANGED
@@ -1,7 +1,3 @@
1
- require 'virtus'
2
- require 'axiom-memory-adapter'
3
-
4
- require_relative 'mementus/version'
5
- require_relative 'mementus/adapter'
6
- require_relative 'mementus/relation'
7
- require_relative 'mementus/model'
1
+ require_relative 'mementus/graph'
2
+ require_relative 'mementus/node'
3
+ require_relative 'mementus/edge'
data/mementus.gemspec CHANGED
@@ -18,9 +18,6 @@ Gem::Specification.new do |spec|
18
18
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
19
  spec.require_paths = ["lib"]
20
20
 
21
- spec.add_runtime_dependency "virtus"
22
- spec.add_runtime_dependency "axiom-memory-adapter"
23
-
24
21
  spec.add_development_dependency "bundler", "~> 1.3"
25
22
  spec.add_development_dependency "rake"
26
23
  spec.add_development_dependency "rspec"
@@ -0,0 +1,45 @@
1
+ require 'spec_helper'
2
+
3
+ describe Mementus::Graph do
4
+ specify '#new' do
5
+ graph = Mementus::Graph.new
6
+
7
+ expect(graph.nodes_count).to eq(0)
8
+ expect(graph.edges_count).to eq(0)
9
+ end
10
+
11
+ specify '#add_node' do
12
+ graph = Mementus::Graph.new
13
+ graph.add_node(Mementus::Node.new(1, :node))
14
+
15
+ expect(graph.nodes_count).to eq(1)
16
+ expect(graph.edges_count).to eq(0)
17
+ end
18
+
19
+ specify '#add_edge' do
20
+ graph = Mementus::Graph.new
21
+ graph.add_edge(Mementus::Edge.new(Mementus::Node.new(1, :node), Mementus::Node.new(2, :node)))
22
+
23
+ expect(graph.nodes_count).to eq(2)
24
+ expect(graph.edges_count).to eq(1)
25
+ end
26
+
27
+ specify '#has_node?' do
28
+ graph = Mementus::Graph.new
29
+ node = Mementus::Node.new(1, :node)
30
+ graph.add_node(node)
31
+
32
+ expect(graph.has_node?(node)).to be true
33
+ expect(graph.has_node?(node.dup)).to be false
34
+ end
35
+
36
+ specify '#has_edge?' do
37
+ graph = Mementus::Graph.new
38
+ edge = Mementus::Edge.new(Mementus::Node.new(1, :node), Mementus::Node.new(2, :node))
39
+ edge2 = Mementus::Edge.new(Mementus::Node.new(1, :node), Mementus::Node.new(2, :node))
40
+ graph.add_edge(edge)
41
+
42
+ expect(graph.has_edge?(edge)).to be true
43
+ expect(graph.has_edge?(edge2)).to be false
44
+ end
45
+ end
metadata CHANGED
@@ -1,83 +1,55 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mementus
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.6
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - maetl
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-11-18 00:00:00.000000000 Z
11
+ date: 2016-03-26 00:00:00.000000000 Z
12
12
  dependencies:
13
- - !ruby/object:Gem::Dependency
14
- name: virtus
15
- requirement: !ruby/object:Gem::Requirement
16
- requirements:
17
- - - ! '>='
18
- - !ruby/object:Gem::Version
19
- version: '0'
20
- type: :runtime
21
- prerelease: false
22
- version_requirements: !ruby/object:Gem::Requirement
23
- requirements:
24
- - - ! '>='
25
- - !ruby/object:Gem::Version
26
- version: '0'
27
- - !ruby/object:Gem::Dependency
28
- name: axiom-memory-adapter
29
- requirement: !ruby/object:Gem::Requirement
30
- requirements:
31
- - - ! '>='
32
- - !ruby/object:Gem::Version
33
- version: '0'
34
- type: :runtime
35
- prerelease: false
36
- version_requirements: !ruby/object:Gem::Requirement
37
- requirements:
38
- - - ! '>='
39
- - !ruby/object:Gem::Version
40
- version: '0'
41
13
  - !ruby/object:Gem::Dependency
42
14
  name: bundler
43
15
  requirement: !ruby/object:Gem::Requirement
44
16
  requirements:
45
- - - ~>
17
+ - - "~>"
46
18
  - !ruby/object:Gem::Version
47
19
  version: '1.3'
48
20
  type: :development
49
21
  prerelease: false
50
22
  version_requirements: !ruby/object:Gem::Requirement
51
23
  requirements:
52
- - - ~>
24
+ - - "~>"
53
25
  - !ruby/object:Gem::Version
54
26
  version: '1.3'
55
27
  - !ruby/object:Gem::Dependency
56
28
  name: rake
57
29
  requirement: !ruby/object:Gem::Requirement
58
30
  requirements:
59
- - - ! '>='
31
+ - - ">="
60
32
  - !ruby/object:Gem::Version
61
33
  version: '0'
62
34
  type: :development
63
35
  prerelease: false
64
36
  version_requirements: !ruby/object:Gem::Requirement
65
37
  requirements:
66
- - - ! '>='
38
+ - - ">="
67
39
  - !ruby/object:Gem::Version
68
40
  version: '0'
69
41
  - !ruby/object:Gem::Dependency
70
42
  name: rspec
71
43
  requirement: !ruby/object:Gem::Requirement
72
44
  requirements:
73
- - - ! '>='
45
+ - - ">="
74
46
  - !ruby/object:Gem::Version
75
47
  version: '0'
76
48
  type: :development
77
49
  prerelease: false
78
50
  version_requirements: !ruby/object:Gem::Requirement
79
51
  requirements:
80
- - - ! '>='
52
+ - - ">="
81
53
  - !ruby/object:Gem::Version
82
54
  version: '0'
83
55
  description: In-memory data model
@@ -87,23 +59,22 @@ executables: []
87
59
  extensions: []
88
60
  extra_rdoc_files: []
89
61
  files:
90
- - .gitignore
91
- - .travis.yml
62
+ - ".gitignore"
63
+ - ".travis.yml"
92
64
  - Gemfile
93
65
  - LICENSE.txt
94
66
  - README.md
95
67
  - Rakefile
96
68
  - lib/mementus.rb
97
69
  - lib/mementus/adapter.rb
70
+ - lib/mementus/edge.rb
71
+ - lib/mementus/graph.rb
98
72
  - lib/mementus/model.rb
73
+ - lib/mementus/node.rb
99
74
  - lib/mementus/relation.rb
100
75
  - lib/mementus/version.rb
101
76
  - mementus.gemspec
102
- - spec/attribute_spec.rb
103
- - spec/collection_spec.rb
104
- - spec/empty_spec.rb
105
- - spec/mapping_spec.rb
106
- - spec/scopes_spec.rb
77
+ - spec/graph_spec.rb
107
78
  - spec/spec_helper.rb
108
79
  homepage: https://github.com/maetl/mementus
109
80
  licenses:
@@ -115,24 +86,21 @@ require_paths:
115
86
  - lib
116
87
  required_ruby_version: !ruby/object:Gem::Requirement
117
88
  requirements:
118
- - - ! '>='
89
+ - - ">="
119
90
  - !ruby/object:Gem::Version
120
91
  version: '0'
121
92
  required_rubygems_version: !ruby/object:Gem::Requirement
122
93
  requirements:
123
- - - ! '>='
94
+ - - ">="
124
95
  - !ruby/object:Gem::Version
125
96
  version: '0'
126
97
  requirements: []
127
98
  rubyforge_project:
128
- rubygems_version: 2.1.8
99
+ rubygems_version: 2.2.2
129
100
  signing_key:
130
101
  specification_version: 4
131
102
  summary: In-memory data model
132
103
  test_files:
133
- - spec/attribute_spec.rb
134
- - spec/collection_spec.rb
135
- - spec/empty_spec.rb
136
- - spec/mapping_spec.rb
137
- - spec/scopes_spec.rb
104
+ - spec/graph_spec.rb
138
105
  - spec/spec_helper.rb
106
+ has_rdoc:
@@ -1,40 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Mementus::Model do
4
-
5
- describe "Attributes" do
6
-
7
- class PrimaryAttributes < Mementus::Model
8
- attribute :name, String
9
- attribute :count, Integer
10
- attribute :switch, Boolean
11
- end
12
-
13
- let(:model) {
14
- PrimaryAttributes.new(
15
- :name => "a string",
16
- :count => 500,
17
- :switch => false
18
- )
19
- }
20
-
21
- it "reads attributes" do
22
- expect(model.name).to eq "a string"
23
- expect(model.count).to eq 500
24
- expect(model.switch).to eq false
25
- end
26
-
27
- it "writes attributes" do
28
- model.name = "stringstringstring"
29
- model.count = 488
30
- model.switch = true
31
-
32
- expect(model.name).to eq "stringstringstring"
33
- expect(model.count).to eq 488
34
- expect(model.switch).to eq true
35
- end
36
-
37
- end
38
-
39
-
40
- end
@@ -1,45 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Mementus::Model do
4
-
5
- describe "Collection" do
6
-
7
- class Item < Mementus::Model
8
- attribute :name, String
9
- attribute :order, Integer
10
- end
11
-
12
- before(:all) do
13
- 20.times do |i|
14
- item = Item.new
15
- item.name = "Item: #{i.to_s}"
16
- item.order = i + 1
17
- item.create
18
- end
19
- end
20
-
21
- it "counts created items" do
22
- expect(Item.collection.count).to eq 20
23
- end
24
-
25
- it "provides materialized objects" do
26
- expect(Item.all.count).to eq 20
27
- expect(Item.all.first.name).to eq "Item: 0"
28
- end
29
-
30
- it "finds item by equality match" do
31
- collection = Item.where(order: 10)
32
- expect(collection.count).to eq 1
33
- expect(collection.objects.first.name).to eq "Item: 9"
34
- end
35
-
36
- it "finds items by predicate match" do
37
- collection = Item.order(order: :desc)
38
- expect(collection.count).to eq 20
39
- expect(collection.objects.first.name).to eq "Item: 19"
40
- expect(collection.objects.last.name).to eq "Item: 0"
41
- end
42
-
43
- end
44
-
45
- end
data/spec/empty_spec.rb DELETED
@@ -1,18 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Mementus::Model do
4
-
5
- describe "Empty" do
6
-
7
- class EmptyItem < Mementus::Model
8
- attribute :id, String
9
- end
10
-
11
- it "returns an empty collection when no items found" do
12
- expect(EmptyItem.all).to be_instance_of(Array)
13
- expect(EmptyItem.all.count).to eq 0
14
- end
15
-
16
- end
17
-
18
- end
data/spec/mapping_spec.rb DELETED
@@ -1,35 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Mementus::Model do
4
-
5
- describe "Mapping" do
6
-
7
- class Mapping < Mementus::Model
8
- attribute :description, String
9
- attribute :number, Integer
10
- end
11
-
12
- let(:mapping) {
13
- Mapping.new(
14
- :description => "Hello world.",
15
- :number => 2014
16
- )
17
- }
18
-
19
- let(:cache_key) {
20
- mapping.cache_key
21
- }
22
-
23
- it "maps attribute schema to tuple" do
24
- schema = [[:__cache_key, String], [:description, String], [:number, Integer]]
25
- expect(mapping.schema_tuple).to eq schema
26
- end
27
-
28
- it "maps attribute values to tuple" do
29
- values = [mapping.cache_key, "Hello world.", 2014]
30
- expect(mapping.values_tuple).to eq values
31
- end
32
-
33
- end
34
-
35
- end
data/spec/scopes_spec.rb DELETED
@@ -1,30 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Mementus::Model do
4
-
5
- class CuteThing < Mementus::Model
6
- attribute :name, String
7
- attribute :category, String
8
- scope :cats, category: "cats"
9
- scope :dogs, category: "dogs"
10
- end
11
-
12
- before(:all) do
13
- 20.times do |i|
14
- thing = CuteThing.new
15
- thing.name = rand(36**12).to_s(36)
16
- thing.category = (i % 2) == 0 ? "dogs" : "cats"
17
- thing.create
18
- end
19
- end
20
-
21
- describe "#scope" do
22
-
23
- it "can execute named scopes" do
24
- expect(CuteThing.cats.count).to eq 10
25
- expect(CuteThing.dogs.count).to eq 10
26
- end
27
-
28
- end
29
-
30
- end