Sha256: 95c6c42cdc8386116e216598cd223a62286c02b5527bcf6556bfcfcdc3cd5efb

Contents?: true

Size: 1.17 KB

Versions: 5

Compression:

Stored size: 1.17 KB

Contents

module SimplyUseful
  module CoreExtensions
    module Keys

      # How it's done in active support
      # File lib/active_support/core_ext/hash/keys.rb, line 22
      #22:         def symbolize_keys
      #23:           inject({}) do |options, (key, value)|
      #24:             options[(key.to_sym rescue key) || key] = value
      #25:             options
      #26:           end
      #27:         end

      # Another article mentioning that method
      # http://www.any-where.de/blog/ruby-hash-convert-string-keys-to-symbols/

      def deep_symbolize_keys
        _deep_symbolize_keys(self)
      end

      def deep_symbolize_keys!
        self.replace(self.deep_symbolize_keys)
      end

      private

      def _deep_symbolize_keys(value)
        if value.is_a? Hash
          hash = value.inject({}) do |memo,(k,v)|
            memo[(k.to_sym rescue k)|| k] = _deep_symbolize_keys(v)
            memo
          end
          return hash
        elsif value.is_a? Array
          value.map do |e|
            _deep_symbolize_keys(e)
          end
        else
          value
        end
      end

    end
  end
end

class Hash
  include SimplyUseful::CoreExtensions::Keys
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
simply_useful-0.2.3 lib/simply_useful/core_ext/hash.rb
simply_useful-0.2.2 lib/simply_useful/core_ext/hash.rb
simply_useful-0.2.1 lib/simply_useful/core_ext/hash.rb
simply_useful-0.2.0 lib/simply_useful/core_ext/hash.rb
simply_useful-0.1.6 lib/core_ext/hash.rb