Class: Ruber::SettingsContainer::Proxy
- Inherits:
- BasicObject
- Defined in:
- lib/ruber/settings_container.rb
Overview
Utility class to be used to avoid having to repeat the group when fetching options from a Ruber::SettingsContainer. When created, it takes a Ruber::SettingsContainer and a group name as parameters. To access one option in that group, you can simply call the #[] and #[]= methods specifying the option name (and, in the second case, the value). Alternatively, you can use the option names as if they were method names (appending an equal sign to store values)
Note that you don’t usually need to create instances of this class, as #[] returns one when called with one argument.
Instance Method Summary collapse
-
#[](option, path_opt = nil) ⇒ Object
Calls the #[] method of the associated container.
-
#[]=(option, value) ⇒ Object
Calls the #[]= method of the associated container.
-
#initialize(container, group) ⇒ Proxy
constructor
A new instance of Proxy.
-
#method_missing(meth, *args) ⇒ Object
Attempts to read or write an option with the same name as the method.
Constructor Details
#initialize(container, group) ⇒ Proxy
Returns a new instance of Proxy.
139 140 141 142 |
# File 'lib/ruber/settings_container.rb', line 139 def initialize container, group @container = container @group = group end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(meth, *args) ⇒ Object
Attempts to read or write an option with the same name as the method
If the method name ends with a @=@, it attempts to change the value of a setting called as the method in the group associated with the proxy. If the method doesn’t end with an @=@, it attempts to read the setting
The contents of the args array are passed to the called method
180 181 182 183 184 185 |
# File 'lib/ruber/settings_container.rb', line 180 def method_missing meth, *args name = meth.to_s if name[-1,1] == '=' then @container.send :[]=, @group, name[0...-1].to_sym, *args else @container[@group, meth, *args] end end |
Instance Method Details
#[](option, path_opt = nil) ⇒ Object
153 154 155 |
# File 'lib/ruber/settings_container.rb', line 153 def [](option, path_opt = nil) @container[@group, option, path_opt] end |