Class: Garner::Cache::Identity

Inherits:
Object
  • Object
show all
Defined in:
lib/garner/cache/identity.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeIdentity

Returns a new instance of Identity.



6
7
8
9
10
11
12
13
# File 'lib/garner/cache/identity.rb', line 6

def initialize
  @bindings = []
  @key_hash = {}

  # Set up options hash with defaults
  @options_hash = Garner.config.global_cache_options || {}
  @options_hash.merge!({ :expires_in => Garner.config.expires_in })
end

Instance Attribute Details

#bindingsObject

Returns the value of attribute bindings.



4
5
6
# File 'lib/garner/cache/identity.rb', line 4

def bindings
  @bindings
end

#key_hashObject

Returns the value of attribute key_hash.



4
5
6
# File 'lib/garner/cache/identity.rb', line 4

def key_hash
  @key_hash
end

#options_hashObject

Returns the value of attribute options_hash.



4
5
6
# File 'lib/garner/cache/identity.rb', line 4

def options_hash
  @options_hash
end

Instance Method Details

#bind(object, &block) ⇒ Object

Bind this cache identity to a (bindable) object.

Parameters:

  • object (Object)

    An object; should support configured binding strategy.



22
23
24
25
# File 'lib/garner/cache/identity.rb', line 22

def bind(object, &block)
  @bindings << object
  block_given? ? fetch(&block) : self
end

#fetch(&block) ⇒ Object



15
16
17
# File 'lib/garner/cache/identity.rb', line 15

def fetch(&block)
  Garner::Cache.fetch(@bindings, @key_hash, @options_hash, &block)
end

#key(hash, &block) ⇒ Object

Merge the given hash into the cache identity’s key hash.

Parameters:

  • hash (Hash)

    A hash to merge on top of the current key hash.



30
31
32
33
# File 'lib/garner/cache/identity.rb', line 30

def key(hash, &block)
  @key_hash.merge!(hash)
  block_given? ? fetch(&block) : self
end

#options(hash, &block) ⇒ Object

Merge the given hash into the cache identity’s cache options. Any cache_options supported by Garner.config.cache may be passed.

Parameters:

  • hash (Hash)

    Options to pass to Garner.config.cache.



39
40
41
42
# File 'lib/garner/cache/identity.rb', line 39

def options(hash, &block)
  @options_hash.merge!(hash)
  block_given? ? fetch(&block) : self
end