Module: Settingable::Settings

Extended by:
Forwardable
Defined in:
lib/settingable/settings.rb

Overview

A module containing the settings configuration.

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object

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

Returns:

  • (Object)


91
92
93
94
# File 'lib/settingable/settings.rb', line 91

def method_missing(method, *args, &block)
  return super if args.length > 1 || block_given? || method =~ /(\?|\!)\z/
  map_method(method, args)
end

Class Method Details

.included(base) ⇒ Object



44
45
46
# File 'lib/settingable/settings.rb', line 44

def self.included(base)
  base.extend ClassMethods
end

Instance Method Details

#[](key) ⇒ Object

Retrieves a key. If it doesn't exist, it errors.

Parameters:

  • key (Symbol, String)

    The key.

Returns:

  • (Object)


82
83
84
# File 'lib/settingable/settings.rb', line 82

def [](key)
  @settings.fetch(key.to_s.to_sym)
end

#[]=(key, value) ⇒ void

This method returns an undefined value.

Sets a key to a value.

Parameters:

  • key (Symbol, String)

    The key.

  • value (Object)

    The value.



74
75
76
# File 'lib/settingable/settings.rb', line 74

def []=(key, value)
  @settings[key.to_s.to_sym] = value
end

#build { ... } ⇒ self

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

Yields:

Returns:

  • (self)


64
65
66
67
# File 'lib/settingable/settings.rb', line 64

def build
  yield self
  self
end

#initialize(settings = {}) ⇒ Object

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

Parameters:

  • settings (Hash) (defaults to: {})

    The initial settings.



55
56
57
# File 'lib/settingable/settings.rb', line 55

def initialize(settings = {})
  @settings = DeepMerge.deep_merge(self.class.default_settings, settings)
end