Skip to content

Commit 6754afe

Browse files
committed
backup
1 parent 9b24456 commit 6754afe

File tree

10 files changed

+127
-125
lines changed

10 files changed

+127
-125
lines changed

lib/trailblazer/macro.rb

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,21 +26,20 @@ def initialize(return_value_step, variable_name:)
2626
@variable_name = variable_name
2727
end
2828

29-
def call((ctx, flow_options), **circuit_options)
30-
return_value, ctx = @return_value_step.([ctx, flow_options], **circuit_options)
29+
def call(ctx, flow_options, circuit_options)
30+
puts "yooo"
31+
ctx, flow_options, return_value = @return_value_step.(ctx, flow_options, circuit_options)
3132

3233
ctx[@variable_name] = return_value
3334

34-
return return_value, ctx
35+
return ctx, flow_options, Trailblazer::Activity::Right
3536
end
3637
end
3738

3839
def self.task_adapter_for_decider(decider_with_step_interface, variable_name:)
39-
return_value_circuit_step = Activity::Circuit.Step(decider_with_step_interface, option: true)
40+
return_value_circuit_step = Activity::Circuit.Step(decider_with_step_interface) # FIXME: this should be binary!
4041

41-
assign_task = AssignVariable.new(return_value_circuit_step, variable_name: variable_name)
42-
43-
Activity::Circuit::TaskAdapter.new(assign_task) # call {assign_task} with circuit-interface, interpret result.
42+
AssignVariable.new(return_value_circuit_step, variable_name: variable_name)
4443
end
4544

4645
def self.block_activity_for(block_activity, &block)

lib/trailblazer/macro/guard.rb

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@ def self.Guard(proc, name: :default, &block)
66

77
module Guard
88
def self.build(callable)
9-
option = Trailblazer::Option(callable)
9+
option = Trailblazer::Activity::Circuit::Step(callable) # DISCUSS: should we use Option here directly?
1010

11-
->((ctx, *), **circuit_args) do
12-
Trailblazer::Operation::Result.new(!!option.call(ctx, keyword_arguments: ctx.to_hash, **circuit_args), {})
11+
->(ctx, flow_options, circuit_options) do
12+
_, _, result = option.call(ctx, flow_options, circuit_options)
13+
14+
Trailblazer::Operation::Result.new(!!result, {})
1315
end
1416
end
1517
end

lib/trailblazer/macro/model.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ module Trailblazer
22
module Macro
33

44
def self.Model(model_class = nil, action = :new, find_by_key = :id, id: 'model.build', not_found_terminus: false)
5-
task = Activity::Circuit::TaskAdapter.for_step(Model.new)
5+
task = Activity::Circuit.Step(Model.new, binary: true)
66

77
injections = {
88
Activity::Railway.Inject() => [:params], # pass-through {:params} if it's in ctx.
@@ -25,7 +25,7 @@ def call(ctx, params: {}, **)
2525
builder = Builder.new
2626
model = builder.call(ctx, params) or return
2727

28-
ctx[:model] = model
28+
ctx[:model] = model # DISCUSS: what about nil models?
2929
end
3030

3131
class Builder

lib/trailblazer/macro/model/find.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ def self.for_keywords(model_class, find_method:, **options, &block) # FIXME: def
8080
end
8181

8282
def self.for_query(model_class, query, column_key: :id, params_key: column_key, **, &block) # FIXME: defaulting is redundant with bla_explicit_positional.
83-
query_on_model_class = ->(ctx, **kws) { model_class.instance_exec(ctx, **kws, &query) } # FIXME: we can only use procs here. what about methods, classes etc?
83+
query_on_model_class = ->(ctx, **kws) { puts kws.inspect; model_class.instance_exec(ctx, **kws, &query) } # FIXME: we can only use procs here. what about methods, classes etc?
8484

8585
finder = Macro.task_adapter_for_decider(query_on_model_class, variable_name: :model) # FIXME: {:model} is hard-coded.
8686

lib/trailblazer/macro/policy.rb

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,18 @@ def initialize(name: nil, path: nil)
1212
# outgoing Task::Binary API.
1313
#
1414
# Retrieve the injectable {condition}, execute it and interpret its {Result} object.
15-
def call((ctx, flow_options), **circuit_options)
15+
def call(ctx, flow_options, circuit_options)
1616
condition = ctx[@path] # this allows dependency injection.
17-
result = condition.([ctx, flow_options], **circuit_options)
17+
result = condition.(ctx, flow_options, circuit_options)
18+
puts "@@@@@ #{result.inspect}"
1819

1920
ctx[:"policy.#{@name}"] = result[:policy] # assign the policy as a ctx variable.
2021
ctx[:"result.policy.#{@name}"] = result
2122

2223
# flow control
2324
signal = result.success? ? Trailblazer::Activity::Right : Trailblazer::Activity::Left
2425

25-
return signal, [ctx, flow_options]
26+
return ctx, flow_options, signal
2627
end
2728
end
2829

lib/trailblazer/macro/rescue.rb

Lines changed: 4 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,44 +2,24 @@ module Trailblazer
22
module Macro
33
NoopHandler = lambda { |*| }
44

5-
def self.Rescue(*exceptions, handler: NoopHandler, id: Rescue.random_id, &block)
5+
def self.Rescue(*exceptions, handler: NoopHandler, id: Macro.id_for(nil, macro: :Rescue), &block)
66
exceptions = [StandardError] unless exceptions.any?
77

8-
handler = Rescue.deprecate_positional_handler_signature(handler)
9-
handler = Trailblazer::Option(handler)
8+
handler = Trailblazer::Activity::Circuit.Step(handler)
109

1110
# This block is evaluated by {Wrap}.
1211
rescue_block = ->((ctx, flow_options), **circuit_options, &nested_activity) do
1312
begin
1413
nested_activity.call
1514
rescue *exceptions => exception
1615
# DISCUSS: should we deprecate this signature and rather apply the Task API here?
17-
handler.call(exception, ctx, **circuit_options) # FIXME: when there's an error here, it shows the wrong exception!
16+
handler.call(exception, ctx, flow_options, circuit_options) # FIXME: when there's an error here, it shows the wrong exception!
1817

19-
[Operation::Railway.fail!, [ctx, flow_options]]
18+
return ctx, flow_options, Operation::Railway.fail!
2019
end
2120
end
2221

2322
Wrap(rescue_block, id: id, &block)
2423
end
25-
26-
# TODO: remove me in 2.2.
27-
module Rescue
28-
def self.deprecate_positional_handler_signature(handler)
29-
return handler if handler.is_a?(Symbol) # can't do nothing about this.
30-
return handler if handler.method(:call).arity != 2 # means (exception, (ctx, flow_options), *, &block), "new style"
31-
32-
->(exception, (ctx, flow_options), **circuit_options, &block) do
33-
warn "[Trailblazer] Rescue handlers have a new signature: (exception, *, &block)"
34-
handler.(exception, ctx, &block)
35-
end
36-
end
37-
38-
# TODO: for legacy reasons, we pass `:id` to {#id_for}. In 2.2, remove the id hint and use
39-
# generic {Macro.id_for} behavior.
40-
def self.random_id
41-
Macro.id_for(nil, macro: :Rescue, id: rand(1000))
42-
end
43-
end
4424
end
4525
end

lib/trailblazer/macro/wrap.rb

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,14 @@ def self.Wrap(user_wrap, id: Macro.id_for(user_wrap, macro: :Wrap), &block)
4242

4343
# Wrap exposes {#inherited} which will also copy the block activity.
4444
# Currently, this is only used for patching (as it will try to subclass Wrap).
45-
class Wrap < Macro::Strategy # TODO: it would be cool to have Activity::Interface and Strategy::Interface
45+
class Wrap < Macro::Strategy
4646
# behaves like an operation so it plays with Nested and simply calls the operation in the user-provided block.
4747
# class Wrapped
4848
# @private
4949
def self.deprecate_positional_wrap_signature(user_wrap)
50+
return user_wrap
51+
# FIXME: deprecate old array-kwargs based circuit interface.
52+
5053
parameters = user_wrap.is_a?(Proc) || user_wrap.is_a?(Method) ? user_wrap.parameters : user_wrap.method(:call).parameters
5154

5255
return user_wrap if parameters[0] == [:req] # means ((ctx, flow_options), *, &block), "new style"
@@ -57,22 +60,23 @@ def self.deprecate_positional_wrap_signature(user_wrap)
5760
end
5861
end
5962

60-
def self.call((ctx, flow_options), **circuit_options)
63+
def self.call(ctx, flow_options, circuit_options)
6164
# since yield is called without arguments, we need to pull default params from here. Oh ... tricky.
62-
block_calling_wrapped = ->(args=[ctx, flow_options], kwargs=circuit_options) {
63-
Activity::Circuit::Runner.(block_activity, args, **kwargs)
65+
66+
block_called_from_user_yield = ->() { # DISCUSS: because we allow users to call {yield}, we don't receive any args here.
67+
Activity::Circuit::Runner.(block_activity, ctx, flow_options, circuit_options)
6468
}
6569

6670
# call the user's Wrap {} block in the operation.
67-
# This will invoke block_calling_wrapped above if the user block yields.
68-
returned = @state.get(:user_wrap).([ctx, flow_options], **circuit_options, &block_calling_wrapped)
71+
# This will invoke block_called_from_user_yield above if the user block yields.
72+
returned = @state.get(:user_wrap).(ctx, flow_options, circuit_options, &block_called_from_user_yield)
6973

7074
# {returned} can be
7175
# 1. {circuit interface return} from the begin block, because the wrapped OP passed
7276
# 2. {task interface return} because the user block returns "customized" signals, true of fale
7377

7478
if returned.is_a?(Array) # 1. {circuit interface return}, new style.
75-
signal, (ctx, flow_options) = returned
79+
ctx, flow_options, signal = returned
7680
else # 2. {task interface return}, only a signal (or true/false)
7781
# TODO: deprecate this?
7882
signal = returned
@@ -82,7 +86,7 @@ def self.call((ctx, flow_options), **circuit_options)
8286
# This usually means signal is a terminus or a custom signal.
8387
signal = @state.get(:signal_to_output).fetch(signal, signal)
8488

85-
return signal, [ctx, flow_options]
89+
return ctx, flow_options, signal
8690
end
8791
end # Wrap
8892
end

0 commit comments

Comments
 (0)