Class: Shallow::Caller::Base
- Inherits:
-
Object
- Object
- Shallow::Caller::Base
- Defined in:
- lib/shallow.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#cache_key ⇒ Object
readonly
Returns the value of attribute cache_key.
-
#captured_method ⇒ Object
readonly
Returns the value of attribute captured_method.
Instance Method Summary collapse
- #call(&context) ⇒ Object
-
#initialize(options = {}) ⇒ Base
constructor
A new instance of Base.
Constructor Details
#initialize(options = {}) ⇒ Base
Returns a new instance of Base.
61 62 63 64 65 66 |
# File 'lib/shallow.rb', line 61 def initialize( = {}) keys = [:keys] @args = [:args] @cache_key = File.join(keys.inject([CACHE_FOLDER]){|m,v| m << v.to_s}) @captured_method = [:capture] end |
Instance Attribute Details
#cache_key ⇒ Object (readonly)
Returns the value of attribute cache_key.
60 61 62 |
# File 'lib/shallow.rb', line 60 def cache_key @cache_key end |
#captured_method ⇒ Object (readonly)
Returns the value of attribute captured_method.
60 61 62 |
# File 'lib/shallow.rb', line 60 def captured_method @captured_method end |
Instance Method Details
#call(&context) ⇒ Object
68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/shallow.rb', line 68 def call(&context) if cache.exist?(cache_key) returning_value = YAML.load(cache.read(cache_key)) context.call(*returning_value, &context) if block_given? && !returning_value.nil? return *returning_value else returning_value = nil cache.fetch(cache_key) { returning_value = captured_method.call(*@args, &lambda {|args| context.call(args); args }) returning_value ? [returning_value].to_yaml : nil.to_yaml } return *returning_value end end |