Sha256: 8d26d69328d74ba27f8901c890b892385f6d0cc8354f4c0f2235cfcd227ccb3f

Contents?: true

Size: 967 Bytes

Versions: 5

Compression:

Stored size: 967 Bytes

Contents

require 'time'

class Hash

  # Cleans up the hash and returns the result. It performs the following
  # operations:
  #
  # * Values with keys ending in _at and _on are converted into Times and
  #   Dates, respectively
  # * All keys are converted to symbols
  # * Value strings 'true', 'false', and 'none' are converted into
  #   true, false, and nil, respectively
  def clean
    inject({}) do |hash, (key, value)|
      real_key = key.to_s
      if real_key =~ /_on$/
        hash.merge(key.to_sym => Date.parse(value))
      elsif real_key =~ /_at$/
        hash.merge(key.to_sym => Time.parse(value))
      elsif value == 'true'
        hash.merge(key.to_sym => true)
      elsif value == 'false'
        hash.merge(key.to_sym => false)
      elsif value == 'none'
        hash.merge(key.to_sym => nil)
      elsif value.is_a?(Hash)
        hash.merge(key.to_sym => value.clean)
      else
        hash.merge(key.to_sym => value)
      end
    end
  end

end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
nanoc-2.0.1 lib/nanoc/base/core_ext/hash.rb
nanoc-2.0 lib/nanoc/base/core_ext/hash.rb
nanoc-2.0.2 lib/nanoc/base/core_ext/hash.rb
nanoc-2.0.3 lib/nanoc/base/core_ext/hash.rb
nanoc-2.0.4 lib/nanoc/base/core_ext/hash.rb