Class: Mooset::Storage::Memory

Inherits:
Object
  • Object
show all
Defined in:
lib/mooset/storage/memory.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#nameObject (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

Returns:

  • (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