Class: ServiceBureau::Locator

Inherits:
Object
  • Object
show all
Defined in:
lib/service_bureau/locator.rb

Overview

Finds service factories and instantiates services

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeLocator

Initializes a new Locator with the mapping of service keys to factories.



5
6
7
# File 'lib/service_bureau/locator.rb', line 5

def initialize
  @service_factories = ServiceBureau::Locations.factory_map
end

Class Method Details

.get_service(service_key, *args) ⇒ Object

Finds a factory and instantiates a service

Parameters:

  • service_key (Symbol)

    the service to look up

  • args

    will be passed to the factory’s call method.

Returns:

  • An instance of the service

Raises:



18
19
20
21
# File 'lib/service_bureau/locator.rb', line 18

def self.get_service service_key, *args
  @instance ||= new
  @instance.get_service service_key, *args
end

Instance Method Details

#get_service(service_key, *args) ⇒ Object

Finds a factory and instantiates a service

Parameters:

  • service_key (Symbol)

    the service to look up

  • args

    will be passed to the factory’s call method.

Returns:

  • An instance of the service

Raises:



24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/service_bureau/locator.rb', line 24

def get_service service_key, *args
  service_factories.fetch(service_key).call *args

rescue KeyError
  raise UnknownServiceError.new("Cannot locate factory for '#{service_key}' - please check your configuration")

rescue NoMethodError => err
  if err.message =~ /undefined method `call'/
    raise UncallableFactoryError.new("The factory registered for '#{service_key}' did not respond to #call")
  else
    raise
  end
end