Class: Showoff::State

Inherits:
Object
  • Object
show all
Defined in:
lib/showoff/state.rb

Overview

Just a very simple global key-value data store.

Constant Summary collapse

@@state =
{}

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.append(key, value) ⇒ Object

Parameters:

  • key (String)

    The key of the array to manage.

  • value (Any)

    The value to append to the array at that key.



45
46
47
48
# File 'lib/showoff/state.rb', line 45

def self.append(key, value)
  @@state[key] ||= []
  @@state[key] << value
end

.appendAtIndex(key, pos, value) ⇒ Object

Append to an array saved at a certain position of an array at a certain key.

Parameters:

  • key (String)

    The key of the top level array to manage.

  • pos (Integer)

    The index where the array to append to exists.

  • The (Any)

    value to append to the array at that key.



75
76
77
78
79
# File 'lib/showoff/state.rb', line 75

def self.appendAtIndex(key, pos, value)
  @@state[key]      ||= []
  @@state[key][pos] ||= []
  @@state[key][pos]  << value
end

.dumpObject



11
12
13
# File 'lib/showoff/state.rb', line 11

def self.dump
  @@state
end

.get(key) ⇒ Object

@returns The value of that key.

Parameters:

  • key (String)

    The key to look for.



23
24
25
# File 'lib/showoff/state.rb', line 23

def self.get(key)
  @@state[key]
end

.getAtIndex(key, pos) ⇒ Object

Return an indexed value from an array saved at a certain key.

Parameters:

  • key (String)

    The key of the array to manage.

  • pos (Integer)

    The position to retrieve.

  • The (Any)

    value to set for that key.



55
56
57
58
# File 'lib/showoff/state.rb', line 55

def self.getAtIndex(key, pos)
  @@state[key] ||= []
  @@state[key][pos]
end

.increment(key) ⇒ Integer

Note:

The value stored must be an Integer. This will initialize at zero if needed.

Returns The new value of the counter.

Parameters:

  • key (String)

    The key to increment.

Returns:

  • (Integer)

    The new value of the counter.



36
37
38
39
40
41
# File 'lib/showoff/state.rb', line 36

def self.increment(key)
  # ensure that the key is initialized with an integer before incrementing.
  # Don't bother catching errors, we want those to be crashers
  @@state[key] ||= 0
  @@state[key]  += 1
end

.keysObject



6
7
8
# File 'lib/showoff/state.rb', line 6

def self.keys
  @@state.keys
end

.reset(*keys) ⇒ Object



82
83
84
85
86
87
88
# File 'lib/showoff/state.rb', line 82

def self.reset(*keys)
  if keys.empty?
    @@state = {}
  else
    keys.each { |key| @@state.delete(key) }
  end
end

.set(key, value) ⇒ Object

Parameters:

  • key (String)

    The key to set.

  • The (Any)

    value to set for that key.



29
30
31
# File 'lib/showoff/state.rb', line 29

def self.set(key, value)
  @@state[key] = value
end

.setAtIndex(key, pos, value) ⇒ Object

Set an indexed value from an array saved at a certain key.

Parameters:

  • key (String)

    The key of the array to manage.

  • pos (Integer)

    The position to set at.

  • The (Any)

    value to set for that key.



65
66
67
68
# File 'lib/showoff/state.rb', line 65

def self.setAtIndex(key, pos, value)
  @@state[key] ||= []
  @@state[key][pos] = value
end

Instance Method Details

#include?(key) ⇒ Boolean

@returns Whether that key exists.

Parameters:

  • key (String)

    The key to look for.

Returns:

  • (Boolean)


17
18
19
# File 'lib/showoff/state.rb', line 17

def include?(key)
  @@state.include?(key)
end