Class: Trailblazer::Context
- Inherits:
-
Object
- Object
- Trailblazer::Context
- Defined in:
- lib/trailblazer/context.rb,
lib/trailblazer/context/version.rb,
lib/trailblazer/context/aliasing.rb,
lib/trailblazer/context/indifferent_access.rb
Overview
only public creator: Build :data object:
Direct Known Subclasses
Defined Under Namespace
Modules: Aliasing Classes: ContainerChain, IndifferentAccess
Constant Summary collapse
- VERSION =
"0.3.0"
Class Method Summary collapse
- .build(wrapped_options) ⇒ Object
-
.for(wrapped_options, ctx, flow_options, **circuit_options) ⇒ Object
NOTE: In the future, we might look up the Context to use in the ctx.
- .for_circuit(wrapped_options, mutable_options, ctx, flow_options, **circuit_options) ⇒ Object
-
.implementation ⇒ Object
I hate globals, but currently this is the only easy way for setting the implementation.
Instance Method Summary collapse
- #[](name) ⇒ Object
- #[]=(name, value) ⇒ Object
-
#decompose ⇒ Object
Return the Context’s two components.
-
#initialize(wrapped_options, mutable_options) ⇒ Context
constructor
A new instance of Context.
-
#key?(name) ⇒ Boolean
TODO: use ContainerChain.find here for a generic optimization.
- #keys ⇒ Object
- #merge(hash) ⇒ Object
-
#to_hash ⇒ Object
TODO: maybe we shouldn’t allow to_hash from context? TODO: massive performance bottleneck.
Constructor Details
#initialize(wrapped_options, mutable_options) ⇒ Context
Returns a new instance of Context.
40 41 42 43 44 45 |
# File 'lib/trailblazer/context.rb', line 40 def initialize(, , *) = = # TODO: wrapped_options should be optimized for lookups here since # it could also be a Context instance, but should be a ContainerChain. end |
Class Method Details
.build(wrapped_options) ⇒ Object
31 32 33 |
# File 'lib/trailblazer/context.rb', line 31 def self.build(, *) new() end |
.for(wrapped_options, ctx, flow_options, **circuit_options) ⇒ Object
NOTE: In the future, we might look up the Context to use in the ctx.
The demanding signature is for forward-compat.
20 21 22 |
# File 'lib/trailblazer/context.rb', line 20 def self.for(, (ctx, ), **) # TODO: remove implementation.build(, {}, [ctx, ], ) end |
.for_circuit(wrapped_options, mutable_options, ctx, flow_options, **circuit_options) ⇒ Object
24 25 26 27 28 |
# File 'lib/trailblazer/context.rb', line 24 def self.for_circuit(, , (ctx, ), **) context_class = [:context_class] || implementation # Context::IndifferentAccess context_class.build(, , [ctx, ], ) end |
.implementation ⇒ Object
I hate globals, but currently this is the only easy way for setting the implementation.
36 37 38 |
# File 'lib/trailblazer/context.rb', line 36 def self.implementation IndifferentAccess end |
Instance Method Details
#[](name) ⇒ Object
47 48 49 50 51 52 53 54 |
# File 'lib/trailblazer/context.rb', line 47 def [](name) # ContainerChain.find( [@mutable_options, @wrapped_options], name ) # in 99.9% or cases @mutable_options will be a Hash, and these are already optimized for lookups. # it's up to the ContainerChain to optimize itself. return [name] if .key?(name) [name] end |
#[]=(name, value) ⇒ Object
64 65 66 |
# File 'lib/trailblazer/context.rb', line 64 def []=(name, value) [name] = value end |
#decompose ⇒ Object
Return the Context’s two components. Used when computing the new output for the next activity.
77 78 79 |
# File 'lib/trailblazer/context.rb', line 77 def decompose [, ] end |
#key?(name) ⇒ Boolean
TODO: use ContainerChain.find here for a generic optimization
the version here is about 4x faster for now.
59 60 61 62 |
# File 'lib/trailblazer/context.rb', line 59 def key?(name) # ContainerChain.find( [@mutable_options, @wrapped_options], name ) .key?(name) || .key?(name) end |
#keys ⇒ Object
81 82 83 |
# File 'lib/trailblazer/context.rb', line 81 def keys .keys + .keys # FIXME. end |
#merge(hash) ⇒ Object
69 70 71 72 73 |
# File 'lib/trailblazer/context.rb', line 69 def merge(hash) original, = decompose self.class.new(original, .merge(hash)) end |
#to_hash ⇒ Object
TODO: maybe we shouldn’t allow to_hash from context? TODO: massive performance bottleneck. also, we could already “know” here what keys the transformation wants. FIXME: ToKeywordArguments()
89 90 91 92 93 94 95 96 |
# File 'lib/trailblazer/context.rb', line 89 def to_hash {}.tap do |hash| # the "key" here is to call to_hash on all containers. [.to_hash, .to_hash].each do || .each { |k, v| hash[k.to_sym] = v } end end end |