Class: Qonfig::Imports::Mappings Private

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

EMPTY_MAPPINGS =

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:

  • (Hash)

Since:

  • 0.18.0

{}.freeze

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, mappings: EMPTY_MAPPINGS, 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)
  • prefix (Hash) (defaults to: EMPTY_PREFIX)

    a customizable set of options

  • raw (Hash) (defaults to: DEFAULT_RAW_BEHAVIOR)

    a customizable set of options

  • mappings (Hash) (defaults to: EMPTY_MAPPINGS)

    a customizable set of options

  • accessor (Hash) (defaults to: AS_ACCESSOR)

    a customizable set of options

Options Hash (mappings:):

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

Options Hash (prefix:):

  • (String, Symbol)

Options Hash (raw:):

  • (Boolean)

Options Hash (accessor:):

  • (Boolean)

Since:

  • 0.18.0

Version:

  • 0.21.0



23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/qonfig/imports/mappings.rb', line 23

def initialize(
  seeded_klass,
  imported_config,
  mappings: EMPTY_MAPPINGS,
  prefix: EMPTY_PREFIX,
  raw: DEFAULT_RAW_BEHAVIOR,
  accessor: AS_ACCESSOR
)
  prevent_incompatible_import_params!(imported_config, prefix, mappings)
  super(seeded_klass, imported_config, prefix: prefix, raw: raw, accessor: accessor)
  @mappings = mappings
  @key_matchers = build_setting_key_matchers(mappings)
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 rubocop:disable Metrics/MethodLength rubocop:disable Metrics/BlockLength rubocop:disable Metrics/PerceivedComplexity

Parameters:

  • settings_interface (Module) (defaults to: Module.new)

Since:

  • 0.18.0

Version:

  • 0.21.0



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
83
84
85
86
87
88
89
90
# File 'lib/qonfig/imports/mappings.rb', line 47

def import!(settings_interface = Module.new) # rubocop:disable Metrics/AbcSize
  key_matchers.each_pair do |(mapped_method_name, 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('.')
      mapped_method_name = "#{prefix}#{mapped_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(mapped_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(mapped_method_name) do
            imported_config.dig(*setting_key_path_sequence)
          end
        end

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

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