Sha256: 23065a678324102495631267eda653130028de22af375e1f47e3670e6c51e846

Contents?: true

Size: 1.87 KB

Versions: 19

Compression:

Stored size: 1.87 KB

Contents

module Hashie
  module Extensions
    module StringifyKeys
      # Convert all keys in the hash to strings.
      #
      # @example
      #   test = {:abc => 'def'}
      #   test.stringify_keys!
      #   test # => {'abc' => 'def'}
      def stringify_keys!
        StringifyKeys.stringify_keys!(self)
        self
      end

      # Return a new hash with all keys converted
      # to strings.
      def stringify_keys
        StringifyKeys.stringify_keys(self)
      end

      module ClassMethods
        # Stringify all keys recursively within nested
        # hashes and arrays.
        # @api private
        def stringify_keys_recursively!(object)
          case object
          when self.class
            stringify_keys!(object)
          when ::Array
            object.each do |i|
              stringify_keys_recursively!(i)
            end
          when ::Hash
            stringify_keys!(object)
          end
        end

        # Convert all keys in the hash to strings.
        #
        # @param [::Hash] hash
        # @example
        #   test = {:abc => 'def'}
        #   test.stringify_keys!
        #   test # => {'abc' => 'def'}
        def stringify_keys!(hash)
          hash.extend(Hashie::Extensions::StringifyKeys) unless hash.respond_to?(:stringify_keys!)
          hash.keys.each do |k|
            stringify_keys_recursively!(hash[k])
            hash[k.to_s] = hash.delete(k)
          end
          hash
        end

        # Return a copy of hash with all keys converted
        # to strings.
        # @param [::Hash] hash
        def stringify_keys(hash)
          copy = hash.dup
          copy.extend(Hashie::Extensions::StringifyKeys) unless copy.respond_to?(:stringify_keys!)
          copy.tap do |new_hash|
            stringify_keys!(new_hash)
          end
        end
      end

      class << self
        include ClassMethods
      end
    end
  end
end

Version data entries

19 entries across 17 versions & 3 rubygems

Version Path
tdiary-5.0.8 vendor/bundle/gems/hashie-3.5.7/lib/hashie/extensions/stringify_keys.rb
hashie-3.5.7 lib/hashie/extensions/stringify_keys.rb
hashie-3.5.6 lib/hashie/extensions/stringify_keys.rb
tdiary-5.0.5 vendor/bundle/gems/tdiary-5.0.4/vendor/bundle/gems/hashie-3.5.5/lib/hashie/extensions/stringify_keys.rb
tdiary-5.0.5 vendor/bundle/gems/hashie-3.5.5/lib/hashie/extensions/stringify_keys.rb
tdiary-5.0.4 vendor/bundle/gems/hashie-3.5.5/lib/hashie/extensions/stringify_keys.rb
hashie-3.5.5 lib/hashie/extensions/stringify_keys.rb
hashie-3.5.4 lib/hashie/extensions/stringify_keys.rb
hashie-3.5.3 lib/hashie/extensions/stringify_keys.rb
hashie-3.5.2 lib/hashie/extensions/stringify_keys.rb
hashie-3.5.1 lib/hashie/extensions/stringify_keys.rb
hashie-3.5.0 lib/hashie/extensions/stringify_keys.rb
logstash-input-salesforce-3.0.0 vendor/jruby/1.9/gems/hashie-3.4.6/lib/hashie/extensions/stringify_keys.rb
hashie-3.4.6 lib/hashie/extensions/stringify_keys.rb
hashie-3.4.5 lib/hashie/extensions/stringify_keys.rb
hashie-3.4.4 lib/hashie/extensions/stringify_keys.rb
tdiary-4.2.1 vendor/bundle/ruby/2.3.0/gems/hashie-3.4.3/lib/hashie/extensions/stringify_keys.rb
tdiary-4.2.1 vendor/bundle/ruby/2.2.0/gems/hashie-3.4.3/lib/hashie/extensions/stringify_keys.rb
hashie-3.4.3 lib/hashie/extensions/stringify_keys.rb