Skip to content

Commit 92fe970

Browse files
committed
Add new option. Review.
1 parent 40b0fef commit 92fe970

File tree

6 files changed

+49
-54
lines changed

6 files changed

+49
-54
lines changed

lib/table_sync/receiving/config.rb

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# frozen_string_literal: true
22

3+
require_relative "hooks/once"
4+
35
module TableSync::Receiving
46
class Config
57
attr_reader :model, :events
@@ -53,19 +55,23 @@ def add_option(name, value_setter_wrapper:, value_as_proc_setter_wrapper:, defau
5355
end
5456
end
5557

56-
def add_simple_option(name)
58+
def add_hook_option(name, hook_class:)
5759
ivar = :"@#{name}"
5860

59-
@default_values_for_options ||= {}
60-
@default_values_for_options[ivar] = proc { [nil, proc {}] }
61-
62-
define_method(name) do |options = nil, &block|
63-
old_options, old_block = instance_variable_get(ivar)
61+
default_conditions = { columns: %i[] }
62+
default_handler = proc { |**_| }
6463

65-
new_options = options || old_options
66-
new_block = block || old_block
64+
@default_values_for_options ||= {}
65+
@default_values_for_options[ivar] = proc do
66+
hook_class.new(
67+
conditions: default_conditions,
68+
handler: default_handler,
69+
)
70+
end
6771

68-
instance_variable_set(ivar, [new_options, new_block])
72+
define_method(name) do |conditions, &handler|
73+
hook = hook_class.new(conditions:, handler:)
74+
instance_variable_set(ivar, hook)
6975
end
7076
end
7177
end
@@ -218,7 +224,10 @@ def option(name)
218224
value_as_proc_setter_wrapper: any_value,
219225
default: proc { proc { |&block| block.call } }
220226

221-
TableSync::Receiving::Config.add_simple_option :on_first_sync
227+
TableSync::Receiving::Config.add_hook_option(
228+
:on_first_sync,
229+
hook_class: TableSync::Receiving::Hooks::Once,
230+
)
222231

223232
%i[
224233
before_update

lib/table_sync/receiving/handler.rb

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -161,15 +161,8 @@ def perform(config, params) # rubocop:disable Metrics/MethodLength
161161
model.after_commit do
162162
config.option(:after_commit_on_update, **params, results:)
163163

164-
# In case the `on_first_sync` option is not configured
165-
# we'll have default value:
166-
# [nil, proc {}]
167-
# Default value is treated as the option is disabled.
168-
conditions, handler = config.option(:on_first_sync)
169-
if conditions.present?
170-
hook = ::TableSync::Receiving::Hooks::Once.new(conditions:, config:)
171-
hook.perform(targets: results, &handler)
172-
end
164+
hook = config.option(:on_first_sync)
165+
hook.perform(config:, targets: results) if hook.enabled?
173166
end
174167
else
175168
model.after_commit { config.option(:after_commit_on_destroy, **params, results:) }

lib/table_sync/receiving/hooks/once.rb

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,19 @@
22

33
module TableSync::Receiving::Hooks
44
class Once
5-
attr_reader :conditions, :config
5+
attr_reader :conditions, :handler, :lookup_code
66

7-
def initialize(conditions:, config:)
7+
def initialize(conditions:, handler:)
88
@conditions = conditions
9-
@config = config
9+
@handler = handler
10+
init_lookup_code
1011
end
1112

12-
def perform(targets:, &)
13+
def enabled?
14+
conditions[:columns].any?
15+
end
16+
17+
def perform(config:, targets:)
1318
target_keys = config.option(:target_keys)
1419
model = config.model
1520

@@ -21,8 +26,8 @@ def perform(targets:, &)
2126
next unless allow?(entry)
2227

2328
entry.hooks ||= []
24-
entry.hooks << hook_lookup_code
25-
model.after_commit { yield(entry:) }
29+
entry.hooks << lookup_code
30+
model.after_commit { handler.call(entry:) }
2631
end
2732
end
2833
end
@@ -31,11 +36,11 @@ def perform(targets:, &)
3136
private
3237

3338
def allow?(entry)
34-
Array(entry.hooks).exclude?(hook_lookup_code)
39+
Array(entry.hooks).exclude?(lookup_code)
3540
end
3641

37-
def hook_lookup_code
38-
@hook_lookup_code ||= conditions[:columns].map do |column|
42+
def init_lookup_code
43+
@lookup_code = conditions[:columns].map do |column|
3944
"#{column}-#{conditions[column]}"
4045
end.join(":")
4146
end

lib/table_sync/receiving/model/active_record.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ class ActiveRecord
88
repeatable: :repeatable_read,
99
serializable: :serializable,
1010
}.freeze
11-
private_constant :ISOLATION_LEVELS
1211

1312
class AfterCommitWrap
1413
def initialize(&block)

lib/table_sync/receiving/model/sequel.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ class Sequel
1010
repeatable: :repeatable,
1111
serializable: :serializable,
1212
}.freeze
13-
private_constant :ISOLATION_LEVELS
1413

1514
def initialize(table_name)
1615
@raw_model = Class.new(::Sequel::Model(table_name)).tap(&:unrestrict_primary_key)

spec/receiving/config_spec.rb

Lines changed: 14 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -351,33 +351,23 @@
351351

352352
describe "#on_first_sync" do
353353
it "returns correct default value" do
354-
value, callable = config.option(:on_first_sync)
355-
expect(value).to be_nil
356-
expect(callable).to be_a(Proc)
354+
hook = config.option(:on_first_sync)
355+
expect(hook).not_to be_nil
356+
expect(hook).not_to be_enabled
357+
expect(hook.conditions[:columns]).to be_empty
358+
expect(hook.lookup_code).to eq("")
357359
end
358360

359-
it "processes a single value" do
360-
config.on_first_sync :test
361-
362-
value, callable = config.option(:on_first_sync)
363-
expect(value).to eq(:test)
364-
expect(callable).to be_a(Proc)
365-
end
366-
367-
it "processes a single block" do
368-
config.on_first_sync { :test }
369-
370-
value, callable = config.option(:on_first_sync)
371-
expect(value).to be_nil
372-
expect(callable.call).to eq(:test)
373-
end
374-
375-
it "processes a value and a block" do
376-
config.on_first_sync(:spam) { :test }
361+
it "processes a value" do
362+
config.on_first_sync(columns: %i[test], test: "value") do |**_|
363+
# Some hook work here
364+
end
377365

378-
value, callable = config.option(:on_first_sync)
379-
expect(value).to eq(:spam)
380-
expect(callable.call).to eq(:test)
366+
hook = config.option(:on_first_sync)
367+
expect(hook).not_to be_nil
368+
expect(hook).to be_enabled
369+
expect(hook.conditions[:columns]).not_to be_empty
370+
expect(hook.lookup_code).to eq("test-value")
381371
end
382372
end
383373

0 commit comments

Comments
 (0)