Sha256: f5a6aa5a7238bbf5a00d1b338fe9ab0c68fb70c31ecf30745c05b7a01f37490b

Contents?: true

Size: 1.07 KB

Versions: 2

Compression:

Stored size: 1.07 KB

Contents

# frozen_string_literal: true

class BCDD::Result
  class Config
    module ConstantAlias
      MAPPING = {
        'Result' => { target: ::Object, name: :Result, value: ::BCDD::Result },
        'Context' => { target: ::Object, name: :Context, value: ::BCDD::Result::Context },
        'BCDD::Context' => { target: ::BCDD, name: :Context, value: ::BCDD::Result::Context }
      }.transform_values!(&:freeze).freeze

      OPTIONS = MAPPING.to_h do |option_name, mapping|
        affects = mapping.fetch(:target).name.freeze

        [option_name, { default: false, affects: [affects].freeze }]
      end.freeze

      Listener = ->(option_name, boolean) do
        mapping = MAPPING.fetch(option_name)

        target, name, value = mapping.fetch_values(:target, :name, :value)

        defined = target.const_defined?(name, false)

        boolean ? defined || target.const_set(name, value) : defined && target.send(:remove_const, name)
      end

      def self.switcher
        Switcher.new(options: OPTIONS, listener: Listener)
      end
    end

    private_constant :ConstantAlias
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
bcdd-result-0.9.1 lib/bcdd/result/config/constant_alias.rb
bcdd-result-0.9.0 lib/bcdd/result/config/constant_alias.rb