Sha256: 3d6e0f52f28c35bd40933f9ea1657ac698a2bb63f680dad5937a259a88e32599

Contents?: true

Size: 1.53 KB

Versions: 9

Compression:

Stored size: 1.53 KB

Contents

# frozen_string_literal: true

# @api private
module Nanoc::HashExtensions
  # Returns a new hash where all keys are recursively converted to symbols by
  # calling {Nanoc::ArrayExtensions#__nanoc_symbolize_keys_recursively} or
  # {Nanoc::HashExtensions#__nanoc_symbolize_keys_recursively}.
  #
  # @return [Hash] The converted hash
  def __nanoc_symbolize_keys_recursively
    hash = {}
    each_pair do |key, value|
      new_key   = key.respond_to?(:to_sym) ? key.to_sym : key
      new_value = value.respond_to?(:__nanoc_symbolize_keys_recursively) ? value.__nanoc_symbolize_keys_recursively : value
      hash[new_key] = new_value
    end
    hash
  end

  def __nanoc_stringify_keys_recursively
    hash = {}
    each_pair do |key, value|
      new_key   = key.is_a?(Symbol) ? key.to_s : key
      new_value = value.respond_to?(:__nanoc_stringify_keys_recursively) ? value.__nanoc_stringify_keys_recursively : value
      hash[new_key] = new_value
    end
    hash
  end

  # Freezes the contents of the hash, as well as all hash values. The hash
  # values will be frozen using {#__nanoc_freeze_recursively} if they respond to
  # that message, or #freeze if they do not.
  #
  # @see Array#__nanoc_freeze_recursively
  #
  # @return [void]
  def __nanoc_freeze_recursively
    return if frozen?

    freeze
    each_pair do |_key, value|
      if value.respond_to?(:__nanoc_freeze_recursively)
        value.__nanoc_freeze_recursively
      else
        value.freeze
      end
    end
  end
end

# @api private
class Hash
  include Nanoc::HashExtensions
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
nanoc-4.11.0 lib/nanoc/base/core_ext/hash.rb
nanoc-4.10.4 lib/nanoc/base/core_ext/hash.rb
nanoc-4.10.3 lib/nanoc/base/core_ext/hash.rb
nanoc-4.10.2 lib/nanoc/base/core_ext/hash.rb
nanoc-4.10.1 lib/nanoc/base/core_ext/hash.rb
nanoc-4.10.0 lib/nanoc/base/core_ext/hash.rb
nanoc-4.9.9 lib/nanoc/base/core_ext/hash.rb
nanoc-4.9.8 lib/nanoc/base/core_ext/hash.rb
nanoc-4.9.7 lib/nanoc/base/core_ext/hash.rb