Class: Hash

Inherits:
Object
  • Object
show all
Defined in:
lib/jekyll/core_ext.rb

Direct Known Subclasses

Jekyll::Configuration

Instance Method Summary collapse

Instance Method Details

#deep_merge(hash) ⇒ Object

Merges self with another hash, recursively.

This code was lovingly stolen from some random gem: gemjack.com/gems/tartan-0.1.1/classes/Hash.html

Thanks to whoever made it.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/jekyll/core_ext.rb', line 8

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

#pluralized_array(singular_key, plural_key) ⇒ Object

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



30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/jekyll/core_ext.rb', line 30

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

#symbolize_keysObject



52
53
54
# File 'lib/jekyll/core_ext.rb', line 52

def symbolize_keys
  dup.symbolize_keys!
end

#symbolize_keys!Object



45
46
47
48
49
50
# File 'lib/jekyll/core_ext.rb', line 45

def symbolize_keys!
  keys.each do |key|
    self[(key.to_sym rescue key) || key] = delete(key)
  end
  self
end