Sha256: 97c413d98fa76b755fda3f93ba75662736e2cf270e87a80e1fff6c8a5ed4dbf9

Contents?: true

Size: 1.21 KB

Versions: 1

Compression:

Stored size: 1.21 KB

Contents

class Array
  def stringify_all_keys
    map do |v|
      case v
      when Hash, Array
        v.stringify_all_keys
      else
        v
      end
    end
  end

  def symbolize_all_keys
    map do |v|
      case v
      when Hash, Array
        v.symbolize_all_keys
      else
        v
      end
    end
  end
end

class Hash
  def stringify_all_keys
    result = {}
    each do |k, v|
      result[k.to_s] = case v
                       when Hash, Array
                         v.stringify_all_keys
                       else
                         v
                       end
    end
    result
  end

  def symbolize_all_keys
    result = {}
    each do |k, v|
      result[k.to_sym] = case v
                         when Hash, Array
                           v.symbolize_all_keys
                         else
                           v
                         end
    end
    result
  end

  def deep_merge(second)
    merger = proc { |_, v1, v2|
      if Hash === v1 && Hash === v2
        v1.merge(v2, &merger)
      elsif Array === v1 && Array === v2
        v1 | v2
      elsif [:undefined, nil,
             :nil].include?(v2)
        v1
      else
        v2
      end
    }
    merge(second.to_h, &merger)
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
metanorma-utils-1.3.2 lib/utils/hash_transform_keys.rb