Class: Showoff::State
- Inherits:
-
Object
- Object
- Showoff::State
- Defined in:
- lib/showoff/state.rb
Overview
Just a very simple global key-value data store.
Constant Summary collapse
- @@state =
{}
Class Method Summary collapse
- .append(key, value) ⇒ Object
-
.appendAtIndex(key, pos, value) ⇒ Object
Append to an array saved at a certain position of an array at a certain key.
- .dump ⇒ Object
-
.get(key) ⇒ Object
@returns The value of that key.
-
.getAtIndex(key, pos) ⇒ Object
Return an indexed value from an array saved at a certain key.
-
.increment(key) ⇒ Integer
The new value of the counter.
- .keys ⇒ Object
- .reset(*keys) ⇒ Object
- .set(key, value) ⇒ Object
-
.setAtIndex(key, pos, value) ⇒ Object
Set an indexed value from an array saved at a certain key.
Instance Method Summary collapse
-
#include?(key) ⇒ Boolean
@returns Whether that key exists.
Class Method Details
.append(key, value) ⇒ Object
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.
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 |
.dump ⇒ Object
11 12 13 |
# File 'lib/showoff/state.rb', line 11 def self.dump @@state end |
.get(key) ⇒ Object
@returns The value of that key.
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.
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.
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 |
.keys ⇒ Object
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
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.
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.
17 18 19 |
# File 'lib/showoff/state.rb', line 17 def include?(key) @@state.include?(key) end |