Class: Schron::Datastore::Memory

Inherits:
Object
  • Object
show all
Includes:
Interface
Defined in:
lib/schron/datastore/memory.rb

Instance Method Summary collapse

Methods included from Interface

#get

Constructor Details

#initializeMemory

Returns a new instance of Memory.



9
10
11
# File 'lib/schron/datastore/memory.rb', line 9

def initialize
  @data = {}
end

Instance Method Details

#exec_count(query) ⇒ Object



20
21
22
# File 'lib/schron/datastore/memory.rb', line 20

def exec_count(query)
  exec_query(query).count
end

#exec_query(query) ⇒ Object



13
14
15
16
17
18
# File 'lib/schron/datastore/memory.rb', line 13

def exec_query(query)
  results = data_for_kind(query.kind).values
  results = apply_filters(query, results)
  results = apply_sorts(query, results)
  apply_limit_offset(query, results) || []
end

#insert(kind, hash) ⇒ Object



28
29
30
31
32
# File 'lib/schron/datastore/memory.rb', line 28

def insert(kind, hash)
  hash = hash.merge(id: generate_id) unless hash[:id]
  set_data(kind, hash[:id], hash)
  hash.dup
end

#multi_get(kind, ids) ⇒ Object



24
25
26
# File 'lib/schron/datastore/memory.rb', line 24

def multi_get(kind, ids)
  ids.map { |id| get_data(kind, id) }.compact
end

#multi_insert(kind, hashes) ⇒ Object



34
35
36
# File 'lib/schron/datastore/memory.rb', line 34

def multi_insert(kind, hashes)
  hashes.map { |h| insert(kind, h) }
end

#multi_remove(kind, ids) ⇒ Object



54
55
56
57
# File 'lib/schron/datastore/memory.rb', line 54

def multi_remove(kind, ids)
  ids.each { |id| remove(kind, id) }
  nil
end

#multi_update(kind, hashes) ⇒ Object



44
45
46
47
# File 'lib/schron/datastore/memory.rb', line 44

def multi_update(kind, hashes)
  raise "records must all have ids to multi update" unless hashes.all?{ |h| h[:id] }
  hashes.map { |h| update(kind, h) }
end

#remove(kind, id) ⇒ Object



49
50
51
52
# File 'lib/schron/datastore/memory.rb', line 49

def remove(kind, id)
  data_for_kind(kind).delete(id)
  nil
end

#reset!Object



59
60
61
62
63
# File 'lib/schron/datastore/memory.rb', line 59

def reset!
  protect_reset do
    @data = {}
  end
end

#update(kind, hash) ⇒ Object



38
39
40
41
42
# File 'lib/schron/datastore/memory.rb', line 38

def update(kind, hash)
  raise "record must have id to update" unless hash.key?(:id)
  set_data(kind, hash[:id], hash)
  hash.dup
end