inoculate 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b26bc8e397a1e50bb29e716bd1c4abb531f9267765960b936ce03ba8449ab7e5
4
- data.tar.gz: e09a3d2235a100c7723596614845a4faba198261bbecfe979ebdf3b209b7d325
3
+ metadata.gz: e63449f9ee05019e70acf85faafd0f2adf6554704f560de31b66b12c01e54aeb
4
+ data.tar.gz: e6975710a5802d37bd4b84de1345dbd10a10a5cf8538096cd8ffea8459997af9
5
5
  SHA512:
6
- metadata.gz: d6a281590288dec453e6adf338f5b9461160d07ea35c8bdb3914a464630dd76d46ecfe3c2d16099d936e0f8c04159c0e1200cd0f7c65cf426e74a844492be8c1
7
- data.tar.gz: 27c2596ce1a7e2e0c24bdcaf8cb83272a71f0f5e5a3addf8caac8aa082f5b13448f0af66226c45162deb96a6a43cc61d3addf052253aac84ec250002f827ef7d
6
+ metadata.gz: 95e7e8d09278118af12987096c2919419a66b6ea298d6556a3e7df113403e0fa4edd363a1d19172eef8d4739ffdf10b4e8180b19151cdd19361d3c2ccd861200
7
+ data.tar.gz: 9dd927feda03995336037f58b86e9c97be8e2bf83f4ac8a101ee67b10e0d40106bdf0f62a138ef046135ef24f759074c289ef61153c8044d02db08b63e4b20be
data/CHANGELOG.md CHANGED
@@ -1,8 +1,12 @@
1
- ## v0.1.0 — 2022-10-10
1
+ ## v0.2.0 \[2022-10-11]
2
+
3
+ * Build modules at initialization time so that usage is always thread-safe.
4
+
5
+ ## v0.1.0 \[2022-10-10]
2
6
 
3
7
  * Add transient dependency registration.
4
8
  * Add initialization process for gem.
5
9
 
6
- ## v0.0.0 — 2022-10-05
10
+ ## v0.0.0 \[2022-10-05]
7
11
 
8
12
  * Initial gem setup.
data/README.md CHANGED
@@ -3,6 +3,8 @@
3
3
  Inoculate is a small, thread-safe dependency injection library configured entirely with Ruby.
4
4
  It provides several life-cycles and provides dependency access through private accessors.
5
5
 
6
+ [![Gem Version](https://badge.fury.io/rb/inoculate.svg)](https://badge.fury.io/rb/inoculate)
7
+
6
8
  ---
7
9
 
8
10
  ## What's in the box?
@@ -7,8 +7,6 @@
7
7
  module Inoculate
8
8
  # The main configuration entrypoint for Inoculate. Use this to set up named dependencies.
9
9
  #
10
- # @note This method is not thread safe.
11
- #
12
10
  # @example A simple dependency configuration
13
11
  # Inoculate.initialize do |config|
14
12
  # config.transient(:http) { Faraday }
@@ -4,7 +4,6 @@ require "digest"
4
4
 
5
5
  module Inoculate
6
6
  # Registers and builds dependency injection modules.
7
- # @todo building needs to be thread-safe
8
7
  # @todo singleton life cycle
9
8
  # @todo instance life cycle
10
9
  # @todo thread singleton life cycle
@@ -52,34 +51,25 @@ module Inoculate
52
51
  raise Errors::AlreadyRegistered if @registered_blueprints.has_key? name
53
52
  raise Errors::RequiresCallable unless builder.respond_to?(:call) || block
54
53
 
55
- @registered_blueprints[name.to_sym] = {lifecycle: :transient, builder: builder || block, accessor_module: nil}
54
+ blueprint_name = name.to_sym
55
+ @registered_blueprints[blueprint_name] = {
56
+ lifecycle: :transient,
57
+ builder: builder || block,
58
+ accessor_module: build_module(blueprint_name, :transient, builder || block)
59
+ }
56
60
  end
57
61
 
58
- # Build the accessor module associated with a dependency name.
59
- #
60
- # @param name [Symbol] the dependency name to build an accessor module for
61
- #
62
- # @raise [Errors::UnknownName] if the dependency name is not registered
63
- #
64
- # @return [Module] the accessor module for accessing instances of the dependency
65
- #
66
- # @since 0.1.0
67
- def build(name)
68
- blueprint = @registered_blueprints[name]
69
- raise Errors::UnknownName if blueprint.nil?
70
- return blueprint[:accessor_module] unless blueprint[:accessor_module].nil?
62
+ private
71
63
 
64
+ def build_module(name, lifecycle, builder)
72
65
  module_name = "I#{Digest::SHA1.hexdigest(name.to_s)}"
73
- builder = blueprint[:builder]
74
- blueprint[:accessor_module] = Providers.module_eval do
66
+ Providers.module_eval do
75
67
  const_set(module_name, Module.new do
76
68
  private define_method(name) { builder.call }
77
69
  end)
78
70
  end
79
71
  end
80
72
 
81
- private
82
-
83
73
  def validate_builder_name(name)
84
74
  raise Errors::InvalidName, "name must be a symbol or convert to one" unless name.respond_to? :to_sym
85
75
  begin
@@ -37,7 +37,10 @@ module Inoculate
37
37
  m = Module.new do
38
38
  define_method(method_name) do |*names|
39
39
  names.each do |name|
40
- include(Inoculate.manufacturer.build(name))
40
+ mod = Inoculate.manufacturer.registered_blueprints.dig(name, :accessor_module)
41
+ raise Errors::UnknownName if mod.nil?
42
+
43
+ include(mod)
41
44
  end
42
45
  end
43
46
  end
@@ -2,5 +2,5 @@
2
2
 
3
3
  module Inoculate
4
4
  # The library version.
5
- VERSION = "0.1.0"
5
+ VERSION = "0.2.0"
6
6
  end
data/sig/inoculate.rbs CHANGED
@@ -33,11 +33,6 @@ module Inoculate
33
33
  class Manufacturer
34
34
  attr_reader registered_blueprints: Hash[builder_name, blueprint]
35
35
  def transient: (builder_name | _ToSymbol, callable?) ?{ () -> void } -> void
36
- def build: (builder_name) -> Module
37
-
38
- private
39
-
40
- def validate_builder_name: (builder_name) -> void
41
36
  end
42
37
 
43
38
  class Configurer
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: inoculate
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Stephan Tarulli
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-10-10 00:00:00.000000000 Z
11
+ date: 2022-10-12 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: |
14
14
  Inoculate is a small, thread-safe dependency injection library configured entirely with Ruby.