Class: MemStash::Cache

Inherits:
Object show all
Includes:
Enumerable
Defined in:
lib/hoodie/stash/mem_store.rb

Overview

Basic cache object stash store (uses a Hash)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(_ = {}) ⇒ Object

Initializes a new store object.

Parameters:

  • data (Hash)

    (optional) data to load into the stash.



35
36
37
# File 'lib/hoodie/stash/mem_store.rb', line 35

def initialize(_ = {})
  @store = {}
end

Instance Attribute Details

#storeHash (readonly)

Returns of the mem stash cache hash store.

Returns:

  • (Hash)

    of the mem stash cache hash store



27
28
29
# File 'lib/hoodie/stash/mem_store.rb', line 27

def store
  @store
end

Instance Method Details

#[](key) ⇒ Hash, ...

Retrieves the value for a given key, if nothing is set, returns KeyError

Parameters:

  • key (Symbol, String)

    representing the key

Returns:

Raises:

  • (KeyError)

    if no such key found



59
60
61
# File 'lib/hoodie/stash/mem_store.rb', line 59

def [](key)
  @store[key]
end

#[]=(key, value) ⇒ Object

Store the given value with the given key, either an an argument or block. If a previous value was set it will be overwritten with the new value.

Examples:

store a value


stash.set('name') { 'Trigster' }
   => "Trigster"

stash[:cash] = 'in the hash stash cache store'
   => "in the hash stash cache store"

data = { id: 'trig', name: 'Trigster Jay', passwd: 'f00d' }
stash[:juser] = data
   => {
         :id => "trig",
       :name => "Trigster Jay",
     :passwd => "f00d"
   }

Parameters:

  • key (Symbol, String)

    string or symbol representing the key

  • value (Object)

    any object that represents the value (optional)

  • block (&block)

    that returns the value to set (optional)

Returns:

  • nothing.



89
90
91
# File 'lib/hoodie/stash/mem_store.rb', line 89

def []=(key, value)
  @store[key] = value
end

#clear!(key = nil) ⇒ Object

Clear the whole stash store or the value of a key

clear.

Parameters:

  • key (Symbol, String) (defaults to: nil)

    (optional) representing the key to

Returns:

  • nothing.



46
47
48
# File 'lib/hoodie/stash/mem_store.rb', line 46

def clear!(key = nil)
  key.nil? ? @store.clear : @store.delete(key)
end

#each { ... } ⇒ Object

Iterates over all key-value pairs.

Parameters:

  • block (&block)

    that will receive the key/value of each pair

Yields:

  • the string key and value.



99
100
101
# File 'lib/hoodie/stash/mem_store.rb', line 99

def each(&_block)
  @store.each { |k, v| yield(k, v) }
end

#include?(key) ⇒ TrueClass, FalseClass Also known as: key?

return a boolean indicating presence of the given key in the store

Parameters:

  • key (Symbol, String)

    a string or symbol representing the key

Returns:



129
130
131
# File 'lib/hoodie/stash/mem_store.rb', line 129

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

#keysArray<String, Symbol>

return all keys in the store as an array

Returns:

  • (Array<String, Symbol>)

    all the keys in store



148
149
150
# File 'lib/hoodie/stash/mem_store.rb', line 148

def keys
  @store.keys
end

#load(data) ⇒ Object

Loads a hash of data into the stash.

Parameters:

  • hash (Hash)

    of data with either String or Symbol keys.

Returns:

  • nothing.



109
110
111
112
113
# File 'lib/hoodie/stash/mem_store.rb', line 109

def load(data)
  data.each do |key, value|
    @store[key] = value
  end
end

#sizeFixnum

return the size of the store as an integer

Returns:

  • (Fixnum)


119
120
121
# File 'lib/hoodie/stash/mem_store.rb', line 119

def size
  @store.size
end

#value?(value) ⇒ TrueClass, FalseClass

return a boolean indicating presence of the given value in the store

Parameters:

  • value (String)

    a string representing the value

Returns:



140
141
142
# File 'lib/hoodie/stash/mem_store.rb', line 140

def value?(value)
  @store.value? value
end