Class: MiniCache::Store
- Inherits:
-
Object
- Object
- MiniCache::Store
- Includes:
- Enumerable
- Defined in:
- lib/mini_cache/store.rb
Instance Attribute Summary collapse
-
#data ⇒ Object
readonly
Public: Returns the hash of key-value pairs.
Instance Method Summary collapse
-
#each(&block) ⇒ Object
Public: Iterates over all key-value pairs.
-
#get(key) ⇒ Object
Public: Retrieves the value for a given key.
-
#get_or_set(key, value = nil) ⇒ Object
Public: Retrieves the value for a given key if it has already been set; otherwise, sets the value either as an argument or block.
-
#initialize(data = {}) ⇒ Store
constructor
Public: Initializes a new MiniCache object.
-
#load(data) ⇒ Object
Public: Loads a hash of data into the cache.
-
#reset ⇒ Object
Public: Clears all key-value pairs.
-
#set(key, value = nil) ⇒ Object
Public: Sets a value for a given key either as an argument or block.
-
#set?(key) ⇒ Boolean
Public: Determines whether a value has been set for a given key.
-
#unset(key) ⇒ Object
Public: Removes the key-value pair from the cache for a given key.
Constructor Details
#initialize(data = {}) ⇒ Store
Public: Initializes a new MiniCache object.
data - A Hash of key-value pairs (optional).
Returns nothing.
13 14 15 16 |
# File 'lib/mini_cache/store.rb', line 13 def initialize(data = {}) @data = {} self.load(data) end |
Instance Attribute Details
#data ⇒ Object (readonly)
Public: Returns the hash of key-value pairs.
6 7 8 |
# File 'lib/mini_cache/store.rb', line 6 def data @data end |
Instance Method Details
#each(&block) ⇒ Object
Public: Iterates over all key-value pairs.
block - A block of code that will be send the key
and value of each pair.
Yields the String key and value.
117 118 119 |
# File 'lib/mini_cache/store.rb', line 117 def each(&block) @data.each { |k, v| yield(k, v) } end |
#get(key) ⇒ Object
Public: Retrieves the value for a given key.
key - A String or Symbol representing the key.
Returns the value set for the key; if nothing is
set, returns nil.
24 25 26 27 |
# File 'lib/mini_cache/store.rb', line 24 def get(key) check_key!(key) @data[key.to_s] end |
#get_or_set(key, value = nil) ⇒ Object
Public: Retrieves the value for a given key if it has already been set; otherwise, sets the value either as an argument or block.
key - A String or Symbol representing the key. value - Any object that represents the value (optional).
Not used if a block is given.
block - A block of code that returns the value to set
(optional).
Examples
cache.set("name", "Derrick")
=> "Derrick"
cache.get_or_set("name", "Joe")
=> "Derrick"
cache.get_or_set("occupation") { "Engineer" }
=> "Engineer"
cache.get_or_set("occupation") { "Pilot" }
=> "Engineer"
Returns the value.
88 89 90 91 |
# File 'lib/mini_cache/store.rb', line 88 def get_or_set(key, value = nil) return get(key) if set?(key) set(key, block_given? ? yield : value) end |
#load(data) ⇒ Object
Public: Loads a hash of data into the cache.
data - A Hash of data with either String or Symbol keys.
Returns nothing.
126 127 128 129 130 131 |
# File 'lib/mini_cache/store.rb', line 126 def load(data) data.each do |key, value| check_key!(key) @data[key.to_s] = value end end |
#reset ⇒ Object
Public: Clears all key-value pairs.
Returns nothing.
107 108 109 |
# File 'lib/mini_cache/store.rb', line 107 def reset @data = {} end |
#set(key, value = nil) ⇒ Object
Public: Sets a value for a given key either as an argument or block.
key - A String or Symbol representing the key. value - Any object that represents the value (optional).
Not used if a block is given.
block - A block of code that returns the value to set
(optional).
Examples
cache.set("name", "Derrick")
=> "Derrick"
cache.set("name") { "Joe" }
=> "Joe"
Returns the value given.
47 48 49 50 |
# File 'lib/mini_cache/store.rb', line 47 def set(key, value = nil) check_key!(key) @data[key.to_s] = block_given? ? yield : value end |
#set?(key) ⇒ Boolean
Public: Determines whether a value has been set for a given key.
key - A String or Symbol representing the key.
Returns a Boolean.
58 59 60 61 |
# File 'lib/mini_cache/store.rb', line 58 def set?(key) check_key!(key) @data.keys.include?(key.to_s) end |
#unset(key) ⇒ Object
Public: Removes the key-value pair from the cache for a given key.
key - A String or Symbol representing the key.
Returns the value.
99 100 101 102 |
# File 'lib/mini_cache/store.rb', line 99 def unset(key) check_key!(key) @data.delete(key.to_s) end |