module Settingable::Settings

A module containing the settings configuration.

Public Class Methods

included(base) click to toggle source
# File lib/settingable/settings.rb, line 44
def self.included(base)
  base.extend ClassMethods
end
new(settings = {}) click to toggle source

Initialize the settings. Merges the given settings to the default settings.

@param settings [Hash] The initial settings.

# File lib/settingable/settings.rb, line 59
def initialize(settings = {})
  @settings = Settingable::Hash.new(merged_settings(settings))
end

Public Instance Methods

build() { |self| ... } click to toggle source

Builds the settings construct. It yields itself, and then returns itself.

@yield @return [self]

# File lib/settingable/settings.rb, line 68
def build
  yield self
  self
end
method_missing(method, *args, &block) click to toggle source

Method missing. For set methods, it maps to the ‘:[]=` method; for regular methods (i.e. not bang or ? methods), it maps to the `:[]` method.

@return [Object]

Calls superclass method
# File lib/settingable/settings.rb, line 78
def method_missing(method, *args, &block)
  return super if args.length > 1 || block_given? || method =~ /(\?|\!)\z/
  map_method(method, args)
end
responds_to_missing?(method, _include_all = false) click to toggle source

A hook method for ruby. This should not be called directly. It lets ruby know that we respond to certain methods.

@param method [Symbol] The method to check. @return [Boolean]

# File lib/settingable/settings.rb, line 88
def responds_to_missing?(method, _include_all = false)
  !(method =~ /(\?|\!)\z/)
end

Private Instance Methods

map_method(method, args) click to toggle source

Maps the methods. If the method is a setter, i.e. ends in ‘=`, it sets the value. If arguments are provided to a non-setter method, it raises a NameError.

@param method [Symbol] @param args [Array<Object>] @return [Object] @raise NameError If more than 1 argument is provided to a

setter, or arguments are provided to a non-setter method.
# File lib/settingable/settings.rb, line 112
def map_method(method, args)
  if method =~ /\A(.*)\=\z/ && args.length == 1
    self[$+] = args[0]
  elsif args.length == 0
    self[method.to_s]
  else
    fail NameError, "Undefined method `#{method}' called on #{self}"
  end
end
merged_settings(provided) click to toggle source

Merges the given settings with the class defaults. Performs a deep merge.

@param provided [Hash] @return [Hash]

# File lib/settingable/settings.rb, line 99
def merged_settings(provided)
  DeepMerge.deep_merge(self.class.default_settings, provided)
end