Sha256: af1a1b648c5c9657db2a259b764b05d07e82332c02522e0c4bc3c1c120c0cc86

Contents?: true

Size: 1.62 KB

Versions: 12

Compression:

Stored size: 1.62 KB

Contents

# encoding: utf-8

module Nanoc::HashExtensions

  # Returns a new hash where all keys are recursively converted to symbols by
  # calling {Nanoc::ArrayExtensions#symbolize_keys} or
  # {Nanoc::HashExtensions#symbolize_keys}.
  #
  # @return [Hash] The converted hash
  def symbolize_keys
    inject({}) do |hash, (key, value)|
      hash.merge(key.to_sym => value.respond_to?(:symbolize_keys) ? value.symbolize_keys : value)
    end
  end

  # Returns a new hash where all keys are recursively converted to strings by
  # calling {Nanoc::ArrayExtensions#stringify_keys} or
  # {Nanoc::HashExtensions#stringify_keys}.
  #
  # @return [Hash] The converted hash
  def stringify_keys
    inject({}) do |hash, (key, value)|
      hash.merge(key.to_s => value.respond_to?(:stringify_keys) ? value.stringify_keys : value)
    end
  end

  # Freezes the contents of the hash, as well as all hash values. The hash
  # values will be frozen using {#freeze_recursively} if they respond to
  # that message, or #freeze if they do not.
  #
  # @see Array#freeze_recursively
  #
  # @return [void]
  #
  # @since 3.2.0
  def freeze_recursively
    return if self.frozen?
    freeze
    each_pair do |key, value|
      if value.respond_to?(:freeze_recursively)
        value.freeze_recursively
      else
        value.freeze
      end
    end
  end

  # Calculates the checksum for this hash. Any change to this hash will result
  # in a different checksum.
  #
  # @return [String] The checksum for this hash
  #
  # @api private
  def checksum
    array = self.to_a.sort_by { |kv| kv[0].to_s }
    array.checksum
  end

end

class Hash
  include Nanoc::HashExtensions
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
nanoc-3.4.3 lib/nanoc/base/core_ext/hash.rb
nanoc-3.4.2 lib/nanoc/base/core_ext/hash.rb
nanoc-3.4.1 lib/nanoc/base/core_ext/hash.rb
nanoc-3.4.0 lib/nanoc/base/core_ext/hash.rb
nanoc-3.3.7 lib/nanoc/base/core_ext/hash.rb
nanoc-3.3.6 lib/nanoc/base/core_ext/hash.rb
nanoc-3.3.5 lib/nanoc/base/core_ext/hash.rb
nanoc-3.3.4 lib/nanoc/base/core_ext/hash.rb
nanoc-3.3.3 lib/nanoc/base/core_ext/hash.rb
nanoc-3.3.2 lib/nanoc/base/core_ext/hash.rb
nanoc-3.3.1 lib/nanoc/base/core_ext/hash.rb
nanoc-3.3.0 lib/nanoc/base/core_ext/hash.rb