Class: Switches::Backends::Memory

Inherits:
Object
  • Object
show all
Defined in:
lib/switches/backends/memory.rb,
lib/switches/backends/memory/bus.rb

Defined Under Namespace

Classes: Bus

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(uri, instance) ⇒ Memory

Returns a new instance of Memory.



19
20
21
# File 'lib/switches/backends/memory.rb', line 19

def initialize(uri, instance)
  @instance = instance
end

Class Method Details

.busObject



6
7
8
# File 'lib/switches/backends/memory.rb', line 6

def self.bus
  @bus ||= Bus.new
end

.clearObject



14
15
16
17
# File 'lib/switches/backends/memory.rb', line 14

def self.clear
  bus.clear
  storage.clear
end

.storageObject



10
11
12
# File 'lib/switches/backends/memory.rb', line 10

def self.storage
  @storage ||= {}
end

Instance Method Details

#clearObject



44
45
46
# File 'lib/switches/backends/memory.rb', line 44

def clear
  self.class.clear
end

#get(item) ⇒ Object



27
28
29
30
31
# File 'lib/switches/backends/memory.rb', line 27

def get(item)
  if json = self.class.storage[item.key]
    JSONSerializer.deserialize(json)
  end
end

#listenObject



33
34
35
36
37
38
# File 'lib/switches/backends/memory.rb', line 33

def listen
  self.class.bus.subscribe do |message|
    update = Update.load(message)
    @instance.notified(update)
  end
end

#notify(update) ⇒ Object



40
41
42
# File 'lib/switches/backends/memory.rb', line 40

def notify(update)
  self.class.bus.publish(update.to_json)
end

#set(item) ⇒ Object



23
24
25
# File 'lib/switches/backends/memory.rb', line 23

def set(item)
  self.class.storage[item.key] = item.to_json
end

#stopObject



48
49
# File 'lib/switches/backends/memory.rb', line 48

def stop
end