class Ruboty::ToggleSwitch::Storage

Constants

NAMESPACE
Switch

Represents a switch.

Public Class Methods

new(brain) click to toggle source

Returns a new instance of Storage @param [Ruboty::Brain] brain

# File lib/ruboty/toggle_switch/storage.rb, line 27
def initialize(brain)
  @brain = brain
end

Public Instance Methods

[](key) click to toggle source

Returns the switch to which the specified key is mapped.

@return [Switch, nil] the switch or nil

# File lib/ruboty/toggle_switch/storage.rb, line 34
def [](key)
  switches[key]
end
[]=(key, value) click to toggle source

Update the switch which is associated with the specified key.

@param [String] key the key for the switch. @param [Hash] value the value to update the switch. @option value [String] :state “on” or “off”. @option value [String] :from a name of the message sender.

# File lib/ruboty/toggle_switch/storage.rb, line 44
def []=(key, value)
  switches[key] = Switch.new(value[:state], value[:from], Time.now, value[:note])
end
each() { |*entry| ... } click to toggle source

Yields each pair of the key and switch.

# File lib/ruboty/toggle_switch/storage.rb, line 63
def each
  switches.each do |entry|
    yield(*entry)
  end
end
off?(key) click to toggle source

Returns true if the switch is ‘off’.

@return [Boolean] true if the state of the switch is “off”

# File lib/ruboty/toggle_switch/storage.rb, line 58
def off?(key)
  state_for(key) == 'off'
end
on?(key) click to toggle source

Returns true if the switch is ‘on’.

@return [Boolean] true if the state of the switch is “on”

# File lib/ruboty/toggle_switch/storage.rb, line 51
def on?(key)
  state_for(key) == 'on'
end

Private Instance Methods

state_for(key) click to toggle source
# File lib/ruboty/toggle_switch/storage.rb, line 71
def state_for(key)
  switches[key] && switches[key].state
end
switches() click to toggle source
# File lib/ruboty/toggle_switch/storage.rb, line 75
def switches
  @brain.data[NAMESPACE] ||= {}
end