injectable 0.0.5 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +11 -0
- data/.rspec +3 -0
- data/.travis.yml +7 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +4 -0
- data/Gemfile.lock +35 -0
- data/LICENSE.txt +21 -0
- data/README.md +291 -142
- data/Rakefile +2 -26
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/injectable.gemspec +28 -0
- data/lib/injectable.rb +57 -46
- data/lib/injectable/class_methods.rb +157 -0
- data/lib/injectable/dependencies_graph.rb +43 -0
- data/lib/injectable/dependencies_proxy.rb +29 -0
- data/lib/injectable/dependency.rb +40 -0
- data/lib/injectable/instance_methods.rb +53 -0
- data/lib/injectable/missing_dependencies_exception.rb +4 -0
- data/lib/injectable/version.rb +1 -2
- metadata +82 -31
- data/LICENSE +0 -20
- data/lib/injectable/container.rb +0 -127
- data/lib/injectable/inflector.rb +0 -30
- data/lib/injectable/macros.rb +0 -60
- data/lib/injectable/registerable.rb +0 -26
- data/lib/injectable/registry.rb +0 -89
@@ -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
|
data/lib/injectable/registry.rb
DELETED
@@ -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
|