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 +4 -4
- data/lib/tina4/container.rb +31 -11
- data/lib/tina4/router.rb +13 -0
- data/lib/tina4/version.rb +1 -1
- data/lib/tina4.rb +4 -0
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 6d61b5cbf67e0703b569e23d10d160e817c0a2ab39e40afc79970d42b4934ef8
|
|
4
|
+
data.tar.gz: 1c07bde0df2774c7c8105f9ecd90aeb0863e83ad935e89500eda76cf9d638602
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 7134b5ad784722e27d00bee27dd02a5af60f7e096a38ea7a0a740b1a332d3c12b00cd01b3ee586aa23c2162d7a803505b79447868d7101b5c106ebb1a4e985ef
|
|
7
|
+
data.tar.gz: ef891151367dfdbed749fb164cff04a8c3884731763647a660ce7f40b61e33fe6cf23c711c79dfc433ecea5af2c80285cdaebe1ac0f4268b850528467dc1c19b
|
data/lib/tina4/container.rb
CHANGED
|
@@ -3,9 +3,10 @@
|
|
|
3
3
|
module Tina4
|
|
4
4
|
# Lightweight dependency injection container.
|
|
5
5
|
#
|
|
6
|
-
# Tina4.register(:mailer
|
|
7
|
-
# Tina4.
|
|
8
|
-
# Tina4.
|
|
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
|
|
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
|
-
#
|
|
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
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
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
data/lib/tina4.rb
CHANGED