Module: Moneta::Defaults

Overview

Simple interface to key/value stores with Hash-like interface.

Instance Method Summary collapse

Methods included from OptionSupport

#expires, #prefix, #raw, #with

Instance Method Details

#[](key) ⇒ Object

Fetch value with key. Return nil if the key doesn’t exist

Parameters:

  • key (Object)

Returns:

  • (Object)

    value



129
130
131
# File 'lib/moneta/mixins.rb', line 129

def [](key)
  load(key)
end

#[]=(key, value) ⇒ Object

Store value with key

Parameters:

  • key (Object)
  • value (Object)

Returns:

  • value



139
140
141
# File 'lib/moneta/mixins.rb', line 139

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

#closeObject

Explicitly close the store

Returns:

  • nil



93
94
# File 'lib/moneta/mixins.rb', line 93

def close
end

#decrement(key, amount = 1, options = {}) ⇒ Object

Atomically decrement integer value with key

This is just syntactic sugar for calling #increment with a negative value.

This method also accepts negative amounts.

Parameters:

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

Returns:

  • (Object)

    value from store



86
87
88
# File 'lib/moneta/mixins.rb', line 86

def decrement(key, amount = 1, options = {})
  increment(key, -amount, options)
end

#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: {})

    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: {})

    Returns:

    • (Object)

      value from store



113
114
115
116
117
118
119
120
121
122
# File 'lib/moneta/mixins.rb', line 113

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

#increment(key, amount = 1, options = {}) ⇒ Object

Note:

Not every Moneta store implements this method, a NotImplementedError is raised if it is not supported.

Atomically increment integer value with key

This method also accepts negative amounts.

Parameters:

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

Returns:

  • (Object)

    value from store

Raises:

  • (NotImplementedError)


71
72
73
# File 'lib/moneta/mixins.rb', line 71

def increment(key, amount = 1, options = {})
  raise NotImplementedError, 'increment is not supported'
end

#key?(key, options = {}) ⇒ Boolean

Exists the value with key

Parameters:

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

Returns:

  • (Boolean)


56
57
58
# File 'lib/moneta/mixins.rb', line 56

def key?(key, options = {})
  load(key, options) != nil
end