Sha256: f6db5363a3b0fd3521afd58d35a74b0daffd904face88d5b53ca37e7a240eea4

Contents?: true

Size: 1.58 KB

Versions: 22

Compression:

Stored size: 1.58 KB

Contents

class Hash
  # Merges self with another hash, recursively.
  #
  # This code was lovingly stolen from some random gem:
  # http://gemjack.com/gems/tartan-0.1.1/classes/Hash.html
  #
  # Thanks to whoever made it.
  def deep_merge(hash)
    target = dup

    hash.keys.each do |key|
      if hash[key].is_a? Hash and self[key].is_a? Hash
        target[key] = target[key].deep_merge(hash[key])
        next
      end

      target[key] = hash[key]
    end

    target
  end

  # Read array from the supplied hash favouring the singular key
  # and then the plural key, and handling any nil entries.
  #   +hash+ the hash to read from
  #   +singular_key+ the singular key
  #   +plural_key+ the plural key
  #
  # Returns an array
  def pluralized_array(singular_key, plural_key)
    hash = self
    if hash.has_key?(singular_key)
      array = [hash[singular_key]] if hash[singular_key]
    elsif hash.has_key?(plural_key)
      case hash[plural_key]
      when String
        array = hash[plural_key].split
      when Array
        array = hash[plural_key].compact
      end
    end
    array || []
  end
end

# Thanks, ActiveSupport!
class Date
  # Converts datetime to an appropriate format for use in XML
  def xmlschema
    strftime("%Y-%m-%dT%H:%M:%S%Z")
  end if RUBY_VERSION < '1.9'
end

module Enumerable
  # Returns true if path matches against any glob pattern.
  # Look for more detail about glob pattern in method File::fnmatch.
  def glob_include?(e)
    any? { |exp| File.fnmatch?(exp, e) }
  end
end

if RUBY_VERSION < "1.9"
  class String
    def force_encoding(enc)
      self
    end
  end
end

Version data entries

22 entries across 22 versions & 4 rubygems

Version Path
serum-0.3.2 lib/serum/core_ext.rb
jekyll-1.0.4 lib/jekyll/core_ext.rb
monad-0.0.2 lib/monad/core_ext.rb
moft-1.0.5 lib/moft/core_ext.rb
jekyll-1.0.3 lib/jekyll/core_ext.rb
moft-1.0.4 lib/moft/core_ext.rb
moft-1.0.3 lib/moft/core_ext.rb
monad-0.0.1 lib/monad/core_ext.rb
jekyll-1.0.2 lib/jekyll/core_ext.rb
moft-1.0.2.1 lib/moft/core_ext.rb
moft-1.0.2 lib/moft/core_ext.rb
moft-1.0.1 lib/moft/core_ext.rb
jekyll-1.0.1 lib/jekyll/core_ext.rb
jekyll-1.0.0 lib/jekyll/core_ext.rb
serum-0.3.0 lib/serum/core_ext.rb
moft-1.0.0 lib/moft/core_ext.rb
jekyll-1.0.0.rc1 lib/jekyll/core_ext.rb
jekyll-1.0.0.beta4 lib/jekyll/core_ext.rb
jekyll-1.0.0.beta3 lib/jekyll/core_ext.rb
serum-0.2.0 lib/serum/core_ext.rb