Class: Hash

Inherits:
Object
  • Object
show all
Defined in:
lib/i18n_translation_spawner/hash.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.convert_hash_to_ordered_hash(object, deep = false) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/i18n_translation_spawner/hash.rb', line 18

def self.convert_hash_to_ordered_hash(object, deep = false)
  # Hash is ordered in Ruby 1.9!
  if RUBY_VERSION >= '1.9'
    return object
  else
    if object.is_a?(Hash)
      ActiveSupport::OrderedHash.new.tap do |map|
        object.each { |k, v| map[k] = deep ? convert_hash_to_ordered_hash(v, deep) : v }
      end
    elsif deep && object.is_a?(Array)
      array = Array.new
      object.each_with_index { |v, i| array[i] = convert_hash_to_ordered_hash(v, deep) }
      return array
    else
      return object
    end
  end
end

Instance Method Details

#deep_stringify_keysObject



2
3
4
5
6
7
8
# File 'lib/i18n_translation_spawner/hash.rb', line 2

def deep_stringify_keys
  inject({}) { |result, (key, value)|
    value = value.deep_stringify_keys if value.is_a?(Hash)
    result[(key.to_s rescue key) || key] = value
    result
  }
end

#deep_stringify_keys!Object



10
11
12
13
14
15
16
# File 'lib/i18n_translation_spawner/hash.rb', line 10

def deep_stringify_keys!
  stringify_keys!
  each do |k, v|
    self[k] = self[k].deep_stringify_keys! if self[k].is_a?(Hash)
  end
  self
end

#to_ordered_hash(deep = false) ⇒ Object



37
38
39
# File 'lib/i18n_translation_spawner/hash.rb', line 37

def to_ordered_hash(deep = false)
  Hash.convert_hash_to_ordered_hash(self, deep)
end