class RConfig::Config

Public Instance Methods

[](key) click to toggle source

Why the &*#^@*^&$ isn't HashWithIndifferentAccess doing this? HashWithIndifferentAccess doesn't override Hash's []! That's why it's so destructive!

Calls superclass method
# File lib/rconfig/config.rb, line 60
def [](key)
  key = key.to_s if key.kind_of?(Symbol)
  super(key)
end
default(key = self.default_key) click to toggle source

Allow hash.default => hash without breaking Hash's usage of default(key)

# File lib/rconfig/config.rb, line 68
def default(key = self.default_key)
  key = key.to_s if key.is_a?(Symbol)
  if key == self.default_key 
    self['default'] if key?('default')
  else
    hash_default(key)
  end
end
method_missing(method, *args) click to toggle source

Dotted notation can be used with arguments (useful for creating mock objects) in the YAML file the method name is a key, argument(s) form a nested key, so that the correct value is retrieved and returned.

For example loading to variable foo a yaml file that looks like: customer:

id: 12345678
verified:
  phone: verified
  :address: info_not_available
  ? [name, employer]
  : not_verified

Allows the following calls: foo.customer.id => 12345678 foo.customer.verified.phone => verified foo.customer.verified(“phone”) => verified foo.customer.verified(:address) => info_not_available foo.customer.verified(“name”, “employer”) => not_verified

Note that :address is specified as a symbol, where phone is just a string. Depending on what kind of parameter the method being mocked out is going to be called with, define in the YAML file either a string or a symbol. This also works inside the composite array keys.

# File lib/rconfig/config.rb, line 42
def method_missing(method, *args)
  method = method.to_s
  return if method == 'default_key'
  value = self[method]
  case args.size
  when 0  # e.g.: RConfig.application.method
    value
  when 1  # e.g.: RConfig.application.method(one_arg)
    value.send(args[0])
  else    # e.g.: RConfig.application.method(arg_one, args_two, ...)
    value[args]
  end
end
unknown() click to toggle source

HashWithIndifferentAccess#default is broken in early versions of Rails. This is defined to use the hash version in Config#default

# File lib/rconfig/config.rb, line 15
define_method(:hash_default, Hash.instance_method(:default))