Class: Trailblazer::Context::Container::WithAliases

Inherits:
Trailblazer::Context::Container show all
Defined in:
lib/trailblazer/context/container/with_aliases.rb

Overview

Extension to replace Context::Container writers with aliased writers. It'll mutate the well known `@mutable_options` with only original keys and `@replica` with both orignal and aliased keys

Instance Method Summary collapse

Methods inherited from Trailblazer::Context::Container

#decompose

Methods included from CommonMethods

#[], #each, #fetch, #key?, #keys, #to_hash, #value?, #values

Constructor Details

#initialize(wrapped_options, mutable_options, aliases:, replica_class:) ⇒ WithAliases

Returns a new instance of WithAliases.


8
9
10
11
12
13
14
15
16
17
# File 'lib/trailblazer/context/container/with_aliases.rb', line 8

def initialize(wrapped_options, mutable_options, aliases:, replica_class:, **)
  @wrapped_options  = wrapped_options
  @mutable_options  = mutable_options

  # { "contract.default" => :contract, "result.default" => :result }
  @aliases          = aliases

  @replica_class    = replica_class
  @replica          = initialize_replica_store
end

Instance Method Details

#alias_mapping_for(key) ⇒ Object

Returns key and it's mapped alias. `key` could be an alias too.

aliases => { “contract.default” => :contract, “result.default”=>:result } key, _alias = alias_mapping_for(:contract) key, _alias = alias_mapping_for(“contract.default”)


60
61
62
63
64
65
66
67
68
69
# File 'lib/trailblazer/context/container/with_aliases.rb', line 60

def alias_mapping_for(key)
  # when key has an alias
  return [ key, @aliases[key] ] if @aliases.key?(key)

  # when key is an alias
  return [ @aliases.key(key), key ] if @aliases.value?(key)

  # when there is no alias
  return [ key, nil ]
end

#aliased_delete(key) ⇒ Object Also known as: delete


34
35
36
37
38
39
40
# File 'lib/trailblazer/context/container/with_aliases.rb', line 34

def aliased_delete(key)
  _key, _alias = alias_mapping_for(key)

  @mutable_options.delete(_key)
  @replica.delete(_key)
  @replica.delete(_alias) if _alias
end

#aliased_merge(other_hash) ⇒ Object Also known as: merge


44
45
46
47
48
49
50
# File 'lib/trailblazer/context/container/with_aliases.rb', line 44

def aliased_merge(other_hash)
  # other_hash could have aliases and we don't want to store them in @mutable_options.
  _other_hash = replace_aliases_with_original_keys(other_hash)

  options = { aliases: @aliases, replica_class: @replica_class }
  self.class.new(@wrapped_options, @mutable_options.merge(_other_hash), **options)
end

#aliased_writer(key, value) ⇒ Object Also known as: []=


24
25
26
27
28
29
30
# File 'lib/trailblazer/context/container/with_aliases.rb', line 24

def aliased_writer(key, value)
  _key, _alias = alias_mapping_for(key)

  @mutable_options[_key] = value
  @replica[_key]         = value
  @replica[_alias]       = value if _alias
end

#inspectObject


19
20
21
# File 'lib/trailblazer/context/container/with_aliases.rb', line 19

def inspect
  %{#<Trailblazer::Context::Container::WithAliases wrapped_options=#{@wrapped_options} mutable_options=#{@mutable_options} aliases=#{@aliases}>}
end