Class: EXEL::DeferredContextValue
- Inherits:
-
Object
- Object
- EXEL::DeferredContextValue
- 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
-
#keys ⇒ Object
readonly
Returns the value of attribute keys.
Class Method Summary collapse
-
.resolve(value, context) ⇒ Object
If
value
is an instance ofDeferredContextValue
, it will be resolved to its actual value in the context.
Instance Method Summary collapse
-
#[](key) ⇒ Object
Records the keys that will be used to lookup the value from the context at runtime.
-
#get(context) ⇒ Object
Given a context, returns the value that this instance was acting as a placeholder for.
-
#initialize ⇒ DeferredContextValue
constructor
A new instance of DeferredContextValue.
Constructor Details
#initialize ⇒ DeferredContextValue
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
#keys ⇒ Object (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.
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 |