Class: Hash

Inherits:
Object
  • Object
show all
Defined in:
lib/broadway/ext.rb,
lib/broadway/ext.rb

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.



83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/broadway/ext.rb', line 83

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 singular key

Returns an array



105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/broadway/ext.rb', line 105

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

#recursively_symbolize_keys!Object



2
3
4
5
6
7
8
9
10
11
12
# File 'lib/broadway/ext.rb', line 2

def recursively_symbolize_keys!
  self.symbolize_keys!
  self.values.each do |v|
    if v.is_a? Hash
      v.recursively_symbolize_keys!
    elsif v.is_a? Array
      v.recursively_symbolize_keys!
    end
  end
  self
end