tina4ruby 3.10.38 → 3.10.39

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: 0a0442856facd7be61939efc9066b395c7578a2b7bd8df31282e02fdec58b95c
4
- data.tar.gz: 33170414bbc5ed736aab77ce1790b62e90a74a62d51fa3f194b154b69acd34c4
3
+ metadata.gz: 6d61b5cbf67e0703b569e23d10d160e817c0a2ab39e40afc79970d42b4934ef8
4
+ data.tar.gz: 1c07bde0df2774c7c8105f9ecd90aeb0863e83ad935e89500eda76cf9d638602
5
5
  SHA512:
6
- metadata.gz: 237cd6644e93ee8ed01754a550a65cd5b43d9da8771631e7cff0745bb3e26e2e2892ddbea96e619bf264b527ce207276436c9f8a5c444f0ed1c28486a27ebe31
7
- data.tar.gz: 71fb5d29f52b2aa9971424fbfddcc236881764a7bf6af0bcf491aa0a587d86d1ffcbd6ecfca5296bdbf3d2ee5d504d40eff2ea3eb4753c31dbb0db5c89326690
6
+ metadata.gz: 7134b5ad784722e27d00bee27dd02a5af60f7e096a38ea7a0a740b1a332d3c12b00cd01b3ee586aa23c2162d7a803505b79447868d7101b5c106ebb1a4e985ef
7
+ data.tar.gz: ef891151367dfdbed749fb164cff04a8c3884731763647a660ce7f40b61e33fe6cf23c711c79dfc433ecea5af2c80285cdaebe1ac0f4268b850528467dc1c19b
@@ -3,9 +3,10 @@
3
3
  module Tina4
4
4
  # Lightweight dependency injection container.
5
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
6
+ # Tina4::Container.register(:mailer) { MailService.new } # transient — new instance each resolve
7
+ # Tina4::Container.singleton(:db) { Database.new(ENV["DB_URL"]) } # singleton — memoised
8
+ # Tina4::Container.register(:cache, RedisCacheInstance) # concrete instance (always same)
9
+ # Tina4::Container.resolve(:db) # => Database instance
9
10
  #
10
11
  module Container
11
12
  class << self
@@ -14,28 +15,47 @@ module Tina4
14
15
  end
15
16
 
16
17
  # Register a service by name.
17
- # Pass an instance directly, or a block for lazy instantiation.
18
+ # Pass a concrete instance directly, or a block for transient instantiation.
19
+ # Blocks are called on every resolve() — use singleton() for memoised factories.
18
20
  def register(name, instance = nil, &factory)
19
21
  raise ArgumentError, "provide an instance or a block, not both" if instance && factory
20
22
  raise ArgumentError, "provide an instance or a block" unless instance || factory
21
23
 
22
24
  registry[name.to_sym] = if factory
23
- { factory: factory, instance: nil }
25
+ { factory: factory, singleton: false, instance: nil }
24
26
  else
25
- { factory: nil, instance: instance }
27
+ { factory: nil, singleton: false, instance: instance }
26
28
  end
27
29
  end
28
30
 
31
+ # Register a singleton factory by name.
32
+ # The block is called once on first resolve() and the result is memoised.
33
+ def singleton(name, &factory)
34
+ raise ArgumentError, "singleton requires a block" unless factory
35
+
36
+ registry[name.to_sym] = { factory: factory, singleton: true, instance: nil }
37
+ end
38
+
29
39
  # Resolve a service by name.
30
- # Lazy factories are called once and memoized.
40
+ # Singletons and concrete instances return the same object each time.
41
+ # Transient factories (register with block) return a new object each time.
31
42
  def resolve(name)
32
43
  entry = registry[name.to_sym]
33
44
  raise KeyError, "service not registered: #{name}" unless entry
34
45
 
35
- if entry[:instance]
36
- entry[:instance]
37
- elsif entry[:factory]
38
- entry[:instance] = entry[:factory].call
46
+ # Concrete instance (register with value)
47
+ return entry[:instance] if entry[:instance] && entry[:factory].nil?
48
+
49
+ if entry[:factory]
50
+ if entry[:singleton]
51
+ # Singleton — call once, memoize
52
+ entry[:instance] ||= entry[:factory].call
53
+ entry[:instance]
54
+ else
55
+ # Transient — call every time
56
+ entry[:factory].call
57
+ end
58
+ else
39
59
  entry[:instance]
40
60
  end
41
61
  end
data/lib/tina4/router.rb CHANGED
@@ -194,6 +194,14 @@ module Tina4
194
194
  @routes ||= []
195
195
  end
196
196
 
197
+ def get_routes
198
+ routes
199
+ end
200
+
201
+ def list_routes
202
+ routes
203
+ end
204
+
197
205
  # Registered WebSocket routes
198
206
  def ws_routes
199
207
  @ws_routes ||= []
@@ -283,6 +291,11 @@ module Tina4
283
291
  nil
284
292
  end
285
293
 
294
+ # Alias for find_route().
295
+ def match(path, method)
296
+ find_route(path, method)
297
+ end
298
+
286
299
  # Register a class-based middleware globally.
287
300
  # The class should define static before_* and/or after_* methods.
288
301
  # Example:
data/lib/tina4/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Tina4
4
- VERSION = "3.10.38"
4
+ VERSION = "3.10.39"
5
5
  end
data/lib/tina4.rb CHANGED
@@ -382,6 +382,10 @@ module Tina4
382
382
  Tina4::Container.register(name, instance, &block)
383
383
  end
384
384
 
385
+ def singleton(name, &block)
386
+ Tina4::Container.singleton(name, &block)
387
+ end
388
+
385
389
  def resolve(name)
386
390
  Tina4::Container.resolve(name)
387
391
  end
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: 3.10.38
4
+ version: 3.10.39
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tina4 Team