Class: Trailblazer::Context

Inherits:
Object
  • Object
show all
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

IndifferentAccess

Defined Under Namespace

Modules: Aliasing Classes: ContainerChain, IndifferentAccess

Constant Summary collapse

VERSION =
"0.3.0"

Class Method Summary collapse

Instance Method Summary collapse

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(wrapped_options, mutable_options, *)
  @wrapped_options = wrapped_options
  @mutable_options = mutable_options
  # 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(wrapped_options, *)
  new(wrapped_options)
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(wrapped_options, (ctx, flow_options), **circuit_options) # TODO: remove
  implementation.build(wrapped_options, {}, [ctx, flow_options], circuit_options)
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(wrapped_options, mutable_options, (ctx, flow_options), **circuit_options)
  context_class = flow_options[:context_class] || implementation # Context::IndifferentAccess

  context_class.build(wrapped_options, mutable_options, [ctx, flow_options], circuit_options)
end

.implementationObject

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 @mutable_options[name] if @mutable_options.key?(name)
  @wrapped_options[name]
end

#[]=(name, value) ⇒ Object



64
65
66
# File 'lib/trailblazer/context.rb', line 64

def []=(name, value)
  @mutable_options[name] = value
end

#decomposeObject

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
  [@wrapped_options, @mutable_options]
end

#key?(name) ⇒ Boolean

TODO: use ContainerChain.find here for a generic optimization

the version here is about 4x faster for now.

Returns:

  • (Boolean)


59
60
61
62
# File 'lib/trailblazer/context.rb', line 59

def key?(name)
  # ContainerChain.find( [@mutable_options, @wrapped_options], name )
  @mutable_options.key?(name) || @wrapped_options.key?(name)
end

#keysObject



81
82
83
# File 'lib/trailblazer/context.rb', line 81

def keys
  @mutable_options.keys + @wrapped_options.keys # FIXME.
end

#merge(hash) ⇒ Object



69
70
71
72
73
# File 'lib/trailblazer/context.rb', line 69

def merge(hash)
  original, mutable_options = decompose

  self.class.new(original, mutable_options.merge(hash))
end

#to_hashObject

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.
    [@wrapped_options.to_hash, @mutable_options.to_hash].each do |options|
      options.each { |k, v| hash[k.to_sym] = v }
    end
  end
end