Class: Smooth::Backends::Base
- Inherits:
-
Object
- Object
- Smooth::Backends::Base
show all
- Defined in:
- lib/smooth/backends/base.rb
Instance Method Summary
collapse
Constructor Details
#initialize(options = {}) ⇒ Base
Returns a new instance of Base.
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
# File 'lib/smooth/backends/base.rb', line 5
def initialize options={}
@id_counter = 0
@maximum_updated_at = Time.now.to_i
@namespace = options[:namespace]
records = options[:records] || {}
if records.is_a?(Array)
records = records.select {|r| r.is_a?(Hash) }.inject({}) do |memo,record|
record.symbolize_keys!
record[:id] ||= @id_counter += 1
memo[ record[:id] ] = record
memo
end
end
@storage = {id_counter: @id_counter, records: records, maximum_updated_at: @maximum_updated_at}
end
|
Instance Method Details
#create(attributes = {}) ⇒ Object
48
49
50
51
52
53
|
# File 'lib/smooth/backends/base.rb', line 48
def create attributes={}
attributes.symbolize_keys!
attributes[:id] = increment_id
@storage[:maximum_updated_at] = attributes[:created_at] = attributes[:updated_at] = Time.now.to_i
records[attributes[:id]] ||= attributes
end
|
#destroy(id) ⇒ Object
55
56
57
58
|
# File 'lib/smooth/backends/base.rb', line 55
def destroy id
record = records.delete(id)
!record.nil?
end
|
#index ⇒ Object
32
33
34
|
# File 'lib/smooth/backends/base.rb', line 32
def index
records.values
end
|
#maximum_updated_at ⇒ Object
28
29
30
|
# File 'lib/smooth/backends/base.rb', line 28
def maximum_updated_at
@storage[:maximum_updated_at]
end
|
#query(params = {}) ⇒ Object
A Naive query method which only matches for equality
61
62
63
64
65
66
67
68
69
70
71
|
# File 'lib/smooth/backends/base.rb', line 61
def query params={}
params.symbolize_keys!
index.select do |record|
record.symbolize_keys!
params.keys.all? do |key|
record[key] == params[key]
end
end
end
|
#records ⇒ Object
24
25
26
|
# File 'lib/smooth/backends/base.rb', line 24
def records
@storage[:records]
end
|
#show(id) ⇒ Object
36
37
38
|
# File 'lib/smooth/backends/base.rb', line 36
def show id
records[id.to_i]
end
|
#update(attributes = {}) ⇒ Object
40
41
42
43
44
45
46
|
# File 'lib/smooth/backends/base.rb', line 40
def update attributes={}
attributes.symbolize_keys!
@storage[:maximum_updated_at] = attributes[:updated_at] = Time.now.to_i
record = records[attributes[:id]]
record.merge!(attributes)
record
end
|