Class: Qonfig::DataSet

Inherits:
Object
  • Object
show all
Extended by:
DSL
Includes:
Plugins::PrettyPrint::Mixin
Defined in:
lib/qonfig/data_set.rb,
lib/qonfig/plugins/toml/data_set.rb,
lib/qonfig/plugins/pretty_print/data_set.rb

Overview

Since:

  • 0.19.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, define_validator, definition_commands, expose_json, expose_self, expose_toml, expose_yaml, extended, freeze_state!, instance_commands, load_from_env, predefined_validators, re_setting, setting, validate, validators, values_file

Methods included from Plugins::PrettyPrint::Mixin

#pretty_print

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



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

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



45
46
47
# File 'lib/qonfig/data_set.rb', line 45

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



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

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

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

.valid_with?(configurations = {}) ⇒ Boolean

Parameters:

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

Returns:

  • (Boolean)

Since:

  • 0.19.0



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

def valid_with?(configurations = {})
  new(configurations)
  true
rescue Qonfig::ValidationError
  false
end

Instance Method Details

#[](key) ⇒ Object

Parameters:

  • key (String, Symbol)

Returns:

  • (Object)

Since:

  • 0.2.0



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

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

#clear!void

This method returns an undefined value.

Since:

  • 0.2.0



276
277
278
# File 'lib/qonfig/data_set.rb', line 276

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



152
153
154
155
156
# File 'lib/qonfig/data_set.rb', line 152

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

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

Parameters:

  • block (Proc)
  • yield_all (Hash) (defaults to: false)

    a customizable set of options

Options Hash (yield_all:):

  • (Boolean)

Yields:

  • (setting_key, setting_value)

Yield Parameters:

  • setting_key (String)
  • setting_value (Object)

Returns:

  • (Enumerable)

Since:

  • 0.13.0



303
304
305
# File 'lib/qonfig/data_set.rb', line 303

def deep_each_setting(yield_all: false, &block)
  thread_safe_access { settings.__deep_each_setting__(yield_all: yield_all, &block) }
end

#dig(*keys) ⇒ Object

Parameters:

  • keys (Array<String, Symbol>)

Returns:

  • (Object)

Since:

  • 0.2.0



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

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

#dupQonfig::DataSet

Returns:

Since:

  • 0.17.0



390
391
392
393
394
395
396
# File 'lib/qonfig/data_set.rb', line 390

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



289
290
291
# File 'lib/qonfig/data_set.rb', line 289

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

#export_settings(exportable_object, *exported_setting_keys, mappings: Qonfig::Imports::Mappings::EMPTY_MAPPINGS, raw: false, prefix: Qonfig::Imports::Abstract::EMPTY_PREFIX) ⇒ void

This method returns an undefined value.

Parameters:

  • exportable_object (Object)
  • exported_setting_keys (Array<String,Symbol>)
  • mappings (Hash) (defaults to: Qonfig::Imports::Mappings::EMPTY_MAPPINGS)

    a customizable set of options

  • raw (Hash) (defaults to: false)

    a customizable set of options

  • prefix (Hash) (defaults to: Qonfig::Imports::Abstract::EMPTY_PREFIX)

    a customizable set of options

Options Hash (mappings:):

  • (Hash<String|Symbol,String|Symbol>)

Options Hash (raw:):

  • (Boolean)

Options Hash (prefix:):

  • (String, Symbol)

Since:

  • 0.18.0



407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
# File 'lib/qonfig/data_set.rb', line 407

def export_settings(
  exportable_object,
  *exported_setting_keys,
  mappings: Qonfig::Imports::Mappings::EMPTY_MAPPINGS,
  raw: false,
  prefix: Qonfig::Imports::Abstract::EMPTY_PREFIX
)
  thread_safe_access do
    Qonfig::Imports::Export.export!(
      exportable_object,
      self,
      *exported_setting_keys,
      prefix: prefix,
      raw: raw,
      mappings: mappings
    )
  end
end

#freeze!void

This method returns an undefined value.

Since:

  • 0.1.0



61
62
63
# File 'lib/qonfig/data_set.rb', line 61

def freeze!
  thread_safe_access { settings.__freeze__ }
end

#frozen?void

This method returns an undefined value.

Since:

  • 0.2.0



69
70
71
# File 'lib/qonfig/data_set.rb', line 69

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



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

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

#keys(all_variants: false, only_root: false) ⇒ Array<String>

Parameters:

  • all_variants (Hash) (defaults to: false)

    a customizable set of options

  • only_root (Hash) (defaults to: false)

    a customizable set of options

Options Hash (all_variants:):

  • (Boolean)

Options Hash (only_root:):

  • (Boolean)

Returns:

  • (Array<String>)

Since:

  • 0.18.0



349
350
351
352
353
# File 'lib/qonfig/data_set.rb', line 349

def keys(all_variants: false, only_root: false)
  thread_safe_access do
    only_root ? settings.__root_keys__ : settings.__keys__(all_variants: all_variants)
  end
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



98
99
100
101
102
# File 'lib/qonfig/data_set.rb', line 98

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



126
127
128
# File 'lib/qonfig/data_set.rb', line 126

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



137
138
139
140
141
142
143
144
145
# File 'lib/qonfig/data_set.rb', line 137

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



113
114
115
# File 'lib/qonfig/data_set.rb', line 113

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



81
82
83
84
85
86
# File 'lib/qonfig/data_set.rb', line 81

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

#root_keysArray<String>

Returns:

  • (Array<String>)

Since:

  • 0.18.0



359
360
361
# File 'lib/qonfig/data_set.rb', line 359

def root_keys
  thread_safe_access { settings.__root_keys__ }
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



184
185
186
187
188
# File 'lib/qonfig/data_set.rb', line 184

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



199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
# File 'lib/qonfig/data_set.rb', line 199

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



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

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



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

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



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

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



164
165
166
167
168
169
170
171
172
173
174
# File 'lib/qonfig/data_set.rb', line 164

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



311
312
313
# File 'lib/qonfig/data_set.rb', line 311

def valid?
  thread_safe_access { validator.valid? }
end

#valid_with?(configurations = {}) ⇒ Boolean

Parameters:

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

Returns:

  • (Boolean)

Since:

  • 0.19.0



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

def valid_with?(configurations = {})
  # NOTE:
  #  'dup.configure(configurations)' has better thread-safety than 'with(configurations)'
  #  pros:
  #    - no arbitrary lock is obtained;
  #    - all threads can read and work :)
  #  cons:
  #    - useless ton of objects (new dataset, new settings, new locks, and etc);
  #    - useless setting options assignment steps (self.dup + self.to_h + configure(to_h))
  dup.configure(configurations)
  true
rescue Qonfig::ValidationError
  false
end

#validate!void

This method returns an undefined value.

Since:

  • 0.13.0



339
340
341
# File 'lib/qonfig/data_set.rb', line 339

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



369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
# File 'lib/qonfig/data_set.rb', line 369

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