Module: FeedNormalizer::Singular
Instance Method Summary collapse
-
#method_missing(name, *args) ⇒ Object
If the method being called is a singular (in this simple case, does not end with an ‘s’), then it calls the plural method, and calls the first element.
- #respond_to?(x, y = false) ⇒ Boolean
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args) ⇒ Object
If the method being called is a singular (in this simple case, does not end with an ‘s’), then it calls the plural method, and calls the first element. We’re assuming that plural methods provide an array.
Example: Object contains an array called ‘alphas’, which looks like [:a, :b, :c]. Call object.alpha and :a is returned.
13 14 15 16 17 |
# File 'lib/structures.rb', line 13 def method_missing(name, *args) plural_name = :"#{name}s" return self.send(plural_name).first if respond_to?(plural_name) super(name, *args) end |
Instance Method Details
#respond_to?(x, y = false) ⇒ Boolean
19 20 21 |
# File 'lib/structures.rb', line 19 def respond_to?(x, y=false) self.class::ELEMENTS.include?(x) || self.class::ELEMENTS.include?(:"#{x}s") || super(x, y) end |