Guest User

Untitled

a guest
Jan 21st, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.83 KB | None | 0 0
  1. # If you include this module in an instance of a Hash, it will allow you to
  2. # get at any of its values by method name rather than hashy key syntax.
  3. #
  4. # It will also inject itself on any hashes returned as a value.
  5. #
  6. # Also, if there's a value_for method on the instance, it will do pretty mapping
  7. # for the methods.
  8. module HashChainMethodizer
  9. def method_missing(method, *args, &block)
  10. if respond_to?(:value_for) && value_for.has_key?(method.to_sym)
  11. self[value_for[method.to_sym]]
  12. elsif has_key?(method.to_sym)
  13. val = self[method.to_sym]
  14. if val.is_a?(Hash)
  15. val.extend(HashChainMethodizer)
  16. end
  17. return val
  18. else
  19. super
  20. end
  21. end
  22. end
Add Comment
Please, Sign In to add comment