Module: HashNinja::ActiveHash

Defined in:
lib/hash_ninja/active_hash.rb

Overview

Hash with extended ActiveSupport

See also active_support/core_ext/hash/keys

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object

Provides the following recursive methods.

  • recursively_camelize_keys!

  • recursively_camelize_lower_keys!

  • recursively_classify_keys!

  • recursively_underscore_keys!



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/hash_ninja/active_hash.rb', line 90

def method_missing(name, *args, &block)
  # provides 'recursively_xxx' methods
  if name.match(/recursively_/)
    method_name = name.to_s.sub(/^recursively_/, '')
    self.public_send(method_name, *args, &block)
    self.values.each do |value|
      if value.is_a? Hash
        value.extend HashNinja::ActiveHash
        value.public_send(name, *args, &block)
      elsif value.is_a? Array
        value.public_send(name, *args, &block)
      end
    end
    self
  end
end

Instance Method Details

#camelize_keys!Object

Applies ActiveSupport::Inflector#camelize to all the Hash#keys destructively. Keys should be either Symbol or String.

Examples

hash = {:ruby_on_rails => 'http://rubyonrails.org/'}
has.activate!.camelize_keys!  # => {:RubyOnRails => 'http://rubyonrails.org/'}


54
55
56
57
58
59
60
61
62
63
# File 'lib/hash_ninja/active_hash.rb', line 54

def camelize_keys!
  self.keys.each do |key|
    value = delete(key)
    if key.is_a? String
      self[key.camelize] = value
    elsif key.is_a? Symbol
      self[key.to_s.camelize.to_sym] = value
    end
  end
end

#camelize_lower_keys!Object

Applies ActiveSupport::Inflector#camelize(:lower) to all the Hash#keys destructively. Keys should be either Symbol or String.

Examples

hash = {:ruby_on_rails => 'http://rubyonrails.org/'}
has.activate!.camelize_lower_keys!  # => {:rubyOnRails => 'http://rubyonrails.org/'}


72
73
74
75
76
77
78
79
80
81
# File 'lib/hash_ninja/active_hash.rb', line 72

def camelize_lower_keys!
  self.keys.each do |key|
    value = delete(key)
    if key.is_a? String
      self[key.camelize(:lower)] = value
    elsif key.is_a? Symbol
      self[key.to_s.camelize(:lower).to_sym] = value
    end
  end
end

#classify_keys!Object

Applies ActiveSupport::Inflector#classify to all the Hash#keys destructively. Keys should be either Symbol or String.

Examples

hash = {:ruby_on_rails => 'http://rubyonrails.org/'}
has.activate!.classify_keys!  # => {:RubyOnRails => 'http://rubyonrails.org/'}


37
38
39
40
41
42
43
44
45
46
# File 'lib/hash_ninja/active_hash.rb', line 37

def classify_keys!
  self.keys.each do |key|
    value = delete(key)
    if key.is_a? String
      self[key.classify] = value
    elsif key.is_a? Symbol
      self[key.to_s.classify.to_sym] = value
    end
  end
end

#underscore_keys!Object

Applies ActiveSupport::Inflector.underscore to all the Hash#keys destructively. Keys should be either Symbol or String.

Examples

hash = {:rubyOnRails => 'http://rubyonrails.org/'}
has.activate!.underscore_keys!  # => {:ruby_on_rails => 'http://rubyonrails.org/'}


19
20
21
22
23
24
25
26
27
28
29
# File 'lib/hash_ninja/active_hash.rb', line 19

def underscore_keys!
  self.keys.each do |key|
    value = delete(key)
    if key.is_a? String
      self[key.underscore] = value
    elsif key.is_a? Symbol
      self[key.to_s.underscore.to_sym] = value
    end
  end
  self
end