Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # If you include this module in an instance of a Hash, it will allow you to
- # get at any of its values by method name rather than hashy key syntax.
- #
- # It will also inject itself on any hashes returned as a value.
- #
- # Also, if there's a value_for method on the instance, it will do pretty mapping
- # for the methods.
- module HashChainMethodizer
- def method_missing(method, *args, &block)
- if respond_to?(:value_for) && value_for.has_key?(method.to_sym)
- self[value_for[method.to_sym]]
- elsif has_key?(method.to_sym)
- val = self[method.to_sym]
- if val.is_a?(Hash)
- val.extend(HashChainMethodizer)
- end
- return val
- else
- super
- end
- end
- end
Add Comment
Please, Sign In to add comment