Class: Ruboty::ToggleSwitch::Storage

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/ruboty/toggle_switch/storage.rb

Defined Under Namespace

Classes: Switch

Constant Summary collapse

NAMESPACE =
'ruboty-toggle_switch-storage'

Instance Method Summary collapse

Constructor Details

#initialize(brain) ⇒ Storage

Returns a new instance of Storage

Parameters:

  • brain (Ruboty::Brain)


13
14
15
# File 'lib/ruboty/toggle_switch/storage.rb', line 13

def initialize(brain)
  @brain = brain
end

Instance Method Details

#[](key) ⇒ Switch?

Returns the switch to which the specified key is mapped.

Returns:

  • (Switch, nil)

    the switch or nil



20
21
22
# File 'lib/ruboty/toggle_switch/storage.rb', line 20

def [](key)
  switches[key]
end

#[]=(key, value) ⇒ Object

Update the switch which is associated with the specified key.

Parameters:

  • key (String)

    the key for the switch.

  • value (Hash)

    the value to update the switch.

Options Hash (value):

  • :state (String)

    “on” or “off”.

  • :from (String)

    a name of the message sender.



30
31
32
# File 'lib/ruboty/toggle_switch/storage.rb', line 30

def []=(key, value)
  switches[key] = Switch.new(value[:state], value[:from], Time.now, value[:note])
end

#eachObject

Yields each pair of the key and switch.



49
50
51
52
53
# File 'lib/ruboty/toggle_switch/storage.rb', line 49

def each
  switches.each do |entry|
    yield(*entry)
  end
end

#off?(key) ⇒ Boolean

Returns true if the switch is ‘off’.

Returns:

  • (Boolean)

    true if the state of the switch is “off”



44
45
46
# File 'lib/ruboty/toggle_switch/storage.rb', line 44

def off?(key)
  state_for(key) == 'off'
end

#on?(key) ⇒ Boolean

Returns true if the switch is ‘on’.

Returns:

  • (Boolean)

    true if the state of the switch is “on”



37
38
39
# File 'lib/ruboty/toggle_switch/storage.rb', line 37

def on?(key)
  state_for(key) == 'on'
end