Module: ServiceBureau::Services

Defined in:
lib/service_bureau/services.rb

Overview

When mixed in to a class, it provides a handle for all configured services. It also provides an injector method for each service so that altenative implementations can be easily injected e.g. for testing.

Examples:

ServiceBureau::Locations.configure do |c|
  c.my_service MyService.public_method(:new)
end

Class MyClass
  include ServiceBureau::Services
end

obj = MyClass.new
obj.my_service # => returns the result of: MyService.new

obj.my_service = double('a fake service')
obj.my_service # => returns the test double

Class Method Summary collapse

Class Method Details

.included(base) ⇒ Object



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

def self.included(base)
  ServiceBureau::Locations.factory_map.keys.each do |service|
    # Provide a method to inject the service obj
    define_method "#{service}=" do |service_obj|
      __set_service__ service, service_obj
    end

    # Provide a handle to the service instance
    define_method service do |*args|
      __get_service__ service, *args
    end
  end
end