Class: Arstotzka::Fetcher::Cache Private

Inherits:
Object
  • Object
show all
Includes:
Base
Defined in:
lib/arstotzka/fetcher/cache.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Class responsible for reading instance variables when cached option is given.

When the “cache” is not accepted, block will be called to determinate the value to be returned also caching it in an instance variable

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Base

#options=

Constructor Details

#initialize(options, &block) ⇒ Cache #initialize(options_hash, &block) ⇒ Cache

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of Cache.

Overloads:

  • #initialize(options, &block) ⇒ Cache

    Parameters:

    • options (Arstotzka::Options)

      options passed as options object

    • block (Proc)

      block to be executed in case variable is not cached

  • #initialize(options_hash, &block) ⇒ Cache

    Parameters:

    • options_hash (Hash)

      opttions passed as hash

    • block (Proc)

      block to be executed in case variable is not cached



26
27
28
29
# File 'lib/arstotzka/fetcher/cache.rb', line 26

def initialize(options_hash = {}, &block)
  self.options = options_hash
  @block = block
end

Instance Attribute Details

#blockObject (readonly, private)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



105
106
107
# File 'lib/arstotzka/fetcher/cache.rb', line 105

def block
  @block
end

#optionsObject (readonly, private)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



105
106
107
# File 'lib/arstotzka/fetcher/cache.rb', line 105

def options
  @options
end

Instance Method Details

#fetch { ... } ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Fetches value from instance or block

When cached option is given, fetches value from cache, and in case of failure, retrieves value from block given in the initialization

Examples:

Without cache option

settings = { min: 1, max: 100 }
instance = Object.new

options = Arstotzka::Options.new(
  key: :x, instance: instance
)

cache = Arstotzka::Fetcher::Cache.new do
  (settings[:min]..settings[:max]).sum
end

cache.fetch # returns 5050
settings[:max] = 10
cache.fetch # returns 555

With simple cache option

settings = { calculate: false, min: 1, max: 100 }
instance = Object.new

options = Arstotzka::Options.new(
  key: :x, instance: instance, cached: true
)

cache = Arstotzka::Fetcher::Cache.new do
  if settings[:calculate]
    (settings[:min]..settings[:max]).sum
  end
end

cache.fetch # returns nil (which is not considered cache)
settings[:calculate] = true
cache.fetch # returns 5050
settings[:max] = 10
cache.fetch # returns 5050 from cache

With full cache option

settings = { calculate: false, min: 1, max: 100 }
instance = Object.new

options = Arstotzka::Options.new(
  key: :x, instance: instance, cached: :full
)

cache = Arstotzka::Fetcher::Cache.new do
  if settings[:calculate]
    (settings[:min]..settings[:max]).sum
  end
end

cache.fetch # returns nil
settings[:calculate] = true
cache.fetch # returns nil from cache

Yields:

  • runs the block given in the initialization when the value was not found in the cache

Returns:

  • (Object)


95
96
97
98
99
100
101
# File 'lib/arstotzka/fetcher/cache.rb', line 95

def fetch
  if cached
    fetch_with_cache
  else
    block.call
  end
end