Class: Store::Memory
- Inherits:
-
Object
show all
- Defined in:
- lib/store/memory.rb
Defined Under Namespace
Classes: EqualFilter, GTEFilter, GTFilter, LTFilter
Instance Attribute Summary collapse
Instance Method Summary
collapse
-
#all(table) ⇒ Object
-
#collate(table, filters, opts = {}) ⇒ Object
-
#count(table) ⇒ Object
-
#create(table, entry) ⇒ Object
-
#create_equal_filter(field, value) ⇒ Object
-
#create_gt_filter(field, value) ⇒ Object
-
#create_gte_filter(field, value) ⇒ Object
-
#create_lt_filter(field, value) ⇒ Object
-
#each(table, &block) ⇒ Object
-
#find(table, filters, opts = {}) ⇒ Object
-
#initialize(database_name) ⇒ Memory
constructor
A new instance of Memory.
-
#reset(table) ⇒ Object
-
#update(table, id, entry) ⇒ Object
Constructor Details
#initialize(database_name) ⇒ Memory
Returns a new instance of Memory.
8
9
10
11
|
# File 'lib/store/memory.rb', line 8
def initialize database_name
@collections = {}
@id = 0
end
|
Instance Attribute Details
#timestamper ⇒ Object
4
5
6
|
# File 'lib/store/memory.rb', line 4
def timestamper
@timestamper || lambda {Time.new}
end
|
Instance Method Details
#all(table) ⇒ Object
25
26
27
|
# File 'lib/store/memory.rb', line 25
def all table
collection(table).values
end
|
#collate(table, filters, opts = {}) ⇒ Object
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
|
# File 'lib/store/memory.rb', line 90
def collate table, filters, opts={}
start = opts.delete(:start)
limit = opts.delete(:limit)
facetlimit = opts.delete(:facetlimit)
result = {
items: find(table, filters, opts)
}
if opts[:facets]
result[:facets] = calculate_facets(opts[:facets], result[:items])
if facetlimit
result[:facets].each do |k,v|
v.pop while v.count > facetlimit
end
end
end
result[:count] = result[:items].count
if start
start.times do |i|
result[:items].shift
end
end
if limit
result[:items].pop while result[:items].count > limit
end
result
end
|
#count(table) ⇒ Object
29
30
31
|
# File 'lib/store/memory.rb', line 29
def count table
collection(table).count
end
|
#create(table, entry) ⇒ Object
13
14
15
16
17
18
19
20
21
22
23
|
# File 'lib/store/memory.rb', line 13
def create table, entry
if !entry['_id']
entry['_id'] = @id += 1
end
entry.keys.each do |k|
entry[k.to_s] = entry.delete(k)
end
entry['updated_at'] = entry['created_at'] = timestamper.call
collection(table)[entry['_id']] = entry
entry['_id']
end
|
#create_equal_filter(field, value) ⇒ Object
157
158
159
|
# File 'lib/store/memory.rb', line 157
def create_equal_filter field, value
EqualFilter.new(field,value)
end
|
#create_gt_filter(field, value) ⇒ Object
163
164
165
|
# File 'lib/store/memory.rb', line 163
def create_gt_filter field, value
GTFilter.new(field,value)
end
|
#create_gte_filter(field, value) ⇒ Object
166
167
168
|
# File 'lib/store/memory.rb', line 166
def create_gte_filter field, value
GTEFilter.new(field,value)
end
|
#create_lt_filter(field, value) ⇒ Object
160
161
162
|
# File 'lib/store/memory.rb', line 160
def create_lt_filter field, value
LTFilter.new(field,value)
end
|
#each(table, &block) ⇒ Object
33
34
35
|
# File 'lib/store/memory.rb', line 33
def each table, &block
collection(table).values.each &block
end
|
#find(table, filters, opts = {}) ⇒ Object
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
|
# File 'lib/store/memory.rb', line 41
def find table, filters, opts={}
values = collection(table).values
filters.each do |filter|
values = filter.filter(values)
end
if opts[:sort]
fields = opts[:sort].split(',')
fields.map! do |field|
sort = field.split('=')
[sort[0], (sort[1] || 1).to_i]
end
values.sort! do |e1,e2|
order = 0
fields.each do |field|
name = field[0]
asc = field[1]
f1 = e1[name]
f2 = e2[name]
f1 = 1 if f1 == true
f1 = 0 if f1 == false
f2 = 1 if f2 == true
f2 = 0 if f2 == false
order = asc * ((f1 <=> f2) || 0)
break if order != 0
end
order
end
end
if opts[:start]
opts[:start].times do |i|
values.shift
end
end
if opts[:limit]
values.pop while values.count > opts[:limit]
end
values
end
|
#reset(table) ⇒ Object
37
38
39
|
# File 'lib/store/memory.rb', line 37
def reset table
@collections.delete(table)
end
|
#update(table, id, entry) ⇒ Object
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
|
# File 'lib/store/memory.rb', line 125
def update table, id, entry
old_entry=nil
if id.kind_of?(Hash)
collection(table).each do |orig_k,orig_v|
if id.all?{|k,v| orig_v[k.to_s] == v}
old_entry = orig_v
id = orig_k
break;
end
end
else
old_entry = collection(table)[id]
end
if not old_entry
create table, entry
return entry
end
entry.keys.each do |key|
entry[key.to_s] = entry.delete(key)
end
entry = old_entry.merge(entry)
entry['updated_at'] = timestamper.call
collection(table)[id] = entry
entry
end
|