Sha256: 90ee91805dccbc8eff6e472e10b647f512c0b54b0e185d9865b0ce5ab4ed5db5

Contents?: true

Size: 1.12 KB

Versions: 8

Compression:

Stored size: 1.12 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!
        keys.each do |k|
          self[k.to_s] = self.delete(k)
        end
        self
      end

      # Return a new hash with all keys converted
      # to strings.
      def stringify_keys
        dup.stringify_keys!
      end
    end

    module SymbolizeKeys
      # Convert all keys in the hash to strings.
      #
      # @example
      #   test = {'abc' => 'def'}
      #   test.symbolize_keys!
      #   test # => {:abc => 'def'}
      def symbolize_keys!
        keys.each do |k|
          self[k.to_sym] = self.delete(k)
        end
        self
      end

      # Return a new hash with all keys converted
      # to symbols.
      def symbolize_keys
        dup.symbolize_keys!
      end
    end
    
    module KeyConversion
      def self.included(base)
        base.send :include, SymbolizeKeys
        base.send :include, StringifyKeys
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 3 rubygems

Version Path
cb_hashie-2.0.0.beta lib/hashie/extensions/key_conversion.rb
hashie-pre-2.0.0.beta lib/hashie/extensions/key_conversion.rb
hashie-model-1.2.3 vendor/hashie/lib/hashie/extensions/key_conversion.rb
hashie-model-1.2.1 vendor/hashie/lib/hashie/extensions/key_conversion.rb
hashie-model-1.2.0 vendor/hashie/lib/hashie/extensions/key_conversion.rb
hashie-model-1.1.0 vendor/hashie/lib/hashie/extensions/key_conversion.rb
hashie-model-1.0.1 vendor/hashie/lib/hashie/extensions/key_conversion.rb
hashie-model-1.0.0.alpha vendor/hashie/lib/hashie/extensions/key_conversion.rb