Module: Lazier::Hash

Extended by:
ActiveSupport::Concern
Defined in:
lib/lazier/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.



18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/lazier/hash.rb', line 18

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.



36
37
38
# File 'lib/lazier/hash.rb', line 36

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