karafka-core 2.2.3 → 2.2.4

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: d1dc79b27db2c0435b33651aa0e9431206f5154c7f1666888323d9c0c5ca4c50
4
- data.tar.gz: 00e3d7694bb29cb286a926cdae3f2690a08f441b560b8a86289ec4dac4d8ddef
3
+ metadata.gz: 5725a5489ddec63bb880cba5cb2b2e69394f3aafeb573892c380c475812c2aa8
4
+ data.tar.gz: 4eafb4c97db61bae7373bed200f64329b440eae496d5712bd37374e0a20134dc
5
5
  SHA512:
6
- metadata.gz: 80bfce9c07d877da97ecf1d1f0cf5c2e628d265dd38a9134654ef87d2590e431f4a18501a5a56193cdc5daa6593f10023bba12364df8fcd6f2f41d6cfedf9142
7
- data.tar.gz: 249c81d7588bd7824a474bd0fd679d7840b13e21ed7bf754b0840a5075daf675d9ef758720b73adea91f50ccc328b59c6907768bc48f7e58df1b2b416c9b03d5
6
+ metadata.gz: 5e9f1322799d2ecc16f930a1a2cf80c3e122b25101a32a8f3899e78df0ef1aba2dd4b97b7959c69c348d23cdaba2a65a287548a26022056f7b04714579df34f3
7
+ data.tar.gz: 13f54f8a7e697ce744ace8f5932d5ddeacb1a28fb181e8a1ac53e66e236b482bf3e3c3823161b33ed6e852f4301435d7b3d6ac5dc930450e27d93781f9c9dbf8
checksums.yaml.gz.sig CHANGED
Binary file
data/CHANGELOG.md CHANGED
@@ -1,5 +1,9 @@
1
1
  # Karafka core changelog
2
2
 
3
+ ### 2.2.4 (2023-10-25)
4
+ - [Enhancement] Allow for `lazy` evaluated constructors.
5
+ - [Enhancement] Allow no-arg constructors.
6
+
3
7
  ### 2.2.3 (2023-10-17)
4
8
  - [Change] Set minimum `karafka-rdkafka` on `0.13.6`.
5
9
 
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- karafka-core (2.2.3)
4
+ karafka-core (2.2.4)
5
5
  concurrent-ruby (>= 1.1)
6
6
  karafka-rdkafka (>= 0.13.6, < 0.14.0)
7
7
 
data/karafka-core.gemspec CHANGED
@@ -32,7 +32,7 @@ Gem::Specification.new do |spec|
32
32
  spec.metadata = {
33
33
  'funding_uri' => 'https://karafka.io/#become-pro',
34
34
  'homepage_uri' => 'https://karafka.io',
35
- 'changelog_uri' => 'https://github.com/karafka/karafka-core/blob/master/CHANGELOG.md',
35
+ 'changelog_uri' => 'https://karafka.io/docs/Changelog-Karafka-Core',
36
36
  'bug_tracker_uri' => 'https://github.com/karafka/karafka-core/issues',
37
37
  'source_code_uri' => 'https://github.com/karafka/karafka-core',
38
38
  'documentation_uri' => 'https://karafka.io/docs',
@@ -4,11 +4,16 @@ module Karafka
4
4
  module Core
5
5
  module Configurable
6
6
  # Single end config value representation
7
- Leaf = Struct.new(:name, :default, :constructor, :compiled) do
7
+ Leaf = Struct.new(:name, :default, :constructor, :compiled, :lazy) do
8
8
  # @return [Boolean] true if already compiled
9
9
  def compiled?
10
10
  compiled
11
11
  end
12
+
13
+ # @return [Boolean] is this a lazy evaluated leaf
14
+ def lazy?
15
+ lazy == true
16
+ end
12
17
  end
13
18
  end
14
19
  end
@@ -30,12 +30,13 @@ module Karafka
30
30
  # @param name [Symbol] setting or nested node name
31
31
  # @param default [Object] default value
32
32
  # @param constructor [#call, nil] callable or nil
33
+ # @param lazy [Boolean] is this a lazy leaf
33
34
  # @param block [Proc] block for nested settings
34
- def setting(name, default: nil, constructor: nil, &block)
35
+ def setting(name, default: nil, constructor: nil, lazy: false, &block)
35
36
  @children << if block
36
37
  Node.new(name, block)
37
38
  else
38
- Leaf.new(name, default, constructor, false)
39
+ Leaf.new(name, default, constructor, false, lazy)
39
40
  end
40
41
 
41
42
  compile
@@ -95,27 +96,75 @@ module Karafka
95
96
  @children.each do |value|
96
97
  # Do not redefine something that was already set during compilation
97
98
  # This will allow us to reconfigure things and skip override with defaults
98
- skippable = respond_to?(value.name)
99
+ skippable = respond_to?(value.name) || (value.is_a?(Leaf) && value.compiled?)
100
+ lazy_leaf = value.is_a?(Leaf) && value.lazy?
99
101
 
100
- singleton_class.attr_accessor value.name
102
+ # Do not create accessor for leafs that are lazy as they will get a custom method
103
+ # created instead
104
+ singleton_class.attr_accessor(value.name) unless lazy_leaf
101
105
 
102
106
  next if skippable
103
107
 
104
108
  initialized = if value.is_a?(Leaf)
105
- next if value.compiled?
106
-
107
109
  value.compiled = true
108
- value.constructor ? value.constructor.call(value.default) : value.default
110
+
111
+ if value.constructor && value.lazy?
112
+ false
113
+ elsif value.constructor
114
+ call_constructor(value)
115
+ else
116
+ value.default
117
+ end
109
118
  else
110
119
  value.compile
111
120
  value
112
121
  end
113
122
 
114
- public_send("#{value.name}=", initialized)
123
+ if lazy_leaf && !initialized
124
+ build_dynamic_accessor(value)
125
+ else
126
+ public_send("#{value.name}=", initialized)
127
+ end
115
128
  end
116
129
 
117
130
  @compiled = true
118
131
  end
132
+
133
+ private
134
+
135
+ # Defines a lazy evaluated read and writer that will re-evaluate in case value constructor
136
+ # evaluates to `nil` or `false`. This allows us to define dynamic constructors that
137
+ # can react to external conditions to become expected value once this value is
138
+ # available
139
+ #
140
+ # @param value [Leaf]
141
+ def build_dynamic_accessor(value)
142
+ singleton_class.attr_writer(value.name)
143
+
144
+ define_singleton_method(value.name) do
145
+ existing = instance_variable_get("@#{value.name}")
146
+
147
+ return existing if existing
148
+
149
+ built = call_constructor(value)
150
+
151
+ instance_variable_set("@#{value.name}", built)
152
+ end
153
+ end
154
+
155
+ # Runs the constructor with or without the default depending on its arity and returns the
156
+ # result
157
+ #
158
+ # @param value [Leaf]
159
+ def call_constructor(value)
160
+ constructor = value.constructor
161
+
162
+ if constructor.arity.zero?
163
+ constructor.call
164
+ else
165
+ constructor.call(value.default)
166
+ end
167
+ end
119
168
  end
120
169
  end
121
170
  end
@@ -65,14 +65,14 @@ module Karafka
65
65
 
66
66
  # Two versions are needed to pass arguments in the correct way
67
67
  if RUBY_VERSION >= '2.7'
68
- class_eval <<~CODE, __FILE__, __LINE__
68
+ class_eval <<~CODE, __FILE__, __LINE__ + 1
69
69
  # Pipes the settings setup to the config root node
70
70
  def setting(...)
71
71
  config.setting(...)
72
72
  end
73
73
  CODE
74
74
  else
75
- class_eval <<~CODE, __FILE__, __LINE__
75
+ class_eval <<~CODE, __FILE__, __LINE__ + 1
76
76
  # Pipes the settings setup to the config root node
77
77
  # @param args [Object] anything provided to settings
78
78
  # @param block [Proc] block for settings
@@ -4,6 +4,6 @@ module Karafka
4
4
  module Core
5
5
  # Current Karafka::Core version
6
6
  # We follow the versioning schema of given Karafka version
7
- VERSION = '2.2.3'
7
+ VERSION = '2.2.4'
8
8
  end
9
9
  end
data.tar.gz.sig CHANGED
Binary file
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: karafka-core
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.2.3
4
+ version: 2.2.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Maciej Mensfeld
@@ -35,7 +35,7 @@ cert_chain:
35
35
  AnG1dJU+yL2BK7vaVytLTstJME5mepSZ46qqIJXMuWob/YPDmVaBF39TDSG9e34s
36
36
  msG3BiCqgOgHAnL23+CN3Rt8MsuRfEtoTKpJVcCfoEoNHOkc
37
37
  -----END CERTIFICATE-----
38
- date: 2023-10-17 00:00:00.000000000 Z
38
+ date: 2023-10-25 00:00:00.000000000 Z
39
39
  dependencies:
40
40
  - !ruby/object:Gem::Dependency
41
41
  name: concurrent-ruby
@@ -128,7 +128,7 @@ licenses:
128
128
  metadata:
129
129
  funding_uri: https://karafka.io/#become-pro
130
130
  homepage_uri: https://karafka.io
131
- changelog_uri: https://github.com/karafka/karafka-core/blob/master/CHANGELOG.md
131
+ changelog_uri: https://karafka.io/docs/Changelog-Karafka-Core
132
132
  bug_tracker_uri: https://github.com/karafka/karafka-core/issues
133
133
  source_code_uri: https://github.com/karafka/karafka-core
134
134
  documentation_uri: https://karafka.io/docs
metadata.gz.sig CHANGED
Binary file