mementus 0.2.3 → 0.2.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/mementus/query/source.rb +13 -0
- data/lib/mementus/query/step.rb +13 -0
- data/lib/mementus/query/traversal.rb +25 -0
- data/lib/mementus/version.rb +1 -1
- data/lib/mementus.rb +3 -0
- data/spec/query_spec.rb +17 -0
- metadata +7 -3
- data/lib/mementus/model.rb +0 -120
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9008afd3cf8e01e46d74afdb9ba4421ba5fa6576
|
4
|
+
data.tar.gz: 3a23ffd82033bad559238dd80ec316efb86feb60
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a51672c818d3883aeecf5a599e8b33c6f5f342e8434345e3c93d60749a591781b8557dd640c3e74b98abb222702dd6a2a28ade00702f452f723f7144be3fb9a6
|
7
|
+
data.tar.gz: c35706222b866937ad4439a762df37969cd800bae904f91751c6eb2b557c293cdd2085676e53a4dee70ca667b09b5086244af3e05b84dea4db0daa9258b29c87
|
@@ -0,0 +1,25 @@
|
|
1
|
+
module Mementus
|
2
|
+
module Query
|
3
|
+
class Traversal
|
4
|
+
attr_reader :graph
|
5
|
+
|
6
|
+
def initialize(graph)
|
7
|
+
@graph = graph
|
8
|
+
@steps = []
|
9
|
+
end
|
10
|
+
|
11
|
+
def add_step(step)
|
12
|
+
@steps << step
|
13
|
+
self
|
14
|
+
end
|
15
|
+
|
16
|
+
def adjacent
|
17
|
+
@steps.last.to_node.adjacent
|
18
|
+
end
|
19
|
+
|
20
|
+
def id
|
21
|
+
@steps.last.to_node.id
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
data/lib/mementus/version.rb
CHANGED
data/lib/mementus.rb
CHANGED
@@ -4,3 +4,6 @@ require_relative 'mementus/edge'
|
|
4
4
|
require_relative 'mementus/node_proxy'
|
5
5
|
require_relative 'mementus/depth_first_search'
|
6
6
|
require_relative 'mementus/breadth_first_search'
|
7
|
+
require_relative 'mementus/query/source'
|
8
|
+
require_relative 'mementus/query/step'
|
9
|
+
require_relative 'mementus/query/traversal'
|
data/spec/query_spec.rb
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Mementus::Query do
|
4
|
+
let(:query) do
|
5
|
+
graph = Mementus::Graph.new
|
6
|
+
graph.add_edge(Mementus::Edge.new(Mementus::Node.new(1, :node), Mementus::Node.new(2, :node)))
|
7
|
+
Mementus::Query::Source.new(graph)
|
8
|
+
end
|
9
|
+
|
10
|
+
specify '#node' do
|
11
|
+
expect(query.node(1).id).to eq(1)
|
12
|
+
end
|
13
|
+
|
14
|
+
specify '#node#adjacent' do
|
15
|
+
expect(query.node(1).adjacent.first.id).to eq(2)
|
16
|
+
end
|
17
|
+
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.2.
|
4
|
+
version: 0.2.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-04-
|
11
|
+
date: 2016-04-13 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -70,14 +70,17 @@ files:
|
|
70
70
|
- lib/mementus/depth_first_search.rb
|
71
71
|
- lib/mementus/edge.rb
|
72
72
|
- lib/mementus/graph.rb
|
73
|
-
- lib/mementus/model.rb
|
74
73
|
- lib/mementus/node.rb
|
75
74
|
- lib/mementus/node_proxy.rb
|
75
|
+
- lib/mementus/query/source.rb
|
76
|
+
- lib/mementus/query/step.rb
|
77
|
+
- lib/mementus/query/traversal.rb
|
76
78
|
- lib/mementus/relation.rb
|
77
79
|
- lib/mementus/version.rb
|
78
80
|
- mementus.gemspec
|
79
81
|
- spec/graph_spec.rb
|
80
82
|
- spec/node_proxy_spec.rb
|
83
|
+
- spec/query_spec.rb
|
81
84
|
- spec/spec_helper.rb
|
82
85
|
- spec/traversal_spec.rb
|
83
86
|
homepage: https://github.com/maetl/mementus
|
@@ -107,6 +110,7 @@ summary: In-memory data model
|
|
107
110
|
test_files:
|
108
111
|
- spec/graph_spec.rb
|
109
112
|
- spec/node_proxy_spec.rb
|
113
|
+
- spec/query_spec.rb
|
110
114
|
- spec/spec_helper.rb
|
111
115
|
- spec/traversal_spec.rb
|
112
116
|
has_rdoc:
|
data/lib/mementus/model.rb
DELETED
@@ -1,120 +0,0 @@
|
|
1
|
-
require 'virtus'
|
2
|
-
require 'axiom-memory-adapter'
|
3
|
-
|
4
|
-
module Mementus
|
5
|
-
|
6
|
-
# Proof of concept for a toy ORM that combines some aspects of the
|
7
|
-
# ActiveRecord API with an in-memory query model based on the Axiom
|
8
|
-
# relational algebra API.
|
9
|
-
#
|
10
|
-
class Model
|
11
|
-
|
12
|
-
def self.inherited(concrete_model)
|
13
|
-
concrete_model.send(:include, Virtus.model)
|
14
|
-
concrete_model.send(:extend, Relation::ClassMethods)
|
15
|
-
end
|
16
|
-
|
17
|
-
@@local_storage = nil
|
18
|
-
@@model_registry = {}
|
19
|
-
|
20
|
-
def self.name_to_sym
|
21
|
-
name_without_namespace = name.split("::").last
|
22
|
-
name_without_namespace.gsub(/([^\^])([A-Z])/,'\1_\2').downcase.to_sym
|
23
|
-
end
|
24
|
-
|
25
|
-
# Unique cache identifier for this instance
|
26
|
-
#
|
27
|
-
def cache_key
|
28
|
-
"#{self.class.name_to_sym.to_s}-#{object_id.to_s}"
|
29
|
-
end
|
30
|
-
|
31
|
-
# TODO: this mapping could be based on declared indexes,
|
32
|
-
# rather than dumping the entire attribute schema here.
|
33
|
-
#
|
34
|
-
def schema_tuple
|
35
|
-
tuple = [[:__cache_key, String]]
|
36
|
-
attribute_set.each do |attribute_type|
|
37
|
-
tuple << [attribute_type.name.to_sym, attribute_type.primitive]
|
38
|
-
end
|
39
|
-
tuple
|
40
|
-
end
|
41
|
-
|
42
|
-
# TODO: this mapping could be based on declared indexes,
|
43
|
-
# rather than dumping the entire attribute data set here.
|
44
|
-
#
|
45
|
-
def values_tuple
|
46
|
-
tuple = [cache_key]
|
47
|
-
attributes.each do |_, attribute_value|
|
48
|
-
tuple << attribute_value
|
49
|
-
end
|
50
|
-
tuple
|
51
|
-
end
|
52
|
-
|
53
|
-
# Create only writes to memory. It's required in order to
|
54
|
-
# index the object's attributes in the query model.
|
55
|
-
#
|
56
|
-
def create
|
57
|
-
ensure_registered(self)
|
58
|
-
local_storage[self.class.name_to_sym].insert([values_tuple])
|
59
|
-
self.class.cache_put(self.cache_key, self)
|
60
|
-
end
|
61
|
-
|
62
|
-
def ensure_registered(model)
|
63
|
-
register(model) unless registered?(model)
|
64
|
-
end
|
65
|
-
|
66
|
-
def registered?(model)
|
67
|
-
model_registry.has_key? model.class.name_to_sym
|
68
|
-
end
|
69
|
-
|
70
|
-
def register(model)
|
71
|
-
model_id = model.class.name_to_sym
|
72
|
-
model_registry[model_id] = true
|
73
|
-
local_storage[model_id] = Axiom::Relation.new(model.schema_tuple)
|
74
|
-
end
|
75
|
-
|
76
|
-
def model_registry
|
77
|
-
@@model_registry
|
78
|
-
end
|
79
|
-
|
80
|
-
def local_storage
|
81
|
-
@@local_storage ||= Axiom::Adapter::Memory.new
|
82
|
-
end
|
83
|
-
|
84
|
-
def self.collection
|
85
|
-
@@local_storage ||= Axiom::Adapter::Memory.new
|
86
|
-
|
87
|
-
if @@local_storage.has_key?(name_to_sym)
|
88
|
-
@@local_storage[name_to_sym]
|
89
|
-
else
|
90
|
-
Axiom::Relation.new(self.new.schema_tuple)
|
91
|
-
end
|
92
|
-
end
|
93
|
-
|
94
|
-
# TODO: fix incomplete scope chaining
|
95
|
-
def self.scope(name, conditions)
|
96
|
-
if conditions.is_a? Hash
|
97
|
-
scope_method = lambda { self.where(conditions) }
|
98
|
-
elsif conditions.is_a? Proc
|
99
|
-
scope_method = conditions
|
100
|
-
end
|
101
|
-
|
102
|
-
define_singleton_method(name, &scope_method)
|
103
|
-
end
|
104
|
-
|
105
|
-
private
|
106
|
-
|
107
|
-
def self.cache
|
108
|
-
@@cache ||= {}
|
109
|
-
end
|
110
|
-
|
111
|
-
def self.cache_put(cache_key, object)
|
112
|
-
self.cache[cache_key] = object
|
113
|
-
end
|
114
|
-
|
115
|
-
def self.cache_get(cache_key)
|
116
|
-
self.cache[cache_key]
|
117
|
-
end
|
118
|
-
|
119
|
-
end
|
120
|
-
end
|