Class: Qonfig::Imports::DirectKey Private

Inherits:
Abstract
  • Object
show all
Defined in:
lib/qonfig/imports/direct_key.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.18.0

Constant Summary

Constants inherited from Abstract

Abstract::AS_ACCESSOR, Abstract::DEFAULT_RAW_BEHAVIOR, Abstract::EMPTY_PREFIX

Instance Method Summary collapse

Constructor Details

#initialize(seeded_klass, imported_config, *keys, prefix: EMPTY_PREFIX, raw: DEFAULT_RAW_BEHAVIOR, accessor: AS_ACCESSOR) ⇒ 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.

Parameters:

  • seeded_klass (Class)
  • imported_config (Qonfig::DataSet)
  • keys (Array<String,Symbol>)
  • prefix (Hash) (defaults to: EMPTY_PREFIX)

    a customizable set of options

  • raw (Hash) (defaults to: DEFAULT_RAW_BEHAVIOR)

    a customizable set of options

  • accessor (Hash) (defaults to: AS_ACCESSOR)

    a customizable set of options

Options Hash (prefix:):

  • (String, Symbol)

Options Hash (raw:):

  • (Boolean)

Options Hash (accessor:):

  • (Boolean)

Since:

  • 0.18.0

Version:

  • 0.21.0



17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/qonfig/imports/direct_key.rb', line 17

def initialize(
  seeded_klass,
  imported_config,
  *keys,
  prefix: EMPTY_PREFIX,
  raw: DEFAULT_RAW_BEHAVIOR,
  accessor: AS_ACCESSOR
)
  prevent_incompatible_import_params!(imported_config, prefix, keys)
  super(seeded_klass, imported_config, prefix: prefix, raw: raw, accessor: accessor)
  @keys = keys
  @key_matchers = build_setting_key_matchers(keys)
end

Instance Method Details

#import!(settings_interface = Module.new) ⇒ 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.

rubocop:disable Metrics/AbcSize, Metrics/MethodLength, Metrics/BlockLength

Parameters:

  • settings_interfcae (Module)

Since:

  • 0.18.0

Version:

  • 0.21.0



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/qonfig/imports/direct_key.rb', line 38

def import!(settings_interface = Module.new)
  key_matchers.each do |key_matcher|
    raise(
      Qonfig::UnknownSettingError,
      "Setting with <#{key_matcher.scope_pattern}> key does not exist!"
    ) unless (imported_config.keys(all_variants: true).any? do |setting_key|
      key_matcher.match?(setting_key)
    end || key_matcher.generic?)

    imported_config.keys(all_variants: true).each do |setting_key|
      next unless key_matcher.match?(setting_key)

      setting_key_path_sequence = setting_key.split('.')
      access_method_name = setting_key_path_sequence.last
      access_method_name = "#{prefix}#{access_method_name}" unless prefix.empty?

      settings_interface.module_exec(
        raw, imported_config, accessor
      ) do |raw, imported_config, accessor|
        unless raw
          # NOTE: get setting value via slice_value
          define_method(access_method_name) do
            imported_config.slice_value(*setting_key_path_sequence)
          end
        else
          # NOTE: get setting object (concrete value or Qonfig::Settings object)
          define_method(access_method_name) do
            imported_config.dig(*setting_key_path_sequence)
          end
        end

        define_method("#{access_method_name}?") do
          # NOTE: based on Qonfig::Settings#__define_option_predicate__ realization
          !!imported_config[setting_key]
        end

        if accessor
          define_method("#{access_method_name}=") do |value|
            imported_config[setting_key] = value
          end
        end
      end
    end
  end
end