Class: SleepingKingStudios::Tools::Toolbox::Configuration

Inherits:
Object
  • Object
show all
Extended by:
ClassMethods
Defined in:
lib/sleeping_king_studios/tools/toolbox/configuration.rb

Overview

Abstract base class for defining configuration objects.

Defined Under Namespace

Modules: ClassMethods

Constant Summary collapse

DEFAULT_OPTION =
ClassMethods::DEFAULT_OPTION

Instance Method Summary collapse

Methods included from ClassMethods

namespace, option

Constructor Details

#initialize(data = nil) {|The| ... } ⇒ Configuration

Returns a new instance of Configuration.

Parameters:

  • data (Hash, Object) (defaults to: nil)

    The data source used to populate configuration values. Can be a Hash or a data object. If the data source is nil, or no data source is given, values will be set to their respective defaults.

Yield Parameters:

  • The (Class)

    singleton class of the new configuration object.



125
126
127
128
129
130
131
132
133
134
135
# File 'lib/sleeping_king_studios/tools/toolbox/configuration.rb', line 125

def initialize(data = nil)
  @data           = convert_data_to_struct(data)
  @root_namespace = self

  return unless block_given?

  SleepingKingStudios::Tools::CoreTools
    .deprecate('Configuration#initialize with a block')

  yield(singleton_class)
end

Instance Method Details

#[](key) ⇒ Object



137
138
139
# File 'lib/sleeping_king_studios/tools/toolbox/configuration.rb', line 137

def [](key)
  send(key) if respond_to?(key)
end

#[]=(key, value) ⇒ Object



141
142
143
# File 'lib/sleeping_king_studios/tools/toolbox/configuration.rb', line 141

def []=(key, value)
  send(:"#{key}=", value) if respond_to?(key)
end

#dig(*keys) ⇒ Object



145
146
147
148
149
150
151
152
153
# File 'lib/sleeping_king_studios/tools/toolbox/configuration.rb', line 145

def dig(*keys)
  keys.reduce(self) do |config, key|
    value = config[key]

    return value if value.nil?

    value
  end
end

#fetch(key, default = DEFAULT_OPTION) ⇒ Object

Raises:

  • (KeyError)


155
156
157
158
159
160
161
162
163
# File 'lib/sleeping_king_studios/tools/toolbox/configuration.rb', line 155

def fetch(key, default = DEFAULT_OPTION)
  return send(key) if respond_to?(key)

  return default unless default == DEFAULT_OPTION

  return yield(key) if block_given?

  raise KeyError, "key not found: #{key.inspect}"
end