Module: Cowtech::Extensions::Hash

Extended by:
ActiveSupport::Concern
Defined in:
lib/cowtech-extensions/hash.rb

Overview

Extensions for Hash objects.

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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

This is called when the user access a member using dotted notation.

Parameters:

  • method (String|Symbol)

    Key to search.

  • args (Array)

    Unused.

  • block (Proc)

    Unused.

Returns:

  • (Object)

    The value for the key.



19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/cowtech-extensions/hash.rb', line 19

def method_missing(method, *args, &block)
    rv = nil

    if self.has_key?(method.to_sym) then
       rv = self[method.to_sym]
    elsif self.has_key?(method.to_s) then
       rv = self[method.to_s]
    else
       rv = ::Hash.method_missing(method, *args, &block)
    end

    rv
end

Instance Method Details

#respond_to?(method) ⇒ Boolean

This is called when the user access a member using dotted notation.

Parameters:

  • method (String|Symbol)

    Key to search.

Returns:

  • (Boolean)

    true if the key exists, false otherwise.



37
38
39
# File 'lib/cowtech-extensions/hash.rb', line 37

def respond_to?(method)
	(self.has_key?(method.to_sym) || self.has_key?(method.to_s)) ? true : ::Hash.respond_to?(method)
end