tina4ruby 0.5.1 → 0.5.2
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 +4 -4
- data/lib/tina4/container.rb +54 -0
- data/lib/tina4/version.rb +1 -1
- data/lib/tina4.rb +10 -0
- metadata +2 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 699bbf680f46c379b0961743db6833320d732307a16bbc4d69e2ca5f408ee7d9
|
|
4
|
+
data.tar.gz: 761653a1180fd152e9eeeb3527d154381616985d53c0143f24cae5c316aa4461
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 27dd9fec865fc4e66c1bb68ffbf785b71130625b3ce560edcae24c7ff5ce4cc481a86bae5b7ac169a81529c279bf50a49df43c1f55606060081bfc433eda6cab
|
|
7
|
+
data.tar.gz: 1693f488ddea8df352c85833f412f7346b658644a1de6da5a05cc0dce4baa0a6262d2af26f0ffc6413f7182c513e03eb530851b169c79d7b6feb90f46935fd2a
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Tina4
|
|
4
|
+
# Lightweight dependency injection container.
|
|
5
|
+
#
|
|
6
|
+
# Tina4.register(:mailer, MailService.new) # concrete instance
|
|
7
|
+
# Tina4.register(:db) { Database.new(ENV["DB_URL"]) } # lazy factory
|
|
8
|
+
# Tina4.resolve(:mailer) # => MailService instance
|
|
9
|
+
#
|
|
10
|
+
module Container
|
|
11
|
+
class << self
|
|
12
|
+
def registry
|
|
13
|
+
@registry ||= {}
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
# Register a service by name.
|
|
17
|
+
# Pass an instance directly, or a block for lazy instantiation.
|
|
18
|
+
def register(name, instance = nil, &factory)
|
|
19
|
+
raise ArgumentError, "provide an instance or a block, not both" if instance && factory
|
|
20
|
+
raise ArgumentError, "provide an instance or a block" unless instance || factory
|
|
21
|
+
|
|
22
|
+
registry[name.to_sym] = if factory
|
|
23
|
+
{ factory: factory, instance: nil }
|
|
24
|
+
else
|
|
25
|
+
{ factory: nil, instance: instance }
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
# Resolve a service by name.
|
|
30
|
+
# Lazy factories are called once and memoized.
|
|
31
|
+
def resolve(name)
|
|
32
|
+
entry = registry[name.to_sym]
|
|
33
|
+
raise KeyError, "service not registered: #{name}" unless entry
|
|
34
|
+
|
|
35
|
+
if entry[:instance]
|
|
36
|
+
entry[:instance]
|
|
37
|
+
elsif entry[:factory]
|
|
38
|
+
entry[:instance] = entry[:factory].call
|
|
39
|
+
entry[:instance]
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
# Check if a service is registered.
|
|
44
|
+
def registered?(name)
|
|
45
|
+
registry.key?(name.to_sym)
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
# Remove all registrations (useful in tests).
|
|
49
|
+
def clear!
|
|
50
|
+
@registry = {}
|
|
51
|
+
end
|
|
52
|
+
end
|
|
53
|
+
end
|
|
54
|
+
end
|
data/lib/tina4/version.rb
CHANGED
data/lib/tina4.rb
CHANGED
|
@@ -26,6 +26,7 @@ require_relative "tina4/auth"
|
|
|
26
26
|
require_relative "tina4/session"
|
|
27
27
|
require_relative "tina4/middleware"
|
|
28
28
|
require_relative "tina4/localization"
|
|
29
|
+
require_relative "tina4/container"
|
|
29
30
|
require_relative "tina4/queue"
|
|
30
31
|
|
|
31
32
|
module Tina4
|
|
@@ -217,6 +218,15 @@ module Tina4
|
|
|
217
218
|
Tina4::Localization.t(key, **options)
|
|
218
219
|
end
|
|
219
220
|
|
|
221
|
+
# DI container shortcuts
|
|
222
|
+
def register(name, instance = nil, &block)
|
|
223
|
+
Tina4::Container.register(name, instance, &block)
|
|
224
|
+
end
|
|
225
|
+
|
|
226
|
+
def resolve(name)
|
|
227
|
+
Tina4::Container.resolve(name)
|
|
228
|
+
end
|
|
229
|
+
|
|
220
230
|
private
|
|
221
231
|
|
|
222
232
|
# Resolve auth option for route registration
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: tina4ruby
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.5.
|
|
4
|
+
version: 0.5.2
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Tina4 Team
|
|
@@ -249,6 +249,7 @@ files:
|
|
|
249
249
|
- lib/tina4/api.rb
|
|
250
250
|
- lib/tina4/auth.rb
|
|
251
251
|
- lib/tina4/cli.rb
|
|
252
|
+
- lib/tina4/container.rb
|
|
252
253
|
- lib/tina4/crud.rb
|
|
253
254
|
- lib/tina4/database.rb
|
|
254
255
|
- lib/tina4/database_result.rb
|