Sha256: 2a6c97f8420acc1c1510dd47eef9de07c5fb71d0d137a8895ec887d6f78f1ac4

Contents?: true

Size: 1.54 KB

Versions: 7

Compression:

Stored size: 1.54 KB

Contents

module Metanorma
  module Utils
    module 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
  end
end

module Metanorma
  module Utils
    module 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].include?(v2)
            v1
          else
            v2
          end
        }
        merge(second.to_h, &merger)
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
metanorma-utils-1.10.2 lib/utils/hash_transform_keys.rb
metanorma-utils-1.10.1 lib/utils/hash_transform_keys.rb
metanorma-utils-1.10.0 lib/utils/hash_transform_keys.rb
metanorma-utils-1.9.0 lib/utils/hash_transform_keys.rb
metanorma-utils-1.8.5 lib/utils/hash_transform_keys.rb
metanorma-utils-1.8.4 lib/utils/hash_transform_keys.rb
metanorma-utils-1.8.3 lib/utils/hash_transform_keys.rb