Class: Elephas::Providers::Hash

Inherits:
Object
  • Object
show all
Includes:
Base
Defined in:
lib/elephas/providers/hash.rb

Overview

This is a simple providers, which uses an hash for storing the values.

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Methods included from Base

#now

Constructor Details

- (Hash) initialize(data = nil)

Initialize the provider

Parameters:

  • data (Hash) (defaults to: nil)

    The initial data stored.



19
20
21
# File 'lib/elephas/providers/hash.rb', line 19

def initialize(data = nil)
  @data = data && data.is_a?(::Hash) ? data : {}
end

Instance Attribute Details

- (Hash) data

The internal hash used by the provider.

Returns:

  • (Hash)

    The internal hash used by the provider.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/elephas/providers/hash.rb', line 13

class Hash
  include Elephas::Providers::Base
  attr_accessor :data

  # Initialize the provider
  # @param data [Hash] The initial data stored.
  def initialize(data = nil)
    @data = data && data.is_a?(::Hash) ? data : {}
  end

  # Reads a value from the cache.
  #
  # @param key [String] The key to lookup.
  # @return [Entry|NilClass] The read value or `nil`.
  def read(key)
    self.exists?(key) ? @data[key.ensure_string] : nil
  end

  # Writes a value to the cache.
  #
  # @param key [String] The key to associate the value with.
  # @param value [Object] The value to write. Setting a value to `nil` **doesn't** mean *deleting* the value.
  # @param options [Hash] A list of options for writing.
  # @see Elephas::Cache.setup_options
  # @return [Object] The value itself.
  def write(key, value, options = {})
    entry = ::Elephas::Entry.ensure(value, key, options)
    entry.refresh
    @data[key.ensure_string] = entry
    entry
  end

  # Deletes a value from the cache.
  #
  # @param key [String] The key to delete.
  # @return [Boolean] `true` if the key was in the cache, `false` otherwise.
  def delete(key)
    key = key.ensure_string
    rv = @data.has_key?(key)
    @data.delete(key)
    rv
  end

  # Checks if a key exists in the cache.
  #
  # @param key [String] The key to lookup.
  # @return [Boolean] `true` if the key is in the cache, `false` otherwise.
  def exists?(key)
    key = key.ensure_string
    @data.has_key?(key) && @data[key].valid?
  end
end

Instance Method Details

- (Boolean) delete(key)

Deletes a value from the cache.

Parameters:

  • key (String)

    The key to delete.

Returns:

  • (Boolean)

    true if the key was in the cache, false otherwise.



49
50
51
52
53
54
# File 'lib/elephas/providers/hash.rb', line 49

def delete(key)
  key = key.ensure_string
  rv = @data.has_key?(key)
  @data.delete(key)
  rv
end

- (Boolean) exists?(key)

Checks if a key exists in the cache.

Parameters:

  • key (String)

    The key to lookup.

Returns:

  • (Boolean)

    true if the key is in the cache, false otherwise.



60
61
62
63
# File 'lib/elephas/providers/hash.rb', line 60

def exists?(key)
  key = key.ensure_string
  @data.has_key?(key) && @data[key].valid?
end

- (Entry|NilClass) read(key)

Reads a value from the cache.

Parameters:

  • key (String)

    The key to lookup.

Returns:

  • (Entry|NilClass)

    The read value or nil.



27
28
29
# File 'lib/elephas/providers/hash.rb', line 27

def read(key)
  self.exists?(key) ? @data[key.ensure_string] : nil
end

- (Object) write(key, value, options = {})

Writes a value to the cache.

Parameters:

  • key (String)

    The key to associate the value with.

  • value (Object)

    The value to write. Setting a value to nil doesn't mean deleting the value.

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

    A list of options for writing.

Returns:

  • (Object)

    The value itself.

See Also:



38
39
40
41
42
43
# File 'lib/elephas/providers/hash.rb', line 38

def write(key, value, options = {})
  entry = ::Elephas::Entry.ensure(value, key, options)
  entry.refresh
  @data[key.ensure_string] = entry
  entry
end