Sha256: 3171112a5095975d67af03ef8220e6defd5e3cf29d13a4967ba2f8e2034a5653

Contents?: true

Size: 1.72 KB

Versions: 25

Compression:

Stored size: 1.72 KB

Contents

require 'rom/support/configurable'
require 'rom/gateway'

module ROM
  # Core gateway configuration interface
  #
  # @api public
  class Environment
    include Configurable

    attr_reader :gateways, :gateways_map

    # @api public
    def initialize(*args)
      @gateways = {}
      @gateways_map = {}

      configure_gateways(*args) unless args.empty?
    end

    private

    def configure_gateways(*args)
      normalized_gateway_args = normalize_gateway_args(*args)
      normalized_gateways = normalize_gateways(normalized_gateway_args)

      @gateways, @gateways_map = normalized_gateways.values_at(:gateways, :map)

      normalized_gateway_args.each_with_object(config) do |(name, gateway_config), config|
        options = gateway_config.is_a?(Array) && gateway_config.last
        load_config(config.gateways[name], options) if options.is_a?(Hash)
      end
    end


    # @api private
    def normalize_gateway_args(*args)
      args.first.is_a?(Hash) ? args.first : {default: args}
    end

    # Build gateways using the setup interface
    #
    # @api private
    def normalize_gateways(gateways_config)
      gateways_config.each_with_object({map: {}, gateways: {}}) do |(name, spec), hash|
        identifier, *args = Array(spec)

        if identifier.is_a?(Gateway)
          gateway = identifier
        else
          gateway = Gateway.setup(identifier, *(args.flatten))
          hash[:map][gateway] = identifier
        end

        hash[:gateways][name] = gateway
      end
    end

    # @api private
    def load_config(config, hash)
      hash.each do |key, value|
        if value.is_a?(Hash)
          load_config(config[key], value)
        else
          config.send("#{key}=", value)
        end
      end
    end
  end
end

Version data entries

25 entries across 25 versions & 1 rubygems

Version Path
rom-3.3.3 lib/rom/environment.rb
rom-3.3.2 lib/rom/environment.rb
rom-3.3.1 lib/rom/environment.rb
rom-3.3.0 lib/rom/environment.rb
rom-3.2.3 lib/rom/environment.rb
rom-3.2.2 lib/rom/environment.rb
rom-3.2.1 lib/rom/environment.rb
rom-3.2.0 lib/rom/environment.rb
rom-3.1.0 lib/rom/environment.rb
rom-3.0.3 lib/rom/environment.rb
rom-3.0.2 lib/rom/environment.rb
rom-3.0.1 lib/rom/environment.rb
rom-3.0.0 lib/rom/environment.rb
rom-3.0.0.rc2 lib/rom/environment.rb
rom-3.0.0.rc1 lib/rom/environment.rb
rom-3.0.0.beta3 lib/rom/environment.rb
rom-3.0.0.beta2 lib/rom/environment.rb
rom-3.0.0.beta1 lib/rom/environment.rb
rom-2.0.2 lib/rom/environment.rb
rom-2.0.1 lib/rom/environment.rb