class Showoff::State

Just a very simple global key-value data store.

Public Class Methods

append(key, value) click to toggle source

@param key [String] The key of the array to manage. @param value [Any] The value to append to the array at that key.

# File lib/showoff/state.rb, line 45
def self.append(key, value)
  @@state[key] ||= []
  @@state[key] << value
end
appendAtIndex(key, pos, value) click to toggle source

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

@param key [String] The key of the top level array to manage. @param pos [Integer] The index where the array to append to exists. @param [Any] The value to append to the array at that key.

# File lib/showoff/state.rb, line 75
def self.appendAtIndex(key, pos, value)
  @@state[key]      ||= []
  @@state[key][pos] ||= []
  @@state[key][pos]  << value
end
dump() click to toggle source

@returns [Hash] Hash dump of all data in the datastore

# File lib/showoff/state.rb, line 11
def self.dump
  @@state
end
get(key) click to toggle source

@param key [String] The key to look for. @returns The value of that key.

# File lib/showoff/state.rb, line 23
def self.get(key)
  @@state[key]
end
getAtIndex(key, pos) click to toggle source

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

@param key [String] The key of the array to manage. @param pos [Integer] The position to retrieve. @param [Any] The value to set for that key.

# File lib/showoff/state.rb, line 55
def self.getAtIndex(key, pos)
  @@state[key] ||= []
  @@state[key][pos]
end
increment(key) click to toggle source

@param key [String] The key to increment. @note The value stored must be an Integer. This will initialize at zero if needed. @return [Integer] The new value of the counter.

# 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
keys() click to toggle source

@returns [Array] Array of keys in the datastore

# File lib/showoff/state.rb, line 6
def self.keys
  @@state.keys
end
reset(*keys) click to toggle source
# 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) click to toggle source

@param key [String] The key to set. @param [Any] The value to set for that key.

# File lib/showoff/state.rb, line 29
def self.set(key, value)
  @@state[key] = value
end
setAtIndex(key, pos, value) click to toggle source

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

@param key [String] The key of the array to manage. @param pos [Integer] The position to set at. @param [Any] The value to set for that key.

# File lib/showoff/state.rb, line 65
def self.setAtIndex(key, pos, value)
  @@state[key] ||= []
  @@state[key][pos] = value
end

Public Instance Methods

include?(key) click to toggle source

@param key [String] The key to look for. @returns Whether that key exists.

# File lib/showoff/state.rb, line 17
def include?(key)
  @@state.include?(key)
end