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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 6990765547b19aefdc010ea9e3ce13a00255166ff5eba52963422a2d5e493ff4
4
- data.tar.gz: 33f2417c218fe03148b96c9e7f0a63e8e69f2dd1c9046aee0a9c1fd64e9c93cf
3
+ metadata.gz: 699bbf680f46c379b0961743db6833320d732307a16bbc4d69e2ca5f408ee7d9
4
+ data.tar.gz: 761653a1180fd152e9eeeb3527d154381616985d53c0143f24cae5c316aa4461
5
5
  SHA512:
6
- metadata.gz: 6a100277f5e4ab408ac2a8a536fd5e59f3611276623cb0af76599e05eb3a0f872a6ee5f420558d10bff41dac913f07df91e11812df33bb781fcc23f79595462b
7
- data.tar.gz: 4f8e0599f8985544ebad7ab96e867a616aef01b56c3d52b7d167a7a042ec9626efac25eced5c03e94908bda9325b9e54148297fd4df4fc5c690fb268cf259490
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
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Tina4
4
- VERSION = "0.5.1"
4
+ VERSION = "0.5.2"
5
5
  end
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.1
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