Class: Hyperion::Memory
- Inherits:
-
Object
- Object
- Hyperion::Memory
- Defined in:
- lib/hyperion/memory.rb
Instance Method Summary collapse
- #count(query) ⇒ Object
- #delete(query) ⇒ Object
- #delete_by_key(key) ⇒ Object
- #find(query) ⇒ Object
- #find_by_key(key) ⇒ Object
-
#initialize(opts = {}) ⇒ Memory
constructor
A new instance of Memory.
- #pack_key(kind, key) ⇒ Object
- #save(records) ⇒ Object
- #unpack_key(kind, key) ⇒ Object
Constructor Details
#initialize(opts = {}) ⇒ Memory
Returns a new instance of Memory.
7 8 9 |
# File 'lib/hyperion/memory.rb', line 7 def initialize(opts={}) @store = {} end |
Instance Method Details
#count(query) ⇒ Object
47 48 49 |
# File 'lib/hyperion/memory.rb', line 47 def count(query) find(query).length end |
#delete(query) ⇒ Object
39 40 41 42 43 44 45 |
# File 'lib/hyperion/memory.rb', line 39 def delete(query) records = find(query) store.delete_if do |key, record| records.include?(record) end nil end |
#delete_by_key(key) ⇒ Object
34 35 36 37 |
# File 'lib/hyperion/memory.rb', line 34 def delete_by_key(key) store.delete(key) nil end |
#find(query) ⇒ Object
24 25 26 27 28 29 30 31 32 |
# File 'lib/hyperion/memory.rb', line 24 def find(query) records = store.values records = filter_kind(query.kind, records) records = apply_filters(query.filters, records) records = apply_sorts(query.sorts, records) records = apply_offset(query.offset, records) records = apply_limit(query.limit, records) records end |
#find_by_key(key) ⇒ Object
20 21 22 |
# File 'lib/hyperion/memory.rb', line 20 def find_by_key(key) store[key] end |
#pack_key(kind, key) ⇒ Object
51 52 53 |
# File 'lib/hyperion/memory.rb', line 51 def pack_key(kind, key) key end |
#save(records) ⇒ Object
11 12 13 14 15 16 17 18 |
# File 'lib/hyperion/memory.rb', line 11 def save(records) records.map do |record| key = Hyperion.new?(record) ? generate_key : record[:key] record[:key] = key store[key] = record record end end |
#unpack_key(kind, key) ⇒ Object
55 56 57 |
# File 'lib/hyperion/memory.rb', line 55 def unpack_key(kind, key) key end |