Class: Datacaster::MessageKeysMerger

Inherits:
Base
  • Object
show all
Defined in:
lib/datacaster/message_keys_merger.rb

Instance Method Summary collapse

Methods included from Mixin

#&, #*, #call, #call_with_runtime, #cast_errors, #i18n_key, #i18n_map_keys, #i18n_scope, #i18n_vars, #inspect, #then, #with_context, #with_object_context, #with_runtime, #|

Constructor Details

#initialize(keys) ⇒ MessageKeysMerger

Returns a new instance of MessageKeysMerger.



3
4
5
# File 'lib/datacaster/message_keys_merger.rb', line 3

def initialize(keys)
  @keys = keys
end

Instance Method Details

#cast(object, runtime:) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/datacaster/message_keys_merger.rb', line 7

def cast(object, runtime:)
  return Datacaster.ErrorResult(I18nValues::Key.new(['.hash_value', 'datacaster.errors.hash_value'], value: object)) unless object.is_a?(Hash)

  result = set_initial_value(object)

  @keys.each do |k|
    result =
      if need_hash_merger?(object)
        merge_hash(result, object[k])
      else
        merge_array_or_scalar(result, object[k])
      end
  end

  result = clean(result)

  Datacaster.ValidResult(
    result.nil? ? Datacaster.absent : result
  )
end