class Showoff::State
Just a very simple global key-value data store.
Public Class Methods
@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
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
@returns [Hash] Hash
dump of all data in the datastore
# File lib/showoff/state.rb, line 11 def self.dump @@state end
@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
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
@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
@returns [Array] Array of keys in the datastore
# File lib/showoff/state.rb, line 6 def self.keys @@state.keys end
# File lib/showoff/state.rb, line 82 def self.reset(*keys) if keys.empty? @@state = {} else keys.each { |key| @@state.delete(key) } end end
@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
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
@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