Class: Juno::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/juno/base.rb

Overview

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

Instance Method Summary collapse

Instance Method Details

#[](key) ⇒ Object

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

Parameters:

  • key (Object)

Returns:

  • (Object)

    value



38
39
40
# File 'lib/juno/base.rb', line 38

def [](key)
  load(key)
end

#[]=(key, value) ⇒ Object

Store value with key

Parameters:

  • key (Object)
  • value (Object)

Returns:

  • value



48
49
50
# File 'lib/juno/base.rb', line 48

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

#closeObject

Explicitly close the store



7
8
# File 'lib/juno/base.rb', line 7

def close
end

#fetch(key, default = nil, options = nil) ⇒ Object

Fetch value with key

This is a overloaded method:

  • fetch(key, options = {}, &block) retrieve a key. if the key is not available, execute the block and return its return value.

  • fetch(key, value, options = {}) retrieve a key. if the key is not available, return the value.

Parameters:

  • key (Object)
  • default (Object) (defaults to: nil)

    Default value

  • options (Hash) (defaults to: nil)

Returns:

  • (Object)

    value from store



24
25
26
27
28
29
30
31
# File 'lib/juno/base.rb', line 24

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