Method: Moneta::Defaults#fetch

Defined in:
lib/moneta/mixins.rb

#fetch(key, options = {}, &block) ⇒ Object #fetch(key, default, options = {}) ⇒ Object

Fetch a value with a key

Overloads:

  • #fetch(key, options = {}, &block) ⇒ Object

    retrieve a key. if the key is not available, execute the block and return its return value.

    Parameters:

    • key (Object)
    • options (Hash) (defaults to: {})

    Options Hash (options):

    • :expires (Integer)

      Update expiration time (See Expires)

    • :raw (Boolean)

      Raw access without value transformation (See Transformer)

    • :prefix (String)

      Prefix key (See Transformer)

    Returns:

    • (Object)

      value from store

  • #fetch(key, default, options = {}) ⇒ Object

    retrieve a key. if the key is not available, return the default value.

    Parameters:

    • key (Object)
    • default (Object)

      Default value

    • options (Hash) (defaults to: {})

    Options Hash (options):

    • :expires (Integer)

      Update expiration time (See Expires)

    • :raw (Boolean)

      Raw access without value transformation (See Transformer)

    • :prefix (String)

      Prefix key (See Transformer)

    Returns:

    • (Object)

      value from store



179
180
181
182
183
184
185
186
187
188
# File 'lib/moneta/mixins.rb', line 179

def fetch(key, default = nil, options = nil)
  if block_given?
    raise ArgumentError, 'Only one argument accepted if block is given' if options
    result = load(key, default || {})
    result == nil ? yield(key) : result
  else
    result = load(key, options || {})
    result == nil ? default : result
  end
end