Class: Longleaf::ServiceMappingManager

Inherits:
Object
  • Object
show all
Defined in:
lib/longleaf/services/service_mapping_manager.rb

Overview

Manager which loads and provides access to location to service mappings

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ ServiceMappingManager

Returns a new instance of ServiceMappingManager.

Parameters:

  • config (Hash)

    has representation of the application configuration

Raises:

  • (ArgumentError)


10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/longleaf/services/service_mapping_manager.rb', line 10

def initialize(config)
  raise ArgumentError.new("Configuration must be provided") if config.nil? || config.empty?

  mappings_config = config[AF::SERVICE_MAPPINGS]
  raise ArgumentError.new("Service mappings configuration must be provided") if mappings_config.nil?

  @loc_to_services = Hash.new

  mappings_config.each do |mapping|
    locations = mapping[AF::LOCATIONS]
    services = mapping[AF::SERVICES]

    locations = [locations] if locations.is_a?(String)
    services = [services] if services.is_a?(String)

    locations.each do |loc_name|
      @loc_to_services[loc_name] = Array.new unless @loc_to_services.key?(loc_name)

      service_set = @loc_to_services[loc_name]
      if services.is_a?(String)
        service_set.push(services)
      else
        service_set.concat(services)
      end
    end
  end

  @loc_to_services.each { |loc, services| services.uniq! }
  @loc_to_services.freeze
end

Instance Method Details

#list_services(loc_name) ⇒ Array

Gets a list of service names associated with the given location

Parameters:

  • loc_name (String)

    name of the location to lookup

Returns:

  • (Array)

    a list of service names associated with the location



44
45
46
# File 'lib/longleaf/services/service_mapping_manager.rb', line 44

def list_services(loc_name)
  @loc_to_services[loc_name] || []
end