Class: Acfs::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/acfs/configuration.rb

Overview

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfiguration

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of Configuration.



14
15
16
# File 'lib/acfs/configuration.rb', line 14

def initialize
  @locations = {}
end

Instance Attribute Details

#adapterObject

Returns the value of attribute adapter.



11
12
13
# File 'lib/acfs/configuration.rb', line 11

def adapter
  @adapter
end

#locationsObject (readonly)

Returns the value of attribute locations.



10
11
12
# File 'lib/acfs/configuration.rb', line 10

def locations
  @locations
end

Class Method Details

.currentConfiguration

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Return current configuration object.

Returns:



103
104
105
# File 'lib/acfs/configuration.rb', line 103

def current
  @current ||= new
end

.set(configuration) ⇒ undefined

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Swap configuration object with given new one. Must be a Acfs::Configuration object.

Parameters:

Returns:

  • (undefined)


115
116
117
# File 'lib/acfs/configuration.rb', line 115

def set(configuration)
  @current = configuration if configuration.is_a? Configuration
end

Instance Method Details

#configure {|configuration| ... } ⇒ undefined

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.

Yields:

  • (configuration)

    Give configuration as arguments or evaluate block in context of configuration object.

Yield Parameters:

Returns:

  • (undefined)


29
30
31
32
33
34
35
# File 'lib/acfs/configuration.rb', line 29

def configure(&block)
  if block.arity.positive?
    yield self
  else
    instance_eval(&block)
  end
end

#load(filename) ⇒ undefined

Load configuration from given YAML file.

Parameters:

  • filename (String)

    Path to YAML configuration file.

Returns:

  • (undefined)


72
73
74
75
76
77
78
79
80
81
82
# 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) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Load services from configuration YAML.



88
89
90
91
92
93
94
# 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) ⇒ undefined #locate(service) ⇒ URI, NilClass

Overloads:

  • #locate(service, uri) ⇒ undefined

    Configures URL where a service can be reached.

    Parameters:

    • service (Symbol)

      Service identity key for service that is reachable under given URL.

    • uri (String)

      URL where service is reachable. Will be passed to URI.parse.

    Returns:

    • (undefined)
  • #locate(service) ⇒ URI, NilClass

    Return configured base URL for given service identity key.

    Parameters:

    • service (Symbol)

      Service identity key to lookup.

    Returns:

    • (URI, NilClass)

      Configured base URL or nil.



56
57
58
59
60
61
62
63
# 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