Method: BlueprinterActiveRecord::Helpers#merge_values

Defined in:
lib/blueprinter-activerecord/helpers.rb

#merge_values(value, result = {}) ⇒ Hash

Merges ‘values’, which may be any nested structure of arrays, hashes, strings, and symbols into a nested hash.

Parameters:

  • value (Array|Hash|String|Symbol)
  • result (Hash) (defaults to: {})

Returns:

  • (Hash)

    Symbol keys with Hash values of arbitrary depth



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/blueprinter-activerecord/helpers.rb', line 54

def merge_values(value, result = {})
  case value
  when Array
    value.each { |val| merge_values(val, result) }
  when Hash
    value.each { |key, val|
      key = key.to_sym
      result[key] ||= {}
      merge_values(val, result[key])
    }
  when Symbol
    result[value] ||= {}
  when String
    result[value.to_sym] ||= {}
  else
    raise ArgumentError, "Unexpected value of type '#{value.class.name}' (#{value.inspect})"
  end
  result
end