Class: Endymion::Memory

Inherits:
Object
  • Object
show all
Defined in:
lib/endymion/memory.rb,
lib/endymion/memory/helper.rb

Defined Under Namespace

Modules: Helper

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Memory



8
9
10
# File 'lib/endymion/memory.rb', line 8

def initialize(opts={})
  @store = {}
end

Instance Method Details

#count(query) ⇒ Object



57
58
59
# File 'lib/endymion/memory.rb', line 57

def count(query)
  find(query).length
end

#create(records) ⇒ Object



21
22
23
24
25
26
27
28
# File 'lib/endymion/memory.rb', line 21

def create(records)
  records.map do |record|
    raise 'duplicate key' if record[:key] && store.key?(record[:key])
    record[:key] ||= generate_key
    store[record[:key]] = record
    record
  end
end

#delete(query) ⇒ Object



49
50
51
52
53
54
55
# File 'lib/endymion/memory.rb', line 49

def delete(query)
  records = find(query)
  store.delete_if do |key, record|
    records.include?(record)
  end
  nil
end

#delete_by_key(kind, key) ⇒ Object



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

def delete_by_key(kind, key)
  store.delete(key)
  nil
end

#find(query) ⇒ Object



34
35
36
37
38
39
40
41
42
# File 'lib/endymion/memory.rb', line 34

def find(query)
  records = store.values
  records = filter_kind(query.kind,             records)
  records = Helper.apply_filters(query.filters, records)
  records = Helper.apply_sorts(query.sorts,     records)
  records = Helper.apply_offset(query.offset,   records)
  records = Helper.apply_limit(query.limit,     records)
  records
end

#find_by_key(kind, key) ⇒ Object



30
31
32
# File 'lib/endymion/memory.rb', line 30

def find_by_key(kind, key)
  store[key]
end

#save(records) ⇒ Object



12
13
14
15
16
17
18
19
# File 'lib/endymion/memory.rb', line 12

def save(records)
  records.map do |record|
    key = Endymion.new?(record) ? generate_key : record[:key]
    record[:key] = key
    store[key] = record
    record
  end
end