Class: Datacaster::HashMapper

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

Instance Method Summary collapse

Methods included from Mixin

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

Constructor Details

#initialize(fields) ⇒ HashMapper

Returns a new instance of HashMapper.



3
4
5
6
7
8
9
10
11
12
13
14
# File 'lib/datacaster/hash_mapper.rb', line 3

def initialize(fields)
  keys = fields.keys.flatten
  if keys.length != keys.uniq.length
    intersection = keys.select { |k| keys.count(k) > 1 }.uniq.sort
    raise ArgumentError.new("When using transform_to_hash([:a, :b, :c] => validator), " \
      "each key should not be mentioned more than once on the left-hand-side. Instead, got these " \
      "keys mentioned twice or more: #{intersection.inspect}."
    )
  end

  @fields = fields
end

Instance Method Details

#cast(object, runtime:) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/datacaster/hash_mapper.rb', line 16

def cast(object, runtime:)
  errors = {}
  result = {}

  @fields.each do |key, validator|
    new_value = runtime.ignore_checks! { validator.with_runtime(runtime).(object) }

    # transform_to_hash([:a, :b, :c] => pick(:a, :b, :c) & ...)
    if key.is_a?(Array)
      unwrapped = new_value.valid? ? new_value.value : new_value.raw_errors

      if key.length != unwrapped.length
        raise TypeError, "When using transform_to_hash([:a, :b, :c] => validator), validator should return Array "\
          "with number of elements equal to the number of elements in left-hand-side array.\n" \
          "Got the following (values or errors) instead: #{key.inspect} => #{unwrapped.inspect}."
      end
    end

    if new_value.valid?
      if key.is_a?(Array)
        key.zip(new_value.value) do |new_key, new_key_value|
          result[new_key] = new_key_value
          runtime.checked_key!(new_key)
        end
      else
        result[key] = new_value.value
        runtime.checked_key!(key)
      end
    else
      if key.is_a?(Array)
        errors = Utils.merge_errors(errors, key.zip(new_value.raw_errors).to_h)
      else
        errors = Utils.merge_errors(errors, {key => new_value.raw_errors})
      end
    end
  end

  runtime.will_check!
  result.keys.each { |key| runtime.checked_key!(key) }

  errors.delete_if { |_, v| v.empty? }

  if errors.empty?
    # All unchecked key-value pairs of initial hash are passed through, and eliminated by ContextNode
    # at the end of the chain. If we weren't dealing with the hash, then ignore that.
    result_hash =
      if object.is_a?(Hash)
        object.merge(result)
      else
        result
      end

    result_hash.keys.each { |k| result_hash.delete(k) if result_hash[k] == Datacaster.absent }
    Datacaster.ValidResult(result_hash)
  else
    Datacaster.ErrorResult(errors)
  end
end

#inspectObject



75
76
77
78
79
80
81
82
# File 'lib/datacaster/hash_mapper.rb', line 75

def inspect
  field_descriptions =
    @fields.map do |k, v|
      "#{k.inspect} => #{v.inspect}"
    end

  "#<Datacaster::HashMapper {#{field_descriptions.join(', ')}}>"
end