service_bureau 0.1.0 → 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- MzZkOGZmNWI1Y2FkMjU5OGJiODNlZDUzN2MzZWQ2ODNiYzA0ZDgwYg==
4
+ OWI3ZTYwZmU5YTQ5ODZiZWEzZDZmZTAyYTNkNGY4ZGUzNGIyM2NiNQ==
5
5
  data.tar.gz: !binary |-
6
- ZjNiZWYxMTVkMzIwM2U4ZGE4MTUzMDZmMjAzYmIwODkzZTVmMGY1Zg==
6
+ ZmI5YWY3Y2UwMDNlYjY1Y2FhMTMwNDc1NzIzZDlmNmNjOGQ3YWE3OA==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- MWNkOTdiZDFkYzNjMTYwNjBmMGY4MDViOGMyNmY3YTI4YjJiZTg2NGM4Y2Zk
10
- NTA5Nzg1OWM3MWM5Y2I3ZTJmNzgyYTEzYjkxZjMxZmE4YTkwZGFmZmRjNDkx
11
- MGE4MjBjZjdkNmNhMjA3OGRiM2RlMWVhNDdkYTlmYWEyYjBmMjM=
9
+ MTcxMjdlZWQzMDdlZDRjMGE1N2FlNzI4NjI3N2JkZDdlZDU5YzEzZTYxYTE4
10
+ NGI2ZWQ3ZGYwZmIxNmU1ZWYzODI2YTM0NmRmNDhjODYwNmJlODE3NmU4MDQw
11
+ ZDQwYTVjNDhjNmQwYWFiNTk2ODdjYWY3NWY0Zjg5YmJjMDZjNmM=
12
12
  data.tar.gz: !binary |-
13
- NWIxM2JiNmEwNjVjNjFhMDhhNGI3NTQ2ZmYwYzIyODk4OGViNWNjNzdkZmVm
14
- ZDUwMjg5OGI1NzYyZTk4YWRjZGQ2MTMzNzNjN2Q1MTIzMTRkMWM2MDIwMTlh
15
- YzU3MTljMTUwNmQ4Njg0OTcyMzkyNzllNzY5NzQ0ZjcxNmIyNTM=
13
+ ZDY0ZDRkNTZhMzAyYWNmZTNhMWIwOWM1ZjIxM2U3ODI3ZWUxYWQwMjBmN2Iw
14
+ ODc3NmYyOWIxZDJhZjE0MmJkMWI1MjRiZDQ1MmI1ZTlhNjRlYjM0YmRjYTI3
15
+ YmFjMGIxOGI1YWFiYzY1NmQ0ODdjODliMTkxYzU4OWFkZmY3OTM=
@@ -3,7 +3,11 @@ require "service_bureau/locations"
3
3
  require "service_bureau/locator"
4
4
  require "service_bureau/services"
5
5
 
6
+ # Provides easy access to service dependencies, as well
7
+ # as a way to inject them as dependencies.
6
8
  module ServiceBureau
9
+ # Raised when a requested service is not configured
7
10
  class UnknownServiceError < StandardError;end
11
+ # Raised when a requested service cannot be instantiated.
8
12
  class UncallableFactoryError < StandardError;end
9
13
  end
@@ -1,18 +1,41 @@
1
1
  module ServiceBureau
2
+ # Stores the factories for all the configured services
2
3
  class Locations
3
4
  class << self
5
+ # Configures the factories for each service
6
+ #
7
+ # Each service should be configured with an
8
+ # object that responds to 'call'
9
+ #
10
+ # @example
11
+ # ServiceBureau::Locations.configure do |c|
12
+ # c.my_service MyService.public_method(:new)
13
+ # c.other_service ->(arg){ OtherService.new(arg) }
14
+ # end
15
+ #
16
+ # @return nothing
17
+ # @yield ServiceBureau::Locations
18
+ # @yieldreturn nothing
4
19
  def configure(&block)
5
20
  yield self
6
21
  end
7
22
 
23
+ # @api private
24
+ # Gets the map of service keys to factories.
25
+ #
26
+ # @return [Hash] mapping service keys to factories
8
27
  def factory_map
9
28
  @factory_map ||= {}
10
29
  end
11
30
 
31
+ # Removes all the registered services.
32
+ #
33
+ # @return [Hash] the empty factory_map
12
34
  def clear
13
35
  factory_map.clear
14
36
  end
15
37
 
38
+ # @private
16
39
  def method_missing(method_name, *args, &block)
17
40
  if args.size == 1 && !block_given?
18
41
  factory_map[method_name] = args.first
@@ -1,14 +1,26 @@
1
1
  module ServiceBureau
2
+ # Finds service factories and instantiates services
2
3
  class Locator
4
+ # Initializes a new Locator with the mapping of service keys to factories.
3
5
  def initialize
4
6
  @service_factories = ServiceBureau::Locations.factory_map
5
7
  end
6
8
 
9
+ # Finds a factory and instantiates a service
10
+ #
11
+ # @param service_key [Symbol] the service to look up
12
+ # @param args will be passed to the factory's call method.
13
+ #
14
+ # @return An instance of the service
15
+ #
16
+ # @raise [UnknownServiceError] If a factory cannot be found.
17
+ # @raise [UncallableFactoryError] If a factory is found, but does not respond to call
7
18
  def self.get_service service_key, *args
8
19
  @instance ||= new
9
20
  @instance.get_service service_key, *args
10
21
  end
11
22
 
23
+ # (see .get_service)
12
24
  def get_service service_key, *args
13
25
  service_factories.fetch(service_key).call *args
14
26
 
@@ -1,4 +1,24 @@
1
1
  module ServiceBureau
2
+ # When mixed in to a class, it provides a handle for
3
+ # all configured services.
4
+ # It also provides an injector method for each service
5
+ # so that altenative implementations can be easily
6
+ # injected e.g. for testing.
7
+ #
8
+ # @example
9
+ # ServiceBureau::Locations.configure do |c|
10
+ # c.my_service MyService.public_method(:new)
11
+ # end
12
+ #
13
+ # Class MyClass
14
+ # include ServiceBureau::Services
15
+ # end
16
+ #
17
+ # obj = MyClass.new
18
+ # obj.my_service # => returns the result of: MyService.new
19
+ #
20
+ # obj.my_service = double('a fake service')
21
+ # obj.my_service # => returns the test double
2
22
  module Services
3
23
  def self.included(base)
4
24
  ServiceBureau::Locations.factory_map.keys.each do |service|
@@ -1,3 +1,4 @@
1
1
  module ServiceBureau
2
- VERSION = "0.1.0"
2
+ # The current ServiceBureau version
3
+ VERSION = "1.0.1"
3
4
  end
@@ -10,7 +10,7 @@ Gem::Specification.new do |spec|
10
10
  spec.email = ["mattvanhorn@gmail.com"]
11
11
  spec.description = %q{Something of a cross between a Service Locator and an Abstract Factory to enable easier use of the ServiceObject pattern.}
12
12
  spec.summary = %q{An easy way to provide access to your service classes without creating a lot of coupling}
13
- spec.homepage = ""
13
+ spec.homepage = "https://github.com/mattvanhorn/service_bureau"
14
14
  spec.license = "MIT"
15
15
 
16
16
  spec.files = `git ls-files`.split($/)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: service_bureau
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 1.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Matt Van Horn
@@ -72,7 +72,7 @@ files:
72
72
  - lib/service_bureau/version.rb
73
73
  - service_bureau.gemspec
74
74
  - spec/service_bureau_spec.rb
75
- homepage: ''
75
+ homepage: https://github.com/mattvanhorn/service_bureau
76
76
  licenses:
77
77
  - MIT
78
78
  metadata: {}