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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 49be2a5c56c8ad8cd53ce41a6498bdb8aa47ab042cb123660dae6936ff4daade
4
- data.tar.gz: dc75daed9d1f05881d54164e76e0e57a505a9d0699dc49bd1fde20f698dc6f45
3
+ metadata.gz: 014d1582fc52d294c3c860f611866683ec3308dd0f95c23083defc93b8db0f8a
4
+ data.tar.gz: 321b1a573df10500ddbc4eede5503537caee0cb3380cfd296eb6baf68c478f4e
5
5
  SHA512:
6
- metadata.gz: 4a67b2f11f343e08f9e351605e9721dbd302db977c4cd0ba6146235cca362e8b348be834f274c665669de8102659445519eb08f19ddaa54d948e09613d816b18
7
- data.tar.gz: 3cf88215110cc8e90f84280df6bbcc764744454b8b58a4a9cbe53bd115a4d78824cff6a5dcb08927cacafa33fc062298fa8ccd38efacdc4d8a17e23c4d10fe45
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, both of these objects have access to and use `dependencies` (i.e. `Concurrent::Hash`) which stores the registered key and tuple. Example:
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. Below are a few examples on how to use and customize this information for your own purposes.
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
- Here's how to use a custom register that doesn't care if you override an existing key.
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
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |spec|
4
4
  spec.name = "containable"
5
- spec.version = "2.2.0"
5
+ spec.version = "2.2.1"
6
6
  spec.authors = ["Brooke Kuhlmann"]
7
7
  spec.email = ["brooke@alchemists.io"]
8
8
  spec.homepage = "https://alchemists.io/projects/containable"
@@ -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
@@ -35,12 +35,14 @@ module Containable
35
35
  visit(&)
36
36
  end
37
37
 
38
- private
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
@@ -18,7 +18,7 @@ module Containable
18
18
  process key, value, as
19
19
  end
20
20
 
21
- private
21
+ protected
22
22
 
23
23
  attr_reader :dependencies
24
24
 
data/lib/containable.rb CHANGED
@@ -12,14 +12,4 @@ module Containable
12
12
  end
13
13
 
14
14
  def self.[](register: Register, resolver: Resolver) = Builder.new(register:, resolver:)
15
-
16
- def stub!(**)
17
- require "containable/test"
18
-
19
- extend Test
20
-
21
- stub(**)
22
- end
23
-
24
- def restore = false
25
15
  end
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.0
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.10
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