Sha256: 0b26d7fa8ae1d15dfd9f3fcf426170b57086e9ce2da3d53482589ecd89403378

Contents?: true

Size: 1.12 KB

Versions: 13

Compression:

Stored size: 1.12 KB

Contents

module Hashie2
  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

13 entries across 13 versions & 1 rubygems

Version Path
hashie-model-1.4.2 vendor/hashie2/lib/hashie2/extensions/key_conversion.rb
hashie-model-1.4.1 vendor/hashie2/lib/hashie2/extensions/key_conversion.rb
hashie-model-1.4.0 vendor/hashie2/lib/hashie2/extensions/key_conversion.rb
hashie-model-1.3.9 vendor/hashie2/lib/hashie2/extensions/key_conversion.rb
hashie-model-1.3.8 vendor/hashie2/lib/hashie2/extensions/key_conversion.rb
hashie-model-1.3.7 vendor/hashie2/lib/hashie2/extensions/key_conversion.rb
hashie-model-1.3.6 vendor/hashie2/lib/hashie2/extensions/key_conversion.rb
hashie-model-1.3.5 vendor/hashie2/lib/hashie2/extensions/key_conversion.rb
hashie-model-1.3.4 vendor/hashie2/lib/hashie2/extensions/key_conversion.rb
hashie-model-1.3.3 vendor/hashie2/lib/hashie2/extensions/key_conversion.rb
hashie-model-1.3.2 vendor/hashie2/lib/hashie2/extensions/key_conversion.rb
hashie-model-1.3.1 vendor/hashie2/lib/hashie2/extensions/key_conversion.rb
hashie-model-1.3.0 vendor/hashie2/lib/hashie2/extensions/key_conversion.rb