Class: Plate::HashProxy
- Inherits:
-
Object
- Object
- Plate::HashProxy
- Defined in:
- lib/plate/hash_proxy.rb
Overview
Just a blank class that allows you to call methods that return hash values
Instance Method Summary collapse
-
#[](key) ⇒ Object
Pass through for getting the hash’s value at the given key.
-
#initialize(source = {}) ⇒ HashProxy
constructor
Pass in a hash object to use this class as a proxy for its keys.
-
#method_missing(method, *args) ⇒ Object
Handle method missing calls and proxy methods to hash values.
Constructor Details
#initialize(source = {}) ⇒ HashProxy
Pass in a hash object to use this class as a proxy for its keys.
5 6 7 8 |
# File 'lib/plate/hash_proxy.rb', line 5 def initialize(source = {}) @source = source @source ||= {} end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args) ⇒ Object
Handle method missing calls and proxy methods to hash values.
Allows for checking to see if a key exists and returning a key value.
18 19 20 21 22 23 24 25 26 |
# File 'lib/plate/hash_proxy.rb', line 18 def method_missing(method, *args) if @source.has_key?(method) self[method] elsif method.to_s =~ /^[a-zA-Z0-9_]*\?$/ @source.has_key?(method.to_s.gsub!(/\?/, '').to_sym) else nil end end |
Instance Method Details
#[](key) ⇒ Object
Pass through for getting the hash’s value at the given key.
11 12 13 |
# File 'lib/plate/hash_proxy.rb', line 11 def [](key) @source[key] end |