injectable 0.0.5 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,26 +0,0 @@
1
- # encoding: utf-8
2
- module Injectable
3
- module Registerable
4
-
5
- # Register that instances of klass will perform the given role in this
6
- # container context.
7
- #
8
- # @example Register that the user_finder role will be performed by
9
- # instances of DatabaseUserFinder
10
- # container.register_implementation(:user_finder, DatabaseUserFinder)
11
- #
12
- # @param [ Symbol ] name The name of the role.
13
- # @param [ *Class ] classes The names of the classes performing this role.
14
- #
15
- # @since 0.0.1
16
- def register_implementation(name, *classes)
17
- implementations[name] = classes.flatten
18
- end
19
-
20
- private
21
-
22
- def implementations
23
- @implementations ||= {}
24
- end
25
- end
26
- end
@@ -1,89 +0,0 @@
1
- # encoding: utf-8
2
- require "injectable/registerable"
3
-
4
- module Injectable
5
-
6
- # The registry keeps track of all objects and their dependencies that need
7
- # to be injected at construction.
8
- #
9
- # @since 0.0.0
10
- module Registry
11
- include Registerable
12
- extend self
13
-
14
- # Get an implementation for the provided name.
15
- #
16
- # @example Get an implementation.
17
- # Injectable::Registry.implementation(:persistable)
18
- #
19
- # @param [ Symbol ] name The name of the implementation.
20
- #
21
- # @return [ Class ] The implementing class.
22
- #
23
- # @since 0.0.2
24
- def implementation(name)
25
- impl = implementations[name]
26
- raise(NotRegistered.new(name)) unless impl && !impl.empty?
27
- impl
28
- end
29
-
30
- # Add a constructor method signature to the registry.
31
- #
32
- # @example Add a signature.
33
- # Injectable::Registry.register_signature(
34
- # UserService, [ :user, :user_finder ]
35
- # )
36
- #
37
- # @param [ Class ] klass The class to set the constructor signature for.
38
- # @param [ Array<Symbol> ] dependencies The dependencies of the
39
- # constructor.
40
- #
41
- # @since 0.0.0
42
- def register_signature(klass, dependencies)
43
- signatures[klass] = dependencies.map { |name| name }
44
- end
45
-
46
- # Get the constructor method signature for the provided class.
47
- #
48
- # @example Get the constructor signature.
49
- # Injectable::Registry.signature(UserService)
50
- #
51
- # @param [ Class ] klass The class to get the signature for.
52
- #
53
- # @return [ Array<Class> ] The constructor signature.
54
- #
55
- # @since 0.0.0
56
- def signature(klass)
57
- signatures[klass]
58
- end
59
-
60
- # This error is raised when asking for an implementing class that is not
61
- # registered in the registry.
62
- #
63
- # @since 0.0.2
64
- class NotRegistered < Exception
65
-
66
- # @attribute [r] name The name of the requested implementation.
67
- attr_reader :name
68
-
69
- # Initialize the new error.
70
- #
71
- # @example Initialize the error.
72
- # NotRegistered.new(:persistable)
73
- #
74
- # @param [ Symbol ] name The name of the implementation.
75
- #
76
- # @since 0.0.2
77
- def initialize(name)
78
- @name = name
79
- super("No implementation registered for name: #{name.inspect}.")
80
- end
81
- end
82
-
83
- private
84
-
85
- def signatures
86
- @signatures ||= {}
87
- end
88
- end
89
- end