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.



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

def initialize
  @keys = []
end

Instance Attribute Details

#keysObject (readonly)

Returns the value of attribute keys.



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

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



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

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]


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

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

#get(context) ⇒ Object

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



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

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