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.



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

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

  SleepingKingStudios::Tools::CoreTools
    .deprecate('Configuration', message: 'use a Stannum::Struct')

  return unless block_given?

  # :nocov:
  yield(singleton_class)
  # :nocov:
end

Instance Method Details

#[](key) ⇒ Object



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

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

#[]=(key, value) ⇒ Object



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

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

#dig(*keys) ⇒ Object



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

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)


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

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