Class: Mooset::Storage::Memory
- Inherits:
-
Object
- Object
- Mooset::Storage::Memory
- Defined in:
- lib/mooset/storage/memory.rb
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Instance Method Summary collapse
- #exists?(id) ⇒ Boolean
- #fetch(id, &block) ⇒ Object
- #get(id) ⇒ Object
-
#initialize(name) ⇒ Memory
constructor
A new instance of Memory.
- #set(id, object) ⇒ Object
Constructor Details
#initialize(name) ⇒ Memory
Returns a new instance of Memory.
6 7 8 9 |
# File 'lib/mooset/storage/memory.rb', line 6 def initialize(name) @name = name @objects = {} end |
Instance Attribute Details
#name ⇒ Object (readonly)
Returns the value of attribute name.
4 5 6 |
# File 'lib/mooset/storage/memory.rb', line 4 def name @name end |
Instance Method Details
#exists?(id) ⇒ Boolean
15 16 17 |
# File 'lib/mooset/storage/memory.rb', line 15 def exists?(id) !@objects[id].nil? end |
#fetch(id, &block) ⇒ Object
23 24 25 26 27 28 29 30 31 |
# File 'lib/mooset/storage/memory.rb', line 23 def fetch(id, &block) if exists?(id) get(id) else object = block.call set(id, object) object end end |
#get(id) ⇒ Object
19 20 21 |
# File 'lib/mooset/storage/memory.rb', line 19 def get(id) @objects[id] end |
#set(id, object) ⇒ Object
11 12 13 |
# File 'lib/mooset/storage/memory.rb', line 11 def set(id, object) @objects[id] = object end |