Sha256: 6b4ef73b873fe2bc28b92b684caa9ce545ff75738b0c582c16abc0c259c7487e

Contents?: true

Size: 1.6 KB

Versions: 18

Compression:

Stored size: 1.6 KB

Contents

# frozen_string_literal: true

module LedgerSync
  class LedgerConfigurationStore
    include Enumerable

    attr_reader :configs, :inflections, :base_module_to_config_mapping

    def initialize
      @keys = []
      @base_module_to_config_mapping = {}
      @configs = {}
      @inflections = []
      @class_configs = {}
    end

    def add_alias(client_key, existing_config)
      if respond_to?(client_key)
        raise "Alias already taken: #{client_key}" if send(client_key) != existing_config

        return
      end

      _instance_methods_for(client_key: client_key, ledger_config: existing_config)
    end

    def config_from_class(client_class:)
      @class_configs.fetch(client_class)
    end

    def config_from_base_module(base_module:)
      @base_module_to_config_mapping.fetch(base_module, nil)
    end

    def each(&block)
      configs.each(&block)
    end

    def find(&block)
      configs.values.find(&block)
    end

    def register_ledger(ledger_config:)
      @base_module_to_config_mapping[ledger_config.base_module] = ledger_config

      _instance_methods_for(
        client_key: ledger_config.root_key,
        ledger_config: ledger_config
      )
    end

    private

    def _instance_methods_for(client_key:, ledger_config:)
      @keys << client_key.to_sym

      @configs[client_key] = ledger_config
      @class_configs[ledger_config.client_class] = ledger_config

      instance_variable_set(
        "@#{client_key}",
        ledger_config
      )

      @inflections |= ledger_config.base_module.name.split('::')
      self.class.class_eval { attr_reader client_key }
    end
  end
end

Version data entries

18 entries across 18 versions & 1 rubygems

Version Path
ledger_sync-2.6.0 lib/ledger_sync/ledger_configuration_store.rb
ledger_sync-2.5.0 lib/ledger_sync/ledger_configuration_store.rb
ledger_sync-2.4.4 lib/ledger_sync/ledger_configuration_store.rb
ledger_sync-2.3.1 lib/ledger_sync/ledger_configuration_store.rb
ledger_sync-2.2.3 lib/ledger_sync/ledger_configuration_store.rb
ledger_sync-2.2.1 lib/ledger_sync/ledger_configuration_store.rb
ledger_sync-2.2.0 lib/ledger_sync/ledger_configuration_store.rb
ledger_sync-2.0.2 lib/ledger_sync/ledger_configuration_store.rb
ledger_sync-2.0.1 lib/ledger_sync/ledger_configuration_store.rb
ledger_sync-2.0.0 lib/ledger_sync/ledger_configuration_store.rb
ledger_sync-2.0.0.pre.1 lib/ledger_sync/ledger_configuration_store.rb
ledger_sync-1.8.1 lib/ledger_sync/ledger_configuration_store.rb
ledger_sync-1.8.0 lib/ledger_sync/ledger_configuration_store.rb
ledger_sync-1.7.0 lib/ledger_sync/ledger_configuration_store.rb
ledger_sync-1.6.0 lib/ledger_sync/ledger_configuration_store.rb
ledger_sync-1.5.2 lib/ledger_sync/ledger_configuration_store.rb
ledger_sync-1.5.1 lib/ledger_sync/ledger_configuration_store.rb
ledger_sync-1.5.0 lib/ledger_sync/ledger_configuration_store.rb