Sha256: a600fcf53cc6969b28f9984cdbe03fd6250942fcb3e4f7dbecf2681440331640

Contents?: true

Size: 684 Bytes

Versions: 3

Compression:

Stored size: 684 Bytes

Contents

# frozen_string_literal: true

module Housecanary
  module Utils # :nodoc:
    def deep_symbolize_keys(hash)
      deep_transform_keys(hash) { |key|
        begin
          key.to_sym
        rescue
          key
        end
      }
    end
    module_function :deep_symbolize_keys

    private

    def deep_transform_keys(object, &block)
      case object
      when Hash
        object.each_with_object({}) do |(key, value), result|
          result[yield(key)] = deep_transform_keys(value, &block)
        end
      when Array
        object.map { |e| deep_transform_keys(e, &block) }
      else
        object
      end
    end
    module_function :deep_transform_keys
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
housecanary-ruby-0.2.2 lib/housecanary/utils.rb
housecanary-ruby-0.2.1 lib/housecanary/utils.rb
housecanary-ruby-0.2.0 lib/housecanary/utils.rb