Class: Jouba::MemoryStore
- Inherits:
-
Object
- Object
- Jouba::MemoryStore
- Defined in:
- lib/jouba/store.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#db ⇒ Object
readonly
Returns the value of attribute db.
Class Method Summary collapse
Instance Method Summary collapse
- #delete(key) ⇒ Object
- #flush ⇒ Object
- #get(key, _ = {}) ⇒ Object
-
#initialize ⇒ MemoryStore
constructor
A new instance of MemoryStore.
- #persist(file_path) ⇒ Object
- #set(key, value) ⇒ Object
Constructor Details
#initialize ⇒ MemoryStore
Returns a new instance of MemoryStore.
8 9 10 |
# File 'lib/jouba/store.rb', line 8 def initialize flush end |
Instance Attribute Details
#db ⇒ Object (readonly)
Returns the value of attribute db.
6 7 8 |
# File 'lib/jouba/store.rb', line 6 def db @db end |
Class Method Details
.load(file_path) ⇒ Object
32 33 34 |
# File 'lib/jouba/store.rb', line 32 def self.load(file_path) new.tap { |store| store.instance_variable_set('@db', YAML.load_file(file_path)) } end |
Instance Method Details
#delete(key) ⇒ Object
20 21 22 |
# File 'lib/jouba/store.rb', line 20 def delete(key) db.delete(key) end |
#flush ⇒ Object
24 25 26 |
# File 'lib/jouba/store.rb', line 24 def flush @db = {} end |
#get(key, _ = {}) ⇒ Object
12 13 14 |
# File 'lib/jouba/store.rb', line 12 def get(key, _ = {}) db[key].nil? ? nil : deserialize(db[key]) end |
#persist(file_path) ⇒ Object
28 29 30 |
# File 'lib/jouba/store.rb', line 28 def persist(file_path) File.open(file_path, 'w') { |file| file.write @db.to_yaml } end |
#set(key, value) ⇒ Object
16 17 18 |
# File 'lib/jouba/store.rb', line 16 def set(key, value) db[key] = serialize(value) end |