Sha256: 11d187bc8303aee0cc333b9f2bdb2b6a101a043e2a0d83e8ebebfb2dae9b6837

Contents?: true

Size: 1.48 KB

Versions: 1

Compression:

Stored size: 1.48 KB

Contents

require 'codecode/common/utils/version'

module CodeCode
  module Common
    module Utils
      # Utility methods for Hash manipulation
      module Hash
        # Convert Hash string keys to symbol keys
        #
        # @param [Hash] hash
        # @return [Hash] The resulting hash with symbolized keys
        def self.symbolize_keys(hash)
          hash.inject({}){ |memo, (k, v)|
            memo[k.to_sym] = v
            memo[k.to_sym] = symbolize_keys v if v.class.to_s.eql? 'Hash'
            memo
          }
        end

        # BANG! Convert Hash string keys to symbol keys
        #
        # @param [Hash] hash
        # @return [Hash] The resulting hash with symbolized keys
        def self.symbolize_keys!(hash)
          new_hash = hash.inject({}){ |memo, (k, v)|
            memo[k.to_sym] = v
            memo[k.to_sym] = symbolize_keys v if v.class.to_s.eql? 'Hash'
            memo
          }
          hash.replace new_hash
        end

        # Removes a key from the hash and return the modified hash
        #
        # @param [Hash] hash
        # @param [Symbol] key
        # @return [Hash] The given hash without the removed key
        def self.except(hash, key)
          hash.delete(key)
        end

        # Return a new hash only with the specified keys
        #
        # @param [Hash] hash
        # @param [Array] keys
        # @return [Hash]
        def self.select(hash, keys)
          hash.select { |k, v| keys.include?(k) }
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
codecode-common-utils-0.1.3 lib/codecode/common/utils.rb