Class: Qonfig::DataSet

Inherits:
Object
  • Object
show all
Extended by:
DSL
Includes:
Plugins::PrettyPrint::DataSetMixin
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

Version:

  • 0.21.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_vault, expose_yaml, extended, freeze_state!, instance_commands, load_from_env, load_from_vault, predefined_validators, re_setting, setting, validate, validators, values_file

Methods included from Plugins::PrettyPrint::DataSetMixin

#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



64
65
66
67
# File 'lib/qonfig/data_set.rb', line 64

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



57
58
59
# File 'lib/qonfig/data_set.rb', line 57

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

.build_compacted(base_dataset_klass = self, &config_klass_definitions) ⇒ Qonfig::Compacted

Parameters:

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

Returns:

Since:

  • 0.21.0



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

def build_compacted(base_dataset_klass = self, &config_klass_definitions)
  build(base_dataset_klass, &config_klass_definitions).compacted
end

.valid_with?(settings_map = {}, &configurations) ⇒ Boolean

Parameters:

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

Returns:

  • (Boolean)

Since:

  • 0.19.0

Version:

  • 0.21.0



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

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

Instance Method Details

#[](key) ⇒ Object

Parameters:

  • key (String, Symbol)

Returns:

  • (Object)

Since:

  • 0.2.0



253
254
255
# File 'lib/qonfig/data_set.rb', line 253

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

#[]=(key, value) ⇒ void

This method returns an undefined value.

Parameters:

  • key (String, Symbol)
  • value (Any)

Raises:

Since:

  • 0.21.0



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

def []=(key, value)
  thread_safe_access { settings[key] = value }
end

#clear!void

This method returns an undefined value.

Since:

  • 0.2.0



322
323
324
# File 'lib/qonfig/data_set.rb', line 322

def clear!
  thread_safe_access { settings.__clear__ }
end

#compactedQonfig::Compacted

Returns:

Since:

  • 0.21.0



480
481
482
# File 'lib/qonfig/data_set.rb', line 480

def compacted
  Qonfig::Compacted.build_from(self)
end

#configure(settings_map = {}) {|cofnig| ... } ⇒ void

This method returns an undefined value.

Parameters:

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

Yields:

  • (cofnig)

Yield Parameters:

Since:

  • 0.1.0



181
182
183
184
185
# File 'lib/qonfig/data_set.rb', line 181

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



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

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



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

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

#dupQonfig::DataSet

Returns:

Since:

  • 0.17.0



437
438
439
440
441
442
443
# File 'lib/qonfig/data_set.rb', line 437

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



335
336
337
# File 'lib/qonfig/data_set.rb', line 335

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: Qonfig::Imports::Abstract::DEFAULT_RAW_BEHAVIOR, prefix: Qonfig::Imports::Abstract::EMPTY_PREFIX, accessor: Qonfig::Imports::Abstract::AS_ACCESSOR) ⇒ 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: Qonfig::Imports::Abstract::DEFAULT_RAW_BEHAVIOR)

    a customizable set of options

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

    a customizable set of options

  • accessor (Hash) (defaults to: Qonfig::Imports::Abstract::AS_ACCESSOR)

    a customizable set of options

Options Hash (mappings:):

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

Options Hash (raw:):

  • (Boolean)

Options Hash (prefix:):

  • (String, Symbol)

Options Hash (accessor:):

  • (Boolean)

Since:

  • 0.18.0



455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
# File 'lib/qonfig/data_set.rb', line 455

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

#freeze!void

This method returns an undefined value.

Since:

  • 0.1.0



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

def freeze!
  thread_safe_access { settings.__freeze__ }
end

#frozen?void

This method returns an undefined value.

Since:

  • 0.2.0



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

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



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

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



396
397
398
399
400
# File 'lib/qonfig/data_set.rb', line 396

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, &configurations) ⇒ void

This method returns an undefined value.

Parameters:

  • file_path (String, Symbol, Pathname)
  • configurations (Block)
  • 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

Version:

  • 0.22.0



112
113
114
115
116
117
118
# File 'lib/qonfig/data_set.rb', line 112

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

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

This method returns an undefined value.

Parameters:

  • file_path (String, Symbol, Pathname)
  • configurations (Block)
  • 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

Version:

  • 0.22.0



146
147
148
# File 'lib/qonfig/data_set.rb', line 146

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

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

This method returns an undefined value.

Parameters:

  • configurations (Block)
  • 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

Version:

  • 0.21.0



159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/qonfig/data_set.rb', line 159

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

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

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

This method returns an undefined value.

Parameters:

  • file_path (String, Pathmame)
  • configuration (Block)
  • 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

Version:

  • 0.21.0



31
32
33
# File 'lib/qonfig/plugins/toml/data_set.rb', line 31

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

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

This method returns an undefined value.

Parameters:

  • file_path (String, Symbol, Pathname)
  • configurations (Block)
  • 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

Version:

  • 0.22.0



131
132
133
# File 'lib/qonfig/data_set.rb', line 131

def load_from_yaml(file_path, strict: true, expose: nil, &configurations)
  load_from_file(file_path, format: :yml, strict: strict, expose: expose, &configurations)
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



93
94
95
96
97
98
# File 'lib/qonfig/data_set.rb', line 93

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



406
407
408
# File 'lib/qonfig/data_set.rb', line 406

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, Pathname)

Options Hash (options:):

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

    ::JSON#generate attributes

Since:

  • 0.11.0



216
217
218
219
220
# File 'lib/qonfig/data_set.rb', line 216

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, Pathname)

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, Pathname)

Options Hash (symbolize_keys:):

  • (Boolean)

Options Hash (options:):

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

    ::YAML#dump attributes

Since:

  • 0.11.0



231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
# File 'lib/qonfig/data_set.rb', line 231

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



285
286
287
# File 'lib/qonfig/data_set.rb', line 285

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



294
295
296
# File 'lib/qonfig/data_set.rb', line 294

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



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

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

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

Parameters:

  • dot_style (Hash) (defaults to: Qonfig::Settings::REPRESENT_HASH_IN_DOT_STYLE)

    a customizable set of options

  • 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 (dot_style:):

  • (Boolean)

Options Hash (key_transformer:):

  • (Proc)

Options Hash (value_transformer:):

  • (Proc)

Returns:

  • (Hash)

Since:

  • 0.1.0



194
195
196
197
198
199
200
201
202
203
204
205
206
# File 'lib/qonfig/data_set.rb', line 194

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

#valid?Boolean

Returns:

  • (Boolean)

Since:

  • 0.13.0



357
358
359
# File 'lib/qonfig/data_set.rb', line 357

def valid?
  thread_safe_access { validator.valid? }
end

#valid_with?(settings_map = {}, &configurations) ⇒ Boolean

Parameters:

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

Returns:

  • (Boolean)

Since:

  • 0.19.0

Version:

  • 0.21.0



367
368
369
370
371
372
373
374
375
376
377
378
379
380
# File 'lib/qonfig/data_set.rb', line 367

def valid_with?(settings_map = {}, &configurations)
  # NOTE:
  #  'dup.configure(settings_map)' has better thread-safety than 'with(settings_map)'
  #  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(settings_map, &configurations)
  true
rescue Qonfig::ValidationError
  false
end

#validate!void

This method returns an undefined value.

Since:

  • 0.13.0



386
387
388
# File 'lib/qonfig/data_set.rb', line 386

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



416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
# File 'lib/qonfig/data_set.rb', line 416

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