Class: Garner::Cache::Identity
- Inherits:
-
Object
- Object
- Garner::Cache::Identity
- Defined in:
- lib/garner/cache/identity.rb
Instance Attribute Summary collapse
-
#bindings ⇒ Object
Returns the value of attribute bindings.
-
#key_hash ⇒ Object
Returns the value of attribute key_hash.
-
#options_hash ⇒ Object
Returns the value of attribute options_hash.
-
#ruby_context ⇒ Object
Returns the value of attribute ruby_context.
Instance Method Summary collapse
-
#bind(object, &block) ⇒ Object
Bind this cache identity to a (bindable) object.
- #fetch(&block) ⇒ Object
-
#initialize(ruby_context = nil) ⇒ Identity
constructor
A new instance of Identity.
-
#key(hash, &block) ⇒ Object
Merge the given hash into the cache identity’s key hash.
-
#nocache ⇒ Object
Disable caching for this identity.
-
#options(hash, &block) ⇒ Object
Merge the given hash into the cache identity’s cache options.
Constructor Details
#initialize(ruby_context = nil) ⇒ Identity
Returns a new instance of Identity.
7 8 9 10 11 12 13 14 15 16 |
# File 'lib/garner/cache/identity.rb', line 7 def initialize(ruby_context = nil) @ruby_context = ruby_context @bindings = [] @key_hash = {} # Set up options hash with defaults = Garner.config..dup || {} [:expires_in] ||= Garner.config.expires_in end |
Instance Attribute Details
#bindings ⇒ Object
Returns the value of attribute bindings.
5 6 7 |
# File 'lib/garner/cache/identity.rb', line 5 def bindings @bindings end |
#key_hash ⇒ Object
Returns the value of attribute key_hash.
5 6 7 |
# File 'lib/garner/cache/identity.rb', line 5 def key_hash @key_hash end |
#options_hash ⇒ Object
Returns the value of attribute options_hash.
5 6 7 |
# File 'lib/garner/cache/identity.rb', line 5 def end |
#ruby_context ⇒ Object
Returns the value of attribute ruby_context.
4 5 6 |
# File 'lib/garner/cache/identity.rb', line 4 def ruby_context @ruby_context end |
Instance Method Details
#bind(object, &block) ⇒ Object
Bind this cache identity to a (bindable) object.
35 36 37 38 |
# File 'lib/garner/cache/identity.rb', line 35 def bind(object, &block) @bindings << object block_given? ? fetch(&block) : self end |
#fetch(&block) ⇒ Object
18 19 20 21 22 23 24 |
# File 'lib/garner/cache/identity.rb', line 18 def fetch(&block) if @nocache yield else Garner::Cache.fetch(@bindings, @key_hash, , &block) end end |
#key(hash, &block) ⇒ Object
Merge the given hash into the cache identity’s key hash.
43 44 45 46 |
# File 'lib/garner/cache/identity.rb', line 43 def key(hash, &block) @key_hash.merge!(hash) block_given? ? fetch(&block) : self end |
#nocache ⇒ Object
Disable caching for this identity.
27 28 29 30 |
# File 'lib/garner/cache/identity.rb', line 27 def nocache @nocache = true 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.
52 53 54 55 |
# File 'lib/garner/cache/identity.rb', line 52 def (hash, &block) .merge!(hash) block_given? ? fetch(&block) : self end |