Class: Shallow::Caller::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/shallow.rb

Direct Known Subclasses

Clear

Instance Attribute Summary collapse

Instance Method Summary collapse

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(options = {})
  keys = options[:keys]
  @args = options[:args]
  @cache_key = File.join(keys.inject([CACHE_FOLDER]){|m,v| m << v.to_s})
  @captured_method = options[:capture]
end

Instance Attribute Details

#cache_keyObject (readonly)

Returns the value of attribute cache_key.



60
61
62
# File 'lib/shallow.rb', line 60

def cache_key
  @cache_key
end

#captured_methodObject (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