Class: EXEL::DeferredContextValue

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

Overview

When context is referenced in a job definition, an instance of DeferredContextValue will be put in its place. At runtime, the first time a DeferredContextValue is read via Context#[], it will be replaced by the value it was referring to.

Example:

process with: MyProcessor, foo: context[:bar]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeDeferredContextValue

Returns a new instance of DeferredContextValue.



38
39
40
# File 'lib/exel/deferred_context_value.rb', line 38

def initialize
  @keys = []
end

Instance Attribute Details

#keysObject (readonly)

Returns the value of attribute keys.



11
12
13
# File 'lib/exel/deferred_context_value.rb', line 11

def keys
  @keys
end

Class Method Details

.resolve(value, context) ⇒ Object

If value is an instance of DeferredContextValue, it will be resolved to its actual value in the context. If it is an Array or Hash all DeferredContextValue instances within it will be resolved. If it is anything else, it will just be returned.

Returns:

  • value, with all DeferredContextValue instances resolved



19
20
21
22
23
24
25
26
27
28
29
# File 'lib/exel/deferred_context_value.rb', line 19

def resolve(value, context)
  if deferred?(value)
    value = value.get(context)
  elsif value.is_a?(Array)
    value.map! { |v| resolve(v, context) }
  elsif value.is_a?(Hash)
    value.each { |k, v| value[k] = resolve(v, context) }
  end

  value
end

Instance Method Details

#[](key) ⇒ Object

Records the keys that will be used to lookup the value from the context at runtime. Supports nested hashes such as:

context[:hash1][:hash2][:key]


45
46
47
48
# File 'lib/exel/deferred_context_value.rb', line 45

def [](key)
  keys << key
  self
end

#get(context) ⇒ Object

Given a context, returns the value that this instance was acting as a placeholder for.



51
52
53
# File 'lib/exel/deferred_context_value.rb', line 51

def get(context)
  keys.reduce(context) { |acc, elem| acc[elem] }
end