containable 2.2.0 → 2.2.1
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
- checksums.yaml.gz.sig +0 -0
- data/README.adoc +40 -5
- data/containable.gemspec +1 -1
- data/lib/containable/builder.rb +12 -0
- data/lib/containable/register.rb +5 -3
- data/lib/containable/resolver.rb +1 -1
- data/lib/containable.rb +0 -10
- data.tar.gz.sig +0 -0
- metadata +2 -2
- metadata.gz.sig +0 -0
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 014d1582fc52d294c3c860f611866683ec3308dd0f95c23083defc93b8db0f8a
|
|
4
|
+
data.tar.gz: 321b1a573df10500ddbc4eede5503537caee0cb3380cfd296eb6baf68c478f4e
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 3dd07f1911ceb4df23b10dcdbf5364c0391a6341e5869a0f1dc94c619b493c1acaec058d315d1bc3b6c2c93dd6a2c5c5c9e23e80366098ee4e5bd99a7263a3dd
|
|
7
|
+
data.tar.gz: 7f3d378b0e9a2ecb0385e50ffcd07f9054e559104e5ebd3f2ca3ad17f1c4999107d5748aeb0a316e67742ac594901b649f8a0490cc2726dc913fc37e498a07f2
|
checksums.yaml.gz.sig
CHANGED
|
Binary file
|
data/README.adoc
CHANGED
|
@@ -407,7 +407,7 @@ Container.clone.frozen? # true
|
|
|
407
407
|
|
|
408
408
|
=== Customization
|
|
409
409
|
|
|
410
|
-
You can customize how the container registers and resolves dependencies by creating your own register and resolver. Internally,
|
|
410
|
+
You can customize how the container registers and resolves dependencies by creating your own register and resolver classes. Both classes should inherit from either `Containable::Register` or `Containable::Resolver`. Internally, each subclass has access to the `dependencies` (i.e. `Concurrent::Hash`) object which stores the registered key and associated value (tuple). Example:
|
|
411
411
|
|
|
412
412
|
[source,ruby]
|
|
413
413
|
----
|
|
@@ -417,9 +417,13 @@ You can customize how the container registers and resolves dependencies by creat
|
|
|
417
417
|
}
|
|
418
418
|
----
|
|
419
419
|
|
|
420
|
-
Each tuple captures the dependency (value) and directive (i.e. `:cache` or `:fresh`). This allows you to have access to all information captured at registration.
|
|
420
|
+
Each tuple captures the dependency (value) and directive (i.e. `:cache` or `:fresh`). This allows you to have access to all information captured at registration. Definitely read the source code of each class to learn more.
|
|
421
421
|
|
|
422
|
-
|
|
422
|
+
The following provides a few examples on how you can customize further.
|
|
423
|
+
|
|
424
|
+
==== Registers
|
|
425
|
+
|
|
426
|
+
Here's an example of a custom register that doesn't care if you override an existing key.
|
|
423
427
|
|
|
424
428
|
[source,ruby]
|
|
425
429
|
----
|
|
@@ -441,6 +445,39 @@ end
|
|
|
441
445
|
Container[:one] # "override"
|
|
442
446
|
----
|
|
443
447
|
|
|
448
|
+
Alternatively, you could specify which keys are allowed to be overwritten. Here's a modification to the above example that would make this possible:
|
|
449
|
+
|
|
450
|
+
[source,ruby]
|
|
451
|
+
----
|
|
452
|
+
require "containable"
|
|
453
|
+
require "refinements/array"
|
|
454
|
+
|
|
455
|
+
class Register < Containable::Register
|
|
456
|
+
using Refinements::Array
|
|
457
|
+
|
|
458
|
+
def initialize(*, allowed_keys: %w[http logger], **)
|
|
459
|
+
super(*, **)
|
|
460
|
+
@allowed_keys = allowed_keys
|
|
461
|
+
end
|
|
462
|
+
|
|
463
|
+
protected
|
|
464
|
+
|
|
465
|
+
def check_duplicate key, namespaced_key
|
|
466
|
+
return if allowed_keys.include? namespaced_key
|
|
467
|
+
|
|
468
|
+
message = "Dependency is already registered: #{key.inspect}."
|
|
469
|
+
|
|
470
|
+
fail KeyError, message if dependencies.key? namespaced_key
|
|
471
|
+
end
|
|
472
|
+
|
|
473
|
+
private
|
|
474
|
+
|
|
475
|
+
attr_reader :allowed_keys
|
|
476
|
+
end
|
|
477
|
+
----
|
|
478
|
+
|
|
479
|
+
==== Resolvers
|
|
480
|
+
|
|
444
481
|
Here's an example with a custom resolver that only allows specific keys to be resolved:
|
|
445
482
|
|
|
446
483
|
[source,ruby]
|
|
@@ -477,8 +514,6 @@ Container[:two] # Only use these keys: [:one, :three] (KeyError)
|
|
|
477
514
|
Container[:three] # 3
|
|
478
515
|
----
|
|
479
516
|
|
|
480
|
-
In both cases, you only need to inject your custom register or resolver when extending your container with `Containable`. Both of these classes should inherit from either `Containable::Register` or `Containable::Resolver` to customize behavior as you like. Definitely read the source code of both these classes to learn more.
|
|
481
|
-
|
|
482
517
|
=== Infusible
|
|
483
518
|
|
|
484
519
|
To fully leverage the power of this gem, check out {infusible_link}. You can get far with simple containers but if you want to supercharge your containers and make your architecture truly come alive then make sure to couple this gem with the {infusible_link} gem. 🚀
|
data/containable.gemspec
CHANGED
data/lib/containable/builder.rb
CHANGED
|
@@ -89,5 +89,17 @@ module Containable
|
|
|
89
89
|
def define_frozen?
|
|
90
90
|
define_method(:frozen?) { dependencies.frozen? }
|
|
91
91
|
end
|
|
92
|
+
|
|
93
|
+
def define_stub
|
|
94
|
+
define_method :stub! do |**keywords|
|
|
95
|
+
require "containable/test"
|
|
96
|
+
|
|
97
|
+
extend Test
|
|
98
|
+
|
|
99
|
+
stub(**keywords)
|
|
100
|
+
end
|
|
101
|
+
end
|
|
102
|
+
|
|
103
|
+
def define_restore = define_method(:restore) { false }
|
|
92
104
|
end
|
|
93
105
|
end
|
data/lib/containable/register.rb
CHANGED
|
@@ -35,12 +35,14 @@ module Containable
|
|
|
35
35
|
visit(&)
|
|
36
36
|
end
|
|
37
37
|
|
|
38
|
-
|
|
38
|
+
protected
|
|
39
39
|
|
|
40
40
|
attr_reader :dependencies, :separator, :directives
|
|
41
41
|
|
|
42
42
|
attr_accessor :keys, :depth
|
|
43
43
|
|
|
44
|
+
def namespacify(key) = keys[..depth].append(key).join separator
|
|
45
|
+
|
|
44
46
|
def check_duplicate key, namespaced_key
|
|
45
47
|
message = "Dependency is already registered: #{key.inspect}."
|
|
46
48
|
|
|
@@ -54,6 +56,8 @@ module Containable
|
|
|
54
56
|
%(Invalid directive: #{value.inspect}. Use #{directives.map(&:inspect).join " or "}.)
|
|
55
57
|
end
|
|
56
58
|
|
|
59
|
+
private
|
|
60
|
+
|
|
57
61
|
def visit &block
|
|
58
62
|
increment
|
|
59
63
|
instance_eval(&block) if block
|
|
@@ -64,7 +68,5 @@ module Containable
|
|
|
64
68
|
def increment = self.depth += 1
|
|
65
69
|
|
|
66
70
|
def decrement = self.depth -= 1
|
|
67
|
-
|
|
68
|
-
def namespacify(key) = keys[..depth].append(key).join separator
|
|
69
71
|
end
|
|
70
72
|
end
|
data/lib/containable/resolver.rb
CHANGED
data/lib/containable.rb
CHANGED
data.tar.gz.sig
CHANGED
|
Binary file
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: containable
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 2.2.
|
|
4
|
+
version: 2.2.1
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Brooke Kuhlmann
|
|
@@ -90,7 +90,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
90
90
|
- !ruby/object:Gem::Version
|
|
91
91
|
version: '0'
|
|
92
92
|
requirements: []
|
|
93
|
-
rubygems_version: 4.0.
|
|
93
|
+
rubygems_version: 4.0.11
|
|
94
94
|
specification_version: 4
|
|
95
95
|
summary: A thread-safe dependency injection container.
|
|
96
96
|
test_files: []
|
metadata.gz.sig
CHANGED
|
Binary file
|