ruice 0.1.1 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/Gemfile.lock +2 -2
- data/README.md +63 -1
- data/lib/ruice/container.rb +77 -28
- data/lib/ruice/version.rb +1 -1
- metadata +3 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 33c82a922aa3c93f1bee4abdbe62016130a898f5c37797118c75a1d9207c761a
|
4
|
+
data.tar.gz: d4c9dfbd83fa7df21927105777fc3869e704c485d2c3564b04d95fa6f500bea5
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8d35868d18d03578ce6304d764ab9f84110b14bbe61cf2a262e5aa222f865dbda81cad607a5f91bb1e505da5089f4e69db77cd2d061e5d8d7420d35587b35d70
|
7
|
+
data.tar.gz: 1221bfc943c65e2397084917b7d9f1c6cb810ef8fb9c684ec8a385f9df947c7ea63d070b1bdd6ee691e4c0e9af4bbdc9284e639a9f9c548b4f175b388f1e580f
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -4,6 +4,14 @@
|
|
4
4
|
>
|
5
5
|
> \- *Me, a minor Ruby hater*
|
6
6
|
|
7
|
+
Ruice is a DIC container implemented in less than 150 lines of Ruby code.
|
8
|
+
|
9
|
+
It allows for:
|
10
|
+
|
11
|
+
- Automatic injection in instance props
|
12
|
+
- Configuration properties
|
13
|
+
- Autowiring by class name
|
14
|
+
|
7
15
|
## Development status
|
8
16
|
|
9
17
|
I literally wrote this in 12 minutes. Are you mad?
|
@@ -26,7 +34,61 @@ Or install it yourself as:
|
|
26
34
|
|
27
35
|
## Usage
|
28
36
|
|
29
|
-
|
37
|
+
```ruby
|
38
|
+
|
39
|
+
class DependencyA
|
40
|
+
end
|
41
|
+
|
42
|
+
class InjectionPoint
|
43
|
+
def initialize
|
44
|
+
@dependency_a = Ruice::Dependency.new DependencyA
|
45
|
+
@property = Ruice::Property 'my.config.property'
|
46
|
+
@env = Ruice::Property 'env'
|
47
|
+
end
|
48
|
+
|
49
|
+
def dic_ready
|
50
|
+
# Method always invoked when @injected props have been replaced
|
51
|
+
end
|
52
|
+
|
53
|
+
def something
|
54
|
+
@dependency # Instance of DependencyA
|
55
|
+
@property # 'FooBar'
|
56
|
+
@env # 'production'
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
container = Ruice::Container.new(
|
61
|
+
{
|
62
|
+
my: {
|
63
|
+
config: {
|
64
|
+
property: 'FooBar'
|
65
|
+
}
|
66
|
+
}
|
67
|
+
},
|
68
|
+
'production'
|
69
|
+
)
|
70
|
+
|
71
|
+
# Register dependency by class
|
72
|
+
container.with(Logger, proc {
|
73
|
+
Logger.new('/dev/null')
|
74
|
+
})
|
75
|
+
|
76
|
+
# Register dependency by name
|
77
|
+
|
78
|
+
container.with('my_logger', proc {
|
79
|
+
Logger.new('/dev/null')
|
80
|
+
})
|
81
|
+
|
82
|
+
# Automatically created instance
|
83
|
+
injection_point = container.request InjectionPoint
|
84
|
+
|
85
|
+
# Instance of by class Logger
|
86
|
+
logger = container.request Logger
|
87
|
+
|
88
|
+
# Instance of by name Logger
|
89
|
+
logger = container.request 'my_logger'
|
90
|
+
|
91
|
+
```
|
30
92
|
|
31
93
|
## Development
|
32
94
|
|
data/lib/ruice/container.rb
CHANGED
@@ -1,67 +1,116 @@
|
|
1
|
-
# TODO:
|
2
|
-
# - named dependencies
|
3
|
-
# - dependency sets
|
4
|
-
# - ???
|
5
1
|
module Ruice
|
6
2
|
class Dependency
|
7
|
-
def initialize(target,
|
3
|
+
def initialize(target, is_fresh = false)
|
8
4
|
@target = target
|
9
|
-
@
|
5
|
+
@is_fresh = is_fresh
|
10
6
|
end
|
11
7
|
|
12
|
-
attr_reader :target
|
13
|
-
|
8
|
+
attr_reader :target, :is_fresh, :named
|
9
|
+
end
|
10
|
+
|
11
|
+
class Property
|
12
|
+
def initialize(name, default = nil)
|
13
|
+
@name = name
|
14
|
+
@default = default
|
15
|
+
end
|
16
|
+
|
17
|
+
attr_reader :name, :default
|
14
18
|
end
|
15
19
|
|
16
20
|
class Container
|
17
|
-
def initialize
|
21
|
+
def initialize(properties = {}, env = 'default')
|
22
|
+
raise ArgumentError, 'Container properties can not be nil' if properties.nil?
|
23
|
+
raise ArgumentError, 'Container properties is not a Hash' unless properties.is_a? Hash
|
24
|
+
raise ArgumentError, 'Environment can not be nil' if env.nil?
|
25
|
+
raise ArgumentError, 'Environment must be a string' unless env.is_a? String
|
26
|
+
|
27
|
+
properties[:env] = env
|
28
|
+
|
29
|
+
@properties = properties
|
30
|
+
@env = env.to_sym
|
31
|
+
|
18
32
|
@bindings = {}
|
19
33
|
@instances = {}
|
20
34
|
end
|
21
35
|
|
22
|
-
|
23
|
-
|
36
|
+
attr_reader :env
|
37
|
+
|
38
|
+
def lookup_property(name, default = nil)
|
39
|
+
path = name.split '.'
|
40
|
+
current = @properties
|
41
|
+
path.each do |key_part|
|
42
|
+
break if current.nil?
|
43
|
+
|
44
|
+
raise Exception, 'Can not access value subkey for non-hash ' + current unless current.is_a? Hash
|
45
|
+
|
46
|
+
sym_part = key_part.to_sym
|
47
|
+
|
48
|
+
current = current.fetch(sym_part, nil) || current.fetch(key_part, nil)
|
49
|
+
end
|
50
|
+
|
51
|
+
current || default
|
52
|
+
end
|
53
|
+
|
54
|
+
def request(name)
|
55
|
+
return self if name == Ruice::Container
|
24
56
|
|
25
|
-
return @instances[
|
57
|
+
return @instances[name] if @instances.key? name
|
26
58
|
|
27
|
-
instance = request_new
|
59
|
+
instance = request_new name
|
28
60
|
|
29
|
-
@instances[
|
61
|
+
@instances[name] = instance
|
30
62
|
|
31
63
|
instance
|
32
64
|
end
|
33
65
|
|
34
|
-
def request_new(
|
35
|
-
return self if
|
66
|
+
def request_new(name)
|
67
|
+
return self if name == Ruice::Container
|
68
|
+
|
69
|
+
return @bindings[name].call self if @bindings.key? name
|
36
70
|
|
37
|
-
|
71
|
+
raise ArgumentError, 'Dependency name is not class, and no bindings are present' unless name.respond_to? :new
|
38
72
|
|
39
|
-
instance =
|
73
|
+
instance = name.new
|
40
74
|
vars = instance.instance_variables
|
41
75
|
|
42
76
|
vars.each do |it|
|
43
77
|
value = instance.instance_variable_get it
|
44
78
|
|
45
|
-
next unless value.is_a?
|
79
|
+
next unless value.is_a?(Dependency) || value.is_a?(Property)
|
80
|
+
|
81
|
+
replacement = nil
|
82
|
+
replacement = lookup_property value.name, value.default if value.is_a? Property
|
46
83
|
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
84
|
+
if value.is_a? Dependency
|
85
|
+
replacement = if value.is_fresh
|
86
|
+
request_new value.target
|
87
|
+
else
|
88
|
+
request value.target
|
89
|
+
end
|
90
|
+
end
|
52
91
|
|
53
92
|
instance.instance_variable_set it, replacement
|
54
93
|
end
|
55
94
|
|
56
|
-
instance.dic_ready
|
95
|
+
instance.dic_ready if instance.methods.include? :dic_ready
|
57
96
|
|
58
97
|
instance
|
59
98
|
end
|
60
99
|
|
61
|
-
def
|
62
|
-
|
100
|
+
def with(name, subject)
|
101
|
+
if subject.is_a? Proc
|
102
|
+
|
103
|
+
raise ArgumentError, 'Duplicate provider - ' + name if @bindings.key? name
|
104
|
+
|
105
|
+
@bindings[name] = subject
|
106
|
+
return
|
107
|
+
end
|
108
|
+
|
109
|
+
raise ArgumentError, 'Duplicate instance - ' + name if @instances.key? name
|
110
|
+
|
111
|
+
@instances[name] = subject
|
63
112
|
|
64
|
-
|
113
|
+
self
|
65
114
|
end
|
66
115
|
end
|
67
116
|
end
|
data/lib/ruice/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ruice
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Matīss Treinis
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-
|
11
|
+
date: 2019-06-19 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -111,8 +111,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
111
111
|
- !ruby/object:Gem::Version
|
112
112
|
version: '0'
|
113
113
|
requirements: []
|
114
|
-
|
115
|
-
rubygems_version: 2.5.2.3
|
114
|
+
rubygems_version: 3.0.3
|
116
115
|
signing_key:
|
117
116
|
specification_version: 4
|
118
117
|
summary: Guice-ish dependency injection library for Ruby
|