Sha256: 3f7c06054e10dafd9c092d3b81d554201676084502a559efe51703ee5f27964e

Contents?: true

Size: 1.3 KB

Versions: 1

Compression:

Stored size: 1.3 KB

Contents

module LedgerSync
  class AdaptorConfigurationStore
    include Enumerable

    attr_reader :configs, :inflections

    def initialize
      @keys = []
      @configs = {}
      @inflections = []
      @class_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_class(adaptor_class:)
      @class_configs.fetch(adaptor_class)
    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
      @class_configs[adaptor_config.adaptor_class] = 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

1 entries across 1 versions & 1 rubygems

Version Path
ledger_sync-1.3.5 lib/ledger_sync/adaptor_configuration_store.rb