class Acfs::Configuration

Acfs configuration is used to locate services and get their base URLs.

Attributes

adapter[RW]
locations[R]

Public Class Methods

current() click to toggle source

@api private

Return current configuration object.

@return [Configuration]

# File lib/acfs/configuration.rb, line 103
def current
  @current ||= new
end
new() click to toggle source

@api private

# File lib/acfs/configuration.rb, line 14
def initialize
  @locations = {}
end
set(configuration) click to toggle source

@api private

Swap configuration object with given new one. Must be a {Configuration} object.

@param [Configuration] configuration @return [undefined]

# File lib/acfs/configuration.rb, line 115
def set(configuration)
  @current = configuration if configuration.is_a? Configuration
end

Public Instance Methods

configure() { |self| ... } click to toggle source

@api public

Configure using given block. If block accepts zero arguments bock will be evaluated in context of the configuration instance otherwise the configuration instance will be given as first arguments.

@yield [configuration] Give configuration as arguments or evaluate block

in context of configuration object.

@yieldparam configuration [Configuration] Configuration object. @return [undefined]

# File lib/acfs/configuration.rb, line 29
def configure(&block)
  if block.arity.positive?
    yield self
  else
    instance_eval(&block)
  end
end
load(filename) click to toggle source

@api public

Load configuration from given YAML file.

@param [String] filename Path to YAML configuration file. @return [undefined]

# File lib/acfs/configuration.rb, line 72
def load(filename)
  config = load_yaml_file(filename)
  env = ENV['RACK_ENV'] || ENV['RAILS_ENV'] || 'development'

  config = config[env] if config.key? env
  config.each do |key, value|
    case key
      when 'services' then load_services value
    end
  end
end
load_services(services) click to toggle source

@api private

Load services from configuration YAML.

# File lib/acfs/configuration.rb, line 88
def load_services(services)
  services.each do |service, data|
    if (val = data).is_a?(String) || (val = data['locate'])
      locate service.to_sym, val
    end
  end
end
locate(service, uri = nil) click to toggle source

@api public

@overload locate(service, uri)

Configures URL where a service can be reached.

@param [Symbol] service
  Service identity key for service that is reachable under given URL.

@param [String] uri
  URL where service is reachable. Will be passed to {URI.parse}.

@return [undefined]

@overload locate(service)

Return configured base URL for given service identity key.

@param [Symbol] service Service identity key to lookup.
@return [URI, NilClass] Configured base URL or nil.
# File lib/acfs/configuration.rb, line 56
def locate(service, uri = nil)
  service = service.to_s.underscore.to_sym
  if uri.nil?
    locations[service]
  else
    locations[service] = URI.parse uri
  end
end

Private Instance Methods

load_yaml_file(path) click to toggle source
# File lib/acfs/configuration.rb, line 122
def load_yaml_file(path)
  if YAML.respond_to?(:safe_load_file)
    YAML.safe_load_file(path, aliases: true)
  else
    YAML.safe_load(File.read(path), [], [], true)
  end
end