Class: Teton::Stores::Memory
- Inherits:
-
Object
- Object
- Teton::Stores::Memory
- Defined in:
- lib/teton/stores/memory.rb
Overview
Plugs in a light-weight store that can be used for modeling other stores.
Constant Summary collapse
- CREATED_AT_KEY =
'created_at'
- IDS_KEY =
'ids'
- INDICES_KEY =
'indices'
- META_KEY =
'meta'
- DATA_KEY =
'data'
- UPDATED_AT_KEY =
'updated_at'
Instance Attribute Summary collapse
-
#store ⇒ Object
readonly
Returns the value of attribute store.
Instance Method Summary collapse
- #count(key) ⇒ Object
- #del(key) ⇒ Object
- #from_json!(json) ⇒ Object
- #get(key, limit: nil, skip: nil) ⇒ Object
-
#initialize(store = {}) ⇒ Memory
constructor
A new instance of Memory.
-
#load!(path) ⇒ Object
Persistence API.
- #save!(path) ⇒ Object
-
#set(key, data) ⇒ Object
Main Object API.
- #to_json(*_args) ⇒ Object
Constructor Details
#initialize(store = {}) ⇒ Memory
Returns a new instance of Memory.
16 17 18 |
# File 'lib/teton/stores/memory.rb', line 16 def initialize(store = {}) @store = store || {} end |
Instance Attribute Details
#store ⇒ Object (readonly)
Returns the value of attribute store.
14 15 16 |
# File 'lib/teton/stores/memory.rb', line 14 def store @store end |
Instance Method Details
#count(key) ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/teton/stores/memory.rb', line 52 def count(key) store_pointer = traverse_to_last(key) return 0 unless store_pointer if key.resource? (store_pointer.dig(key.last_part, IDS_KEY) || {}).keys.length else store_pointer.dig(key.last_part, DATA_KEY) ? 1 : 0 end end |
#del(key) ⇒ Object
42 43 44 45 46 47 48 49 50 |
# File 'lib/teton/stores/memory.rb', line 42 def del(key) store_pointer = traverse_to_last(key) return self unless store_pointer store_pointer.delete(key.last_part) self end |
#from_json!(json) ⇒ Object
80 81 82 83 84 |
# File 'lib/teton/stores/memory.rb', line 80 def from_json!(json) @store = JSON.parse(json) self end |
#get(key, limit: nil, skip: nil) ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/teton/stores/memory.rb', line 30 def get(key, limit: nil, skip: nil) store_pointer = traverse_to_last(key) return unless store_pointer if key.resource? entries(key, store_pointer, key.last_part, limit: limit, skip: skip) else entry(key, store_pointer, key.last_part) end end |
#load!(path) ⇒ Object
Persistence API
66 67 68 |
# File 'lib/teton/stores/memory.rb', line 66 def load!(path) from_json!(File.read(path)) end |
#save!(path) ⇒ Object
70 71 72 73 74 75 76 77 78 |
# File 'lib/teton/stores/memory.rb', line 70 def save!(path) dir = File.dirname(path) FileUtils.mkdir_p(dir) File.write(path, to_json) self end |
#set(key, data) ⇒ Object
Main Object API
22 23 24 25 26 27 28 |
# File 'lib/teton/stores/memory.rb', line 22 def set(key, data) store_pointer = insert_traverse_to_last(key) upsert(store_pointer, key.last_part, data) self end |
#to_json(*_args) ⇒ Object
86 87 88 |
# File 'lib/teton/stores/memory.rb', line 86 def to_json(*_args) store.to_json end |