perpetuity-memory 0.1.1 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +7 -1
- data/lib/perpetuity/memory.rb +42 -25
- data/lib/perpetuity/memory/version.rb +1 -1
- data/spec/perpetuity/integration_spec.rb +42 -0
- data/spec/perpetuity/memory_spec.rb +6 -6
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2b6a27a63d63bec0a710cbb16bf011a2ecfb72e4
|
4
|
+
data.tar.gz: f52a34142f093b8d0dd0bc736d9c60ea5f2fd010
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 14e35879db317de4749ef77d02943e61fd9205f597bf0e28ba4096d9bfe3aecada518dec903b7de24e928cf30fb3f19c5ccabcac61cce029c4d2003b4c90bd1d
|
7
|
+
data.tar.gz: 384fdef9534a665575810c9d94e1c6729c2efe8473695848f70dcfb4d1464e1dfeaa0e026b37693431e36c62b54b56408d8c5e02104b83206c7e45cb55d8c47a
|
data/README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# Perpetuity::Memory
|
2
2
|
|
3
|
-
This is the in-memory adapter for Perpetuity.
|
3
|
+
This is the in-memory adapter for the [Perpetuity](https://github.com/jgaskins/perpetuity) ORM.
|
4
4
|
|
5
5
|
## Installation
|
6
6
|
|
@@ -27,6 +27,12 @@ Perpetuity.data_source :memory
|
|
27
27
|
|
28
28
|
For information on using Perpetuity to persist your Ruby objects, see the [main Perpetuity repo](https://github.com/jgaskins/perpetuity).
|
29
29
|
|
30
|
+
## TODO
|
31
|
+
|
32
|
+
* Make sure update works.
|
33
|
+
* Make sure save works.
|
34
|
+
* Make sure identity map works properly.
|
35
|
+
|
30
36
|
## Contributing
|
31
37
|
|
32
38
|
1. Fork it
|
data/lib/perpetuity/memory.rb
CHANGED
@@ -11,32 +11,22 @@ module Perpetuity
|
|
11
11
|
@indexes = Hash.new
|
12
12
|
end
|
13
13
|
|
14
|
-
def insert klass,
|
15
|
-
if
|
16
|
-
return
|
14
|
+
def insert klass, object, _
|
15
|
+
if object.is_a? Array
|
16
|
+
return object.map{|obj| insert(klass, obj, _)}
|
17
17
|
end
|
18
18
|
|
19
|
-
|
20
|
-
attributes[:id] = SecureRandom.uuid
|
21
|
-
end
|
22
|
-
|
23
|
-
# make keys indifferent
|
24
|
-
attributes.default_proc = proc do |h, k|
|
25
|
-
case k
|
26
|
-
when String then sym = k.to_sym; h[sym] if h.key?(sym)
|
27
|
-
when Symbol then str = k.to_s; h[str] if h.key?(str)
|
28
|
-
end
|
29
|
-
end
|
19
|
+
id = get_id(object) || set_id(object, SecureRandom.uuid)
|
30
20
|
|
31
|
-
collection(klass)
|
32
|
-
|
21
|
+
collection(klass)[id] = object
|
22
|
+
id
|
33
23
|
end
|
34
24
|
|
35
25
|
def count klass, criteria=nil, &block
|
36
26
|
if block_given?
|
37
|
-
collection(klass).select(&block).size
|
27
|
+
collection(klass).values.select(&block).size
|
38
28
|
elsif criteria
|
39
|
-
collection(klass).select(&criteria).size
|
29
|
+
collection(klass).values.select(&criteria).size
|
40
30
|
else
|
41
31
|
collection(klass).size
|
42
32
|
end
|
@@ -47,19 +37,19 @@ module Perpetuity
|
|
47
37
|
end
|
48
38
|
|
49
39
|
def first klass
|
50
|
-
|
40
|
+
all(klass).first
|
51
41
|
end
|
52
42
|
|
53
43
|
def find klass, id
|
54
|
-
collection(klass)
|
44
|
+
collection(klass)[id]
|
55
45
|
end
|
56
46
|
|
57
47
|
def retrieve klass, criteria, options = {}
|
58
|
-
collection(klass).find_all(&criteria)
|
48
|
+
collection(klass).values.find_all(&criteria)
|
59
49
|
end
|
60
50
|
|
61
51
|
def all klass
|
62
|
-
collection(klass)
|
52
|
+
collection(klass).values
|
63
53
|
end
|
64
54
|
|
65
55
|
def delete object, klass=nil
|
@@ -90,11 +80,11 @@ module Perpetuity
|
|
90
80
|
end
|
91
81
|
|
92
82
|
def serialize object, mapper
|
93
|
-
|
83
|
+
object.dup
|
94
84
|
end
|
95
85
|
|
96
86
|
def unserialize data, mapper
|
97
|
-
|
87
|
+
data.dup
|
98
88
|
end
|
99
89
|
|
100
90
|
|
@@ -124,8 +114,35 @@ module Perpetuity
|
|
124
114
|
protected
|
125
115
|
|
126
116
|
def collection klass
|
127
|
-
@cache[klass] =
|
117
|
+
@cache[klass] = Hash.new unless @cache.has_key? klass
|
128
118
|
@cache[klass]
|
129
119
|
end
|
120
|
+
|
121
|
+
def get_id(object)
|
122
|
+
if object.respond_to?(:id)
|
123
|
+
object.id
|
124
|
+
elsif object.respond_to?(:fetch)
|
125
|
+
object.fetch(:id, nil)
|
126
|
+
elsif object.respond_to?(:[])
|
127
|
+
object[:id] || nil
|
128
|
+
else
|
129
|
+
object.instance_variable_get(:@id)
|
130
|
+
end
|
131
|
+
end
|
132
|
+
|
133
|
+
def set_id(object, id)
|
134
|
+
if object.respond_to?(:id=)
|
135
|
+
object.id = id
|
136
|
+
elsif object.respond_to?(:[]=)
|
137
|
+
object[:id] = id
|
138
|
+
else
|
139
|
+
object.instance_variable_set(:@id, id)
|
140
|
+
end
|
141
|
+
id
|
142
|
+
end
|
130
143
|
end
|
131
144
|
end
|
145
|
+
|
146
|
+
|
147
|
+
# Register the adapter with Perpetuity.
|
148
|
+
Perpetuity::Configuration.adapters[:memory] = 'Perpetuity::Memory'
|
@@ -0,0 +1,42 @@
|
|
1
|
+
require_relative '../../lib/perpetuity/memory'
|
2
|
+
|
3
|
+
describe 'Full Perpetuity stack using in-memory adapter' do
|
4
|
+
|
5
|
+
before do
|
6
|
+
Perpetuity.data_source :memory
|
7
|
+
class User
|
8
|
+
attr_reader :name, :age
|
9
|
+
def initialize(name, age)
|
10
|
+
@name = name
|
11
|
+
@age = age
|
12
|
+
end
|
13
|
+
end
|
14
|
+
Perpetuity.generate_mapper_for(User) do
|
15
|
+
attribute :name
|
16
|
+
attribute :age
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
let(:mapper) { Perpetuity[User] }
|
21
|
+
let(:user) { User.new(name, age) }
|
22
|
+
let(:name) { 'Craig' }
|
23
|
+
let(:age) { 43 }
|
24
|
+
|
25
|
+
it 'can store an object' do
|
26
|
+
mapper.insert(user)
|
27
|
+
expect(mapper.count).to eq(1)
|
28
|
+
end
|
29
|
+
|
30
|
+
it 'can retrieve a stored object by ID' do
|
31
|
+
id = mapper.insert(user)
|
32
|
+
expect(mapper.count).to eq(1)
|
33
|
+
expect(mapper.find(id).name).to eq(name)
|
34
|
+
expect(mapper.find(id).age).to eq(age)
|
35
|
+
end
|
36
|
+
|
37
|
+
it 'can retrieve a stored object by attribute' do
|
38
|
+
mapper.insert(user)
|
39
|
+
expect(mapper.count).to eq(1)
|
40
|
+
expect(mapper.find{|u| u.name == 'Craig'}.age).to eq(age)
|
41
|
+
end
|
42
|
+
end
|
@@ -38,7 +38,7 @@ module Perpetuity
|
|
38
38
|
it 'gets the first object' do
|
39
39
|
value = {value: 1}
|
40
40
|
subject.insert klass, value, []
|
41
|
-
subject.first(klass)[:value].should == value[
|
41
|
+
subject.first(klass)[:value].should == value[:value]
|
42
42
|
end
|
43
43
|
|
44
44
|
it 'gets all objects' do
|
@@ -47,12 +47,12 @@ module Perpetuity
|
|
47
47
|
subject.all(klass).should == values
|
48
48
|
end
|
49
49
|
|
50
|
-
it 'retrieves by ID
|
50
|
+
it 'retrieves by ID' do
|
51
51
|
time = Time.now.utc
|
52
52
|
id = subject.insert Object, {inserted: time}, []
|
53
53
|
|
54
|
-
object = subject.
|
55
|
-
retrieved_time = object[
|
54
|
+
object = subject.find(Object, id)
|
55
|
+
retrieved_time = object[:inserted]
|
56
56
|
retrieved_time.to_f.should be_within(0.001).of time.to_f
|
57
57
|
end
|
58
58
|
|
@@ -74,9 +74,9 @@ module Perpetuity
|
|
74
74
|
subject.increment klass, id, :count
|
75
75
|
subject.increment klass, id, :count, 10
|
76
76
|
query = subject.query { |o| o[:id] == id }
|
77
|
-
subject.retrieve(klass, query).first[
|
77
|
+
subject.retrieve(klass, query).first[:count].should be == 12
|
78
78
|
subject.increment klass, id, :count, -1
|
79
|
-
subject.retrieve(klass, query).first[
|
79
|
+
subject.retrieve(klass, query).first[:count].should be == 11
|
80
80
|
end
|
81
81
|
end
|
82
82
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: perpetuity-memory
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jamie Gaskins
|
@@ -10,7 +10,7 @@ authors:
|
|
10
10
|
autorequire:
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
|
-
date: 2014-01-
|
13
|
+
date: 2014-01-13 00:00:00.000000000 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: bundler
|
@@ -83,6 +83,7 @@ files:
|
|
83
83
|
- lib/perpetuity/memory.rb
|
84
84
|
- lib/perpetuity/memory/version.rb
|
85
85
|
- perpetuity-memory.gemspec
|
86
|
+
- spec/perpetuity/integration_spec.rb
|
86
87
|
- spec/perpetuity/memory_spec.rb
|
87
88
|
homepage: https://github.com/boochtek/perpetuity-memory
|
88
89
|
licenses:
|
@@ -109,4 +110,5 @@ signing_key:
|
|
109
110
|
specification_version: 4
|
110
111
|
summary: In-memory adapter for Perpetuity
|
111
112
|
test_files:
|
113
|
+
- spec/perpetuity/integration_spec.rb
|
112
114
|
- spec/perpetuity/memory_spec.rb
|