Module: PocztaPolska::DynamicReader
Overview
This helper module is included into other classes and allows to access elements nested in the @data hash. In order to use it, a class must define a constant hash ATTR_MAP
. For the following element in the hash:
my_method: [:some, :nested, :value, :to_s]
calling object.my_method
will return @data[:nested].to_s. The last element in the array is either a conversion method name, a Proc
or nil
.
Instance Method Summary collapse
- #method_missing(method) ⇒ Object
-
#respond_to_missing?(method, include_private = false) ⇒ Boolean
Returns
true
if the method name is a key in theATTR_MAP
hash.
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method) ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/poczta_polska/dynamic_reader.rb', line 17 def method_missing(method) return super unless respond_to_missing?(method) keys = self.class::ATTR_MAP[method].dup convert = keys.pop d = @data keys.each { |k| d = d[k] } if convert.nil? d elsif convert.is_a?(Proc) convert.call(d) else d.public_send(convert) end end |
Instance Method Details
#respond_to_missing?(method, include_private = false) ⇒ Boolean
Returns true
if the method name is a key in the ATTR_MAP
hash.
13 14 15 |
# File 'lib/poczta_polska/dynamic_reader.rb', line 13 def respond_to_missing?(method, include_private = false) self.class::ATTR_MAP.has_key?(method) || super end |