ruice 0.1.1 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 838e559cc7e1c6bb563444a3eb45927c80acb7e5
4
- data.tar.gz: 6b9687644a230f2d19bf4b2e498c9b9ad07ffd07
2
+ SHA256:
3
+ metadata.gz: 33c82a922aa3c93f1bee4abdbe62016130a898f5c37797118c75a1d9207c761a
4
+ data.tar.gz: d4c9dfbd83fa7df21927105777fc3869e704c485d2c3564b04d95fa6f500bea5
5
5
  SHA512:
6
- metadata.gz: 7a3ef2ff40a7ccf530eaccb91635439876e5d22c1838664a969c459738a86f92ab28b89939ca457846a2b35f9b9d91be2a03a1ab473613bfe3735304cd833b40
7
- data.tar.gz: 303d617940251bc1a739538c9ae4b2d1c197bc7d0685372ba5bdf56c8f9f12acedb20089699164408f0bc54b2d7cec6e09177a761115efa7cded5e5204bccaa5
6
+ metadata.gz: 8d35868d18d03578ce6304d764ab9f84110b14bbe61cf2a262e5aa222f865dbda81cad607a5f91bb1e505da5089f4e69db77cd2d061e5d8d7420d35587b35d70
7
+ data.tar.gz: 1221bfc943c65e2397084917b7d9f1c6cb810ef8fb9c684ec8a385f9df947c7ea63d070b1bdd6ee691e4c0e9af4bbdc9284e639a9f9c548b4f175b388f1e580f
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- ruice (0.1.1)
4
+ ruice (0.2.0)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
@@ -50,4 +50,4 @@ DEPENDENCIES
50
50
  ruice!
51
51
 
52
52
  BUNDLED WITH
53
- 2.0.1
53
+ 2.0.2
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
- TODO. Read source if you want.
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
 
@@ -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, require_new = false)
3
+ def initialize(target, is_fresh = false)
8
4
  @target = target
9
- @require_new = require_new
5
+ @is_fresh = is_fresh
10
6
  end
11
7
 
12
- attr_reader :target
13
- attr_reader :require_new
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
- def request(target_class)
23
- return self if target_class == DIC::Container
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[target_class] if @instances.key? target_class
57
+ return @instances[name] if @instances.key? name
26
58
 
27
- instance = request_new target_class
59
+ instance = request_new name
28
60
 
29
- @instances[target_class] = instance
61
+ @instances[name] = instance
30
62
 
31
63
  instance
32
64
  end
33
65
 
34
- def request_new(target_class)
35
- return self if target_class == DIC::Container
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
- return @bindings[target_class].call self if @bindings.key? target_class
71
+ raise ArgumentError, 'Dependency name is not class, and no bindings are present' unless name.respond_to? :new
38
72
 
39
- instance = target_class.new
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? Dependency
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
- replacement = if value.require_new
48
- request_new value.target
49
- else
50
- request value.target
51
- end
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(self) if instance.methods.include? :dic_ready
95
+ instance.dic_ready if instance.methods.include? :dic_ready
57
96
 
58
97
  instance
59
98
  end
60
99
 
61
- def attach(name, provider)
62
- raise ArgumentError, 'Argument must be instance of Proc' unless provider.is_a? Proc
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
- @bindings[name] = provider
113
+ self
65
114
  end
66
115
  end
67
116
  end
@@ -1,3 +1,3 @@
1
1
  module Ruice
2
- VERSION = '0.1.1'
2
+ VERSION = '0.2.0'
3
3
  end
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.1.1
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-04-05 00:00:00.000000000 Z
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
- rubyforge_project:
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