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.

Examples:

Without using Proxy:


o1 = settings_container[:group1, :o1]
o2 = settings_container[:group1, :o2]
o3 = settings_container[:group1, :o3]
o4 = settings_container[:group1, :o4, :abs]
settings_container[:group1, :o1] = 1
settings_container[:group1, :o2] = 2
settings_container[:group1, :o3] = 3
settings_container[:group1, :o4] = 4

Using #[] and #[]:


proxy = settings_container[:group1]
o1 = proxy[:o1]
o2 = proxy[:o2]
o3 = proxy[:o3]
o4 = proxy[:o4, :abs]
proxy[:o1] = 1
proxy[:o2] = 2
proxy[:o3] = 3
proxy[:o4] = 4

Using Proxy via #method_missing


proxy = settings_container[:group1]
o1 = proxy.o1
o2 = proxy.o2
o3 = proxy.o3
o4 = proxy.o4 :abs
proxy.o1 = 1
proxy.o2 = 2
proxy.o3 = 3
proxy.o4 = 4

Instance Method Summary collapse

Constructor Details

#initialize(container, group) ⇒ Proxy

Returns a new instance of Proxy.

Parameters:

  • container (SettingsContainer)

    the object to create the proxy for

  • group (Symbol)

    the group to create the proxy for



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

Parameters:

  • meth (Symbol)

    the name of the method

  • args (Array)

    the parameters to pass



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

Calls the #[] method of the associated container.

The group passed to the container’s #[] method is the one associated with the proxy.

Parameters:

Returns:



153
154
155
# File 'lib/ruber/settings_container.rb', line 153

def [](option, path_opt = nil)
  @container[@group, option, path_opt]
end

#[]=(option, value) ⇒ Object

Calls the #[]= method of the associated container.

The group passed to the container’s #[]= method is the one associated with the proxy.

Parameters:



165
166
167
# File 'lib/ruber/settings_container.rb', line 165

def []=(option, value)
  @container[@group, option] = value
end