Sha256: bab24dcc3162e8eb88aecad6a19cb94c19e68db4df78fedf8ab63568ffed34d1

Contents?: true

Size: 1.28 KB

Versions: 4

Compression:

Stored size: 1.28 KB

Contents

module LedgerSync
  class AdaptorConfigurationStore
    include Enumerable

    attr_reader :configs, :inflections

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

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

        return
      end

      _instance_methods_for(adaptor_key: adaptor_key, adaptor_config: existing_config)
    end

    def config_from_klass(klass:)
      @klass_configs.fetch(klass)
    end

    def each
      configs.each { |k, v| yield(k, v) }
    end

    def register_adaptor(adaptor_config:)
      _instance_methods_for(
        adaptor_key: adaptor_config.root_key,
        adaptor_config: adaptor_config
      )
    end

    private

    def _instance_methods_for(adaptor_key:, adaptor_config:)
      @keys << adaptor_key.to_sym

      @configs[adaptor_key] = adaptor_config
      @klass_configs[adaptor_config.adaptor_klass] = adaptor_config

      instance_variable_set(
        "@#{adaptor_key}",
        adaptor_config
      )

      @inflections |= [adaptor_config.module_string]
      self.class.class_eval { attr_reader adaptor_key }
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
ledger_sync-1.3.4 lib/ledger_sync/adaptor_configuration_store.rb
ledger_sync-1.3.3 lib/ledger_sync/adaptor_configuration_store.rb
ledger_sync-1.3.2 lib/ledger_sync/adaptor_configuration_store.rb
ledger_sync-1.3.1 lib/ledger_sync/adaptor_configuration_store.rb