Class: Qonfig::Settings Private

Inherits:
Object
  • Object
show all
Includes:
Plugins::PrettyPrint::SettingsMixin
Defined in:
lib/qonfig/settings.rb,
lib/qonfig/plugins/pretty_print/settings.rb

Overview

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

Since:

  • 0.21.0

Defined Under Namespace

Modules: Builder Classes: Callbacks, KeyGuard, KeyMatcher, Lock

Constant Summary collapse

BASIC_SETTING_KEY_TRANSFORMER =

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

Returns:

  • (Proc)

Since:

  • 0.11.0

proc { |value| value }.freeze
BASIC_SETTING_VALUE_TRANSFORMER =

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

Returns:

  • (Proc)

Since:

  • 0.11.0

proc { |value| value }.freeze
REPRESENT_HASH_IN_DOT_STYLE =

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

Returns:

  • (Boolean)

Since:

  • 0.25.0

false
DOT_NOTATION_SEPARATOR =

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

Returns:

  • (String)

Since:

  • 0.19.0

'.'

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Plugins::PrettyPrint::SettingsMixin

#pretty_print

Constructor Details

#initialize(__mutation_callbacks__) ⇒ Settings

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.

Returns a new instance of Settings.

Since:

  • 0.1.0

Version:

  • 0.31.0



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/qonfig/settings.rb', line 52

def initialize(__mutation_callbacks__)
  @__options__ = {}
  @__lock__ = Lock.new
  @__mutation_callbacks__ = __mutation_callbacks__
  @__deep_access_cache__ = {}

  # NOTE:
  #   Register a cache invalidator inside our own mutation callbacks.
  #   Nested settings add their parent's callbacks object to their own callbacks
  #   (see #__store_setting_value__), so a mutation of any nested config propagates
  #   upwards through the chain and drops the cached digging results on EACH config
  #   instance along the way ("matryoshka" cache invalidation).
  #
  #   It is prepended so that it runs BEFORE the validation callback — full
  #   data-set validators read setting values back through #__dig__, so the
  #   cache must already be dropped by the time they run.
  @__mutation_callbacks__.prepend(proc { __invalidate_deep_access_cache__ })
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *arguments, &block) ⇒ void

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.

This method returns an undefined value.

Parameters:

  • method_name (String, Symbol)
  • arguments (Array<Object>)
  • block (Proc)

Raises:

Since:

  • 0.1.0



315
316
317
318
319
# File 'lib/qonfig/settings.rb', line 315

def method_missing(method_name, *arguments, &block)
  super
rescue NoMethodError
  ::Kernel.raise(Qonfig::UnknownSettingError, "Setting with <#{method_name}> key doesnt exist!")
end

Instance Attribute Details

#__mutation_callbacks__Qonfig::Settings::Callbacks (readonly)

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.

Returns:

Since:

  • 0.13.0



47
48
49
# File 'lib/qonfig/settings.rb', line 47

def __mutation_callbacks__
  @__mutation_callbacks__
end

#__options__Hash (readonly)

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.

Returns:

  • (Hash)

Since:

  • 0.1.0



41
42
43
# File 'lib/qonfig/settings.rb', line 41

def __options__
  @__options__
end

Instance Method Details

#[](*keys) ⇒ Object

Parameters:

  • key (Symbol, String)

Returns:

  • (Object)

Raises:

Since:

  • 0.1.0

Version:

  • 0.25.0



163
164
165
# File 'lib/qonfig/settings.rb', line 163

def [](*keys)
  __dig__(*keys)
end

#[]=(key, value) ⇒ void

This method returns an undefined value.

Parameters:

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

Since:

  • 0.1.0

Version:

  • 0.21.0



174
175
176
# File 'lib/qonfig/settings.rb', line 174

def []=(key, value)
  __lock__.thread_safe_access { __assign_value__(key, value) }
end

#__append_settings__(settings, with_redefinition: false) ⇒ void

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.

This method returns an undefined value.

Parameters:

  • settings (Qonfig::Settings)
  • with_redefinition (Hash) (defaults to: false)

    a customizable set of options

Options Hash (with_redefinition:):

  • (Boolean)

Since:

  • 0.1.0

Version:

  • 0.29.0



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

def __append_settings__(settings, with_redefinition: false)
  __lock__.thread_safe_merge do
    settings.__options__.each_pair do |key, value|
      __define_setting__(key, value, with_redefinition: with_redefinition)
    end
  end
end

#__apply_values__(settings_map) ⇒ void

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.

This method returns an undefined value.

Parameters:

  • settings_map (Hash)

Since:

  • 0.3.0



183
184
185
# File 'lib/qonfig/settings.rb', line 183

def __apply_values__(settings_map)
  __lock__.thread_safe_access { __set_values_from_map__(settings_map) }
end

#__clear__void

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.

This method returns an undefined value.

Since:

  • 0.2.0



302
303
304
# File 'lib/qonfig/settings.rb', line 302

def __clear__
  __lock__.thread_safe_access { __clear_option_values__ }
end

#__deep_each_setting__(initial_setting_key = nil, yield_all: false, &block) {|key, value| ... } ⇒ Enumerable

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:

  • initial_setting_key (String, NilClass) (defaults to: nil)
  • block (Proc)
  • yield_all (Hash) (defaults to: false)

    a customizable set of options

Options Hash (yield_all:):

  • (Boolean)

Yields:

  • (key, value)

Yield Parameters:

  • key (String)
  • value (Object)

Returns:

  • (Enumerable)

Since:

  • 0.13.0



97
98
99
100
101
# File 'lib/qonfig/settings.rb', line 97

def __deep_each_setting__(initial_setting_key = nil, yield_all: false, &block)
  __lock__.thread_safe_access do
    __deep_each_key_value_pair__(initial_setting_key, yield_all: yield_all, &block)
  end
end

#__define_setting__(key, value, with_redefinition: false) ⇒ void

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.

This method returns an undefined value.

Parameters:

  • key (Symbol, String)
  • value (Object)
  • with_redefinition (Hash) (defaults to: false)

    a customizable set of options

Options Hash (with_redefinition:):

  • (Boolean)

Since:

  • 0.1.0

Version:

  • 0.31.0



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/qonfig/settings.rb', line 111

def __define_setting__(key, value, with_redefinition: false) # rubocop:disable Metrics/AbcSize
  __lock__.thread_safe_definition do
    key = __indifferently_accessable_option_key__(key)

    __prevent_core_method_intersection__(key)

    case
    when with_redefinition || !__options__.key?(key)
      __store_setting_value__(key, value)
    when __is_a_setting__(__options__[key]) && __is_a_setting__(value)
      __options__[key].__append_settings__(value)
    else
      __store_setting_value__(key, value)
    end

    __define_option_reader__(key)
    __define_option_writer__(key)
    __define_option_predicate__(key)
  end
end

#__dig__(*keys) ⇒ Object

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

Returns:

  • (Object)

Since:

  • 0.2.0

Version:

  • 0.31.0



193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'lib/qonfig/settings.rb', line 193

def __dig__(*keys)
  __lock__.thread_safe_access do
    # NOTE:
    #   Resolving a dot-notated/nested key (config['a.b.c.d']) is expensive:
    #   it splits and re-joins string fragments and digs through every nested
    #   config instance on each access. We memoize the resolved value per key
    #   set; the cache is dropped on any mutation (see __invalidate...).
    cache = @__deep_access_cache__
    cache_key = __deep_access_cache_key__(keys)

    if cache.key?(cache_key)
      cache[cache_key]
    else
      __resolve_deep_access__(*keys).tap { |value| cache[cache_key] = value }
    end
  end
end

#__each_setting__(&block) {|key, value| ... } ⇒ Enumerable

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:

  • block (Proc)

Yields:

  • (key, value)

Yield Parameters:

  • key (String)
  • value (Object)

Returns:

  • (Enumerable)

Since:

  • 0.13.0



80
81
82
83
84
# File 'lib/qonfig/settings.rb', line 80

def __each_setting__(&block)
  __lock__.thread_safe_access do
    __each_key_value_pair__(&block)
  end
end

#__freeze__void

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.

This method returns an undefined value.

Since:

  • 0.1.0



335
336
337
338
339
340
341
342
343
# File 'lib/qonfig/settings.rb', line 335

def __freeze__
  __lock__.thread_safe_access do
    __options__.freeze

    __options__.each_value do |value|
      value.__freeze__ if __is_a_setting__(value)
    end
  end
end

#__has_key__(*key_path) ⇒ Boolean

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:

  • key_path (Array<Symbol, String>)

Returns:

  • (Boolean)

Since:

  • 0.17.0



367
368
369
# File 'lib/qonfig/settings.rb', line 367

def __has_key__(*key_path)
  __lock__.thread_safe_access { __is_key_exists__(*key_path) }
end

#__invoke_mutation_callbacks__void

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.

This method returns an undefined value.

Since:

  • 0.13.0



151
152
153
# File 'lib/qonfig/settings.rb', line 151

def __invoke_mutation_callbacks__
  __mutation_callbacks__.call
end

#__is_a_setting__(value) ⇒ Boolean

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:

  • value (Any)

Returns:

  • (Boolean)

Since:

  • 0.13.0



358
359
360
# File 'lib/qonfig/settings.rb', line 358

def __is_a_setting__(value)
  value.is_a?(Qonfig::Settings)
end

#__is_frozen__Boolean

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.

Returns:

  • (Boolean)

Since:

  • 0.2.0



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

def __is_frozen__
  __lock__.thread_safe_access { __options__.frozen? }
end

#__keys__(all_variants: false) ⇒ Array<String>

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:

  • all_variants (Hash) (defaults to: false)

    a customizable set of options

Options Hash (all_variants:):

  • (Boolean)

Returns:

  • (Array<String>)

Since:

  • 0.18.0



286
287
288
# File 'lib/qonfig/settings.rb', line 286

def __keys__(all_variants: false)
  __lock__.thread_safe_access { __setting_keys__(all_variants: all_variants) }
end

#__root_keys__Array<String>

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.

Returns:

  • (Array<String>)

Since:

  • 0.18.0



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

def __root_keys__
  __lock__.thread_safe_access { __root_setting_keys__ }
end

#__slice__(*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>)

Returns:

  • (Hash)

Since:

  • 0.9.0



216
217
218
# File 'lib/qonfig/settings.rb', line 216

def __slice__(*keys)
  __lock__.thread_safe_access { __deep_slice__(*keys) }
end

#__slice_value__(*keys) ⇒ Hash, Any

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

Returns:

  • (Hash, Any)

Since:

  • 0.10.0



225
226
227
# File 'lib/qonfig/settings.rb', line 225

def __slice_value__(*keys)
  __lock__.thread_safe_access { __deep_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



234
235
236
# File 'lib/qonfig/settings.rb', line 234

def __subset__(*keys)
  __lock__.thread_safe_access { __deep_subset__(*keys) }
end

#__to_hash__(dot_notation: REPRESENT_HASH_IN_DOT_STYLE, transform_key: BASIC_SETTING_KEY_TRANSFORMER, transform_value: BASIC_SETTING_VALUE_TRANSFORMER) ⇒ Hash Also known as: __to_h__

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:

  • dot_notation (Hash) (defaults to: REPRESENT_HASH_IN_DOT_STYLE)

    a customizable set of options

  • transform_key (Hash) (defaults to: BASIC_SETTING_KEY_TRANSFORMER)

    a customizable set of options

  • transform_value (Hash) (defaults to: BASIC_SETTING_VALUE_TRANSFORMER)

    a customizable set of options

Options Hash (dot_notation:):

  • (Boolean)

Options Hash (transform_key:):

  • (Proc)

Options Hash (transform_value:):

  • (Proc)

Returns:

  • (Hash)

Since:

  • 0.1.0

Version:

  • 0.25.0



246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
# File 'lib/qonfig/settings.rb', line 246

def __to_hash__(
  dot_notation: REPRESENT_HASH_IN_DOT_STYLE,
  transform_key: BASIC_SETTING_KEY_TRANSFORMER,
  transform_value: BASIC_SETTING_VALUE_TRANSFORMER
)
  unless transform_key.is_a?(Proc)
    ::Kernel.raise(
      Qonfig::IncorrectKeyTransformerError,
      'Key transformer should be a type of proc'
    )
  end

  unless transform_value.is_a?(Proc)
    ::Kernel.raise(
      Qonfig::IncorrectValueTransformerError,
      'Value transformer should be a type of proc'
    )
  end

  __lock__.thread_safe_access do
    if dot_notation
      __build_dot_notated_hash_representation__(
        transform_key: transform_key,
        transform_value: transform_value
      )
    else
      __build_basic_hash_representation__(
        transform_key: transform_key,
        transform_value: transform_value
      )
    end
  end
end

#respond_to_missing?(method_name, include_private = false) ⇒ Boolean

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.

Returns:

  • (Boolean)

Since:

  • 0.1.0



325
326
327
328
329
# File 'lib/qonfig/settings.rb', line 325

def respond_to_missing?(method_name, include_private = false)
  # :nocov:
  __options__.key?(method_name.to_s) || __options__.key?(method_name.to_sym) || super
  # :nocov:
end