Class: Qonfig::DataSet

Inherits:
Object
  • Object
show all
Extended by:
DSL, Validator::DSL
Defined in:
lib/qonfig/data_set.rb,
lib/qonfig/plugins/toml/data_set.rb

Overview

Since:

  • 0.12.0

Defined Under Namespace

Modules: ClassBuilder Classes: Lock

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from DSL

compose, definition_commands, expose_json, expose_self, expose_toml, expose_yaml, extended, instance_commands, load_from_env, setting, values_file

Methods included from Validator::DSL

extended, validate, validators

Constructor Details

#initialize(settings_map = {}, &configurations) ⇒ DataSet

Returns a new instance of DataSet.

Parameters:

  • settings_map (Hash) (defaults to: {})
  • configurations (Proc)

Since:

  • 0.1.0



42
43
44
45
# File 'lib/qonfig/data_set.rb', line 42

def initialize(settings_map = {}, &configurations)
  @__lock__ = Qonfig::DataSet::Lock.new
  thread_safe_definition { load!(settings_map, &configurations) }
end

Instance Attribute Details

#settingsQonfig::Settings (readonly)

Returns:

Since:

  • 0.1.0



35
36
37
# File 'lib/qonfig/data_set.rb', line 35

def settings
  @settings
end

Class Method Details

.build(base_dataset_klass = self, &config_klass_definitions) ⇒ Qonfig::DataSet

Parameters:

  • base_dataset_klass (Class<Qonfig::DataSet>) (defaults to: self)
  • config_klass_definitions (Proc)

Returns:

Since:

  • 0.16.0



22
23
24
25
26
27
28
# File 'lib/qonfig/data_set.rb', line 22

def build(base_dataset_klass = self, &config_klass_definitions)
  unless base_dataset_klass <= Qonfig::DataSet
    raise(Qonfig::ArgumentError, 'Base inherited class should be a type of Qonfig::DataSet')
  end

  Class.new(base_dataset_klass, &config_klass_definitions).new
end

Instance Method Details

#[](key) ⇒ Object

Parameters:

  • key (String, Symbol)

Returns:

  • (Object)

Since:

  • 0.2.0



211
212
213
# File 'lib/qonfig/data_set.rb', line 211

def [](key)
  thread_safe_access { settings[key] }
end

#clear!void

This method returns an undefined value.

Since:

  • 0.2.0



266
267
268
# File 'lib/qonfig/data_set.rb', line 266

def clear!
  thread_safe_access { settings.__clear__ }
end

#configure(settings_map = {}, &configurations) ⇒ void

This method returns an undefined value.

Parameters:

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

Since:

  • 0.1.0



142
143
144
145
146
# File 'lib/qonfig/data_set.rb', line 142

def configure(settings_map = {}, &configurations)
  thread_safe_access do
    apply_settings(settings_map, &configurations)
  end
end

#deep_each_setting(&block) {|setting_key, setting_value| ... } ⇒ Enumerable

Parameters:

  • block (Proc)

Yields:

  • (setting_key, setting_value)

Yield Parameters:

  • setting_key (String)
  • setting_value (Object)

Returns:

  • (Enumerable)

Since:

  • 0.13.0



292
293
294
# File 'lib/qonfig/data_set.rb', line 292

def deep_each_setting(&block)
  thread_safe_access { settings.__deep_each_setting__(&block) }
end

#dig(*keys) ⇒ Object

Parameters:

  • keys (Array<String, Symbol>)

Returns:

  • (Object)

Since:

  • 0.2.0



220
221
222
# File 'lib/qonfig/data_set.rb', line 220

def dig(*keys)
  thread_safe_access { settings.__dig__(*keys) }
end

#dupQonfig::DataSet

Returns:

Since:

  • 0.17.0



339
340
341
342
343
344
345
# File 'lib/qonfig/data_set.rb', line 339

def dup
  thread_safe_definition do
    self.class.build.tap do |duplicate|
      duplicate.configure(to_h)
    end
  end
end

#each_setting(&block) {|setting_key, setting_value| ... } ⇒ Enumerable

Parameters:

  • block (Proc)

Yields:

  • (setting_key, setting_value)

Yield Parameters:

  • setting_key (String)
  • setting_value (Object)

Returns:

  • (Enumerable)

Since:

  • 0.13.0



279
280
281
# File 'lib/qonfig/data_set.rb', line 279

def each_setting(&block)
  thread_safe_access { settings.__each_setting__(&block) }
end

#freeze!void

This method returns an undefined value.

Since:

  • 0.1.0



51
52
53
# File 'lib/qonfig/data_set.rb', line 51

def freeze!
  thread_safe_access { settings.__freeze__ }
end

#frozen?void

This method returns an undefined value.

Since:

  • 0.2.0



59
60
61
# File 'lib/qonfig/data_set.rb', line 59

def frozen?
  thread_safe_access { settings.__is_frozen__ }
end

#key?(*key_path) ⇒ Boolean Also known as: option?, setting?

Parameters:

  • key_path (Array<String, Symbol>)

Returns:

  • (Boolean)

Since:

  • 0.17.0



256
257
258
# File 'lib/qonfig/data_set.rb', line 256

def key?(*key_path)
  thread_safe_access { settings.__has_key__(*key_path) }
end

#load_from_file(file_path, format: :dynamic, strict: true, expose: nil) ⇒ void

This method returns an undefined value.

Parameters:

  • file_path (String, Symbol)
  • format (Hash) (defaults to: :dynamic)

    a customizable set of options

  • strict (Hash) (defaults to: true)

    a customizable set of options

  • expose (Hash) (defaults to: nil)

    a customizable set of options

Options Hash (format:):

  • (String, Symbol)

Options Hash (strict:):

  • (Boolean)

Options Hash (expose:):

  • Environment (NilClass, String, Symbol)

    key

See Also:

  • #load_setting_values_from_file

Since:

  • 0.17.0



88
89
90
91
92
# File 'lib/qonfig/data_set.rb', line 88

def load_from_file(file_path, format: :dynamic, strict: true, expose: nil)
  thread_safe_access do
    load_setting_values_from_file(file_path, format: format, strict: strict, expose: expose)
  end
end

#load_from_json(file_path, strict: true, expose: nil) ⇒ void

This method returns an undefined value.

Parameters:

  • file_path (String)
  • strict (Hash) (defaults to: true)

    a customizable set of options

  • expose (Hash) (defaults to: nil)

    a customizable set of options

Options Hash (strict:):

  • (Boolean)

Options Hash (expose:):

  • Environment (NilClass, String, Symbol)

    key

See Also:

Since:

  • 0.17.0



116
117
118
# File 'lib/qonfig/data_set.rb', line 116

def load_from_json(file_path, strict: true, expose: nil)
  load_from_file(file_path, format: :json, strict: strict, expose: expose)
end

#load_from_self(format: :dynamic, strict: true, expose: nil) ⇒ void

This method returns an undefined value.

Parameters:

  • format (Hash) (defaults to: :dynamic)

    a customizable set of options

  • strict (Hash) (defaults to: true)

    a customizable set of options

  • expose (Hash) (defaults to: nil)

    a customizable set of options

Options Hash (format:):

  • (String, Symbol)

Options Hash (strict:):

  • (Boolean)

Options Hash (expose:):

  • (NilClass, String, Symbol)

Since:

  • 0.17.0



127
128
129
130
131
132
133
134
135
# File 'lib/qonfig/data_set.rb', line 127

def load_from_self(format: :dynamic, strict: true, expose: nil)
  caller_location = caller(1, 1).first

  thread_safe_access do
    load_setting_values_from_file(
      :self, format: format, strict: strict, expose: expose, caller_location: caller_location
    )
  end
end

#load_from_toml(file_path, strict: true, expose: nil) ⇒ void

This method returns an undefined value.

Parameters:

  • file_path (String)
  • strict (Hash) (defaults to: true)

    a customizable set of options

  • expose (Hash) (defaults to: nil)

    a customizable set of options

Options Hash (strict:):

  • (Boolean)

Options Hash (expose:):

  • Environment (NilClass, String, Symbol)

    key

See Also:

Since:

  • 0.17.0



29
30
31
# File 'lib/qonfig/plugins/toml/data_set.rb', line 29

def load_from_toml(file_path, strict: true, expose: nil)
  load_from_file(file_path, format: :toml, strict: strict, expose: expose)
end

#load_from_yaml(file_path, strict: true, expose: nil) ⇒ void

This method returns an undefined value.

Parameters:

  • file_path (String)
  • strict (Hash) (defaults to: true)

    a customizable set of options

  • expose (Hash) (defaults to: nil)

    a customizable set of options

Options Hash (strict:):

  • (Boolean)

Options Hash (expose:):

  • Environment (NilClass, String, Symbol)

    key

See Also:

Since:

  • 0.17.0



103
104
105
# File 'lib/qonfig/data_set.rb', line 103

def load_from_yaml(file_path, strict: true, expose: nil)
  load_from_file(file_path, format: :yml, strict: strict, expose: expose)
end

#reload!(settings_map = {}, &configurations) ⇒ void

This method returns an undefined value.

Parameters:

  • settings_map (Hash) (defaults to: {})
  • configurations (Proc)

Raises:

Since:

  • 0.2.0



71
72
73
74
75
76
# File 'lib/qonfig/data_set.rb', line 71

def reload!(settings_map = {}, &configurations)
  thread_safe_definition do
    raise Qonfig::FrozenSettingsError, 'Frozen config can not be reloaded' if frozen?
    load!(settings_map, &configurations)
  end
end

#save_to_json(path:, options: Qonfig::Uploaders::JSON::DEFAULT_OPTIONS, &value_processor) ⇒ void Also known as: dump_to_json

This method returns an undefined value.

Parameters:

  • value_processor (Block)
  • path (Hash)

    a customizable set of options

  • options (Hash) (defaults to: Qonfig::Uploaders::JSON::DEFAULT_OPTIONS)

    a customizable set of options

Options Hash (path:):

  • (String)

Options Hash (options:):

  • Native (Hash<Symbol|String,Any>) — default: ruby-stdlib

    ::JSON#generate attributes

Since:

  • 0.11.0



174
175
176
177
178
# File 'lib/qonfig/data_set.rb', line 174

def save_to_json(path:, options: Qonfig::Uploaders::JSON::DEFAULT_OPTIONS, &value_processor)
  thread_safe_access do
    Qonfig::Uploaders::JSON.upload(settings, path: path, options: options, &value_processor)
  end
end

#save_to_toml(path:, options: Qonfig::Uploaders::TOML::DEFAULT_OPTIONS, &value_processor) ⇒ void Also known as: dump_to_toml

This method returns an undefined value.

Parameters:

  • value_processor (Block)
  • path (Hash)

    a customizable set of options

  • options (Hash) (defaults to: Qonfig::Uploaders::TOML::DEFAULT_OPTIONS)

    a customizable set of options

Options Hash (path:):

  • (String)

Options Hash (options:):

  • Nothing, (Hash<Symbol,Any>)

    just for compatability and consistency

Since:

  • 0.12.0



13
14
15
16
17
# File 'lib/qonfig/plugins/toml/data_set.rb', line 13

def save_to_toml(path:, options: Qonfig::Uploaders::TOML::DEFAULT_OPTIONS, &value_processor)
  thread_safe_access do
    Qonfig::Uploaders::TOML.upload(settings, path: path, options: options, &value_processor)
  end
end

#save_to_yaml(path:, symbolize_keys: false, options: Qonfig::Uploaders::YAML::DEFAULT_OPTIONS, &value_processor) ⇒ void Also known as: dump_to_yaml

This method returns an undefined value.

Parameters:

  • value_processor (Block)
  • path (Hash)

    a customizable set of options

  • symbolize_keys (Hash) (defaults to: false)

    a customizable set of options

  • options (Hash) (defaults to: Qonfig::Uploaders::YAML::DEFAULT_OPTIONS)

    a customizable set of options

Options Hash (path:):

  • (String)

Options Hash (symbolize_keys:):

  • (Boolean)

Options Hash (options:):

  • Native (Hash<Symbol|String,Any>) — default: ruby-stdlib

    ::YAML#dump attributes

Since:

  • 0.11.0



189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# File 'lib/qonfig/data_set.rb', line 189

def save_to_yaml(
  path:,
  symbolize_keys: false,
  options: Qonfig::Uploaders::YAML::DEFAULT_OPTIONS,
  &value_processor
)
  thread_safe_access do
    Qonfig::Uploaders::YAML.upload(
      settings,
      path: path,
      options: options.merge(symbolize_keys: symbolize_keys),
      &value_processor
    )
  end
end

#slice(*keys) ⇒ Hash

Parameters:

  • keys (Array<String, Symbol>)

Returns:

  • (Hash)

Since:

  • 0.9.0



229
230
231
# File 'lib/qonfig/data_set.rb', line 229

def slice(*keys)
  thread_safe_access { settings.__slice__(*keys) }
end

#slice_value(*keys) ⇒ Hash, Any

Parameters:

  • keys (Array<String, Symbol>)

Returns:

  • (Hash, Any)

Since:

  • 0.10.0



238
239
240
# File 'lib/qonfig/data_set.rb', line 238

def slice_value(*keys)
  thread_safe_access { settings.__slice_value__(*keys) }
end

#subset(*keys) ⇒ Hash

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Parameters:

  • keys (Array<String, Symbol, Array<String, Symbol>>)

Returns:

  • (Hash)

Since:

  • 0.16.0



247
248
249
# File 'lib/qonfig/data_set.rb', line 247

def subset(*keys)
  thread_safe_access { settings.__subset__(*keys) }
end

#to_h(key_transformer: Qonfig::Settings::BASIC_SETTING_KEY_TRANSFORMER, value_transformer: Qonfig::Settings::BASIC_SETTING_VALUE_TRANSFORMER) ⇒ Hash Also known as: to_hash

Parameters:

  • key_transformer (Hash) (defaults to: Qonfig::Settings::BASIC_SETTING_KEY_TRANSFORMER)

    a customizable set of options

  • value_transformer (Hash) (defaults to: Qonfig::Settings::BASIC_SETTING_VALUE_TRANSFORMER)

    a customizable set of options

Options Hash (key_transformer:):

  • (Proc)

Options Hash (value_transformer:):

  • (Proc)

Returns:

  • (Hash)

Since:

  • 0.1.0



154
155
156
157
158
159
160
161
162
163
164
# File 'lib/qonfig/data_set.rb', line 154

def to_h(
  key_transformer: Qonfig::Settings::BASIC_SETTING_KEY_TRANSFORMER,
  value_transformer: Qonfig::Settings::BASIC_SETTING_VALUE_TRANSFORMER
)
  thread_safe_access do
    settings.__to_hash__(
      transform_key: key_transformer,
      transform_value: value_transformer
    )
  end
end

#valid?Boolean

Returns:

  • (Boolean)

Since:

  • 0.13.0



300
301
302
# File 'lib/qonfig/data_set.rb', line 300

def valid?
  thread_safe_access { validator.valid? }
end

#validate!void

This method returns an undefined value.

Since:

  • 0.13.0



308
309
310
# File 'lib/qonfig/data_set.rb', line 308

def validate!
  thread_safe_access { validator.validate! }
end

#with(temporary_configurations = {}, &arbitary_code) ⇒ void

This method returns an undefined value.

Parameters:

  • temporary_configurations (Hash<Symbol|String,Any>) (defaults to: {})
  • arbitary_code (Block)

Since:

  • 0.17.0



318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
# File 'lib/qonfig/data_set.rb', line 318

def with(temporary_configurations = {}, &arbitary_code)
  with_arbitary_access do
    begin
      original_settings = @settings

      temporary_settings = self.class.build.dup.tap do |copied_config|
        copied_config.configure(temporary_configurations)
      end.settings

      @settings = temporary_settings
      yield if block_given?
    ensure
      @settings = original_settings
    end
  end
end