Skip to content

Commit 2365665

Browse files
committed
simplify tW rendering.
1 parent bc9429c commit 2365665

File tree

2 files changed

+61
-67
lines changed

2 files changed

+61
-67
lines changed

lib/trailblazer/developer/render/task_wrap.rb

Lines changed: 33 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@ module Trailblazer
22
module Developer
33
module Render
44
module TaskWrap
5+
module_function
6+
57
# @param activity Trailblazer::Activity
6-
def self.render_for(activity, node)
8+
def render_for(activity, node)
79
task_wrap = task_wrap_for_activity(activity) # TODO: MERGE WITH BELOW
810
task = node.task
911
step_wrap = task_wrap[task] # the taskWrap for the actual step, e.g. {input,call_task,output}.
@@ -17,64 +19,57 @@ def self.render_for(activity, node)
1719
end
1820

1921
# @param activity Activity
20-
def self.task_wrap_for_activity(activity, **)
22+
def task_wrap_for_activity(activity, **)
2123
activity.to_h[:config][:wrap_static]
2224
end
2325

24-
def self.render_pipeline(pipeline, level)
26+
def render_pipeline(pipeline, level)
2527
renderers = Hash.new(method(:render_task_wrap_step))
2628
renderers.merge!(
27-
Trailblazer::Activity::DSL::Linear::VariableMapping::Pipe::Input => method(:render_input),
28-
Trailblazer::Activity::DSL::Linear::VariableMapping::Pipe::Output => method(:render_input),
29+
# Trailblazer::Activity::DSL::Linear::VariableMapping::Pipe::Input => method(:render_input),
30+
# Trailblazer::Activity::DSL::Linear::VariableMapping::Pipe::Output => method(:render_input),
31+
Method => method(:render_method),
2932
)
3033
# TODO: use collect
3134
nodes=[]
3235

33-
pipeline.to_a.collect do |row|
34-
renderer = renderers[row[1].class]
36+
pipeline.to_a.collect do |id, row|
37+
renderer = renderers[row.class]
38+
39+
name, type = renderer.(id, row, level)
40+
41+
nodes = nodes + format_line(name, type, level)
42+
43+
if row.is_a?(Activity::Pipeline)
44+
nodes += render_pipeline(row, level + 1)
45+
end
3546

36-
nodes = nodes + renderer.(row, level) # call the rendering component.
3747
end
3848

3949
nodes
4050
end
4151

42-
def self.render_task_wrap_step(row, level)
43-
id, task = row
52+
def format_line(name, type, level)
53+
offset = level * 4
4454

45-
text = id.to_s.ljust(33, ".") + task.class.to_s
55+
text = name.to_s.ljust(66 - offset, ".") + type
4656

4757
[[level, text]]
4858
end
4959

50-
def self.render_input(row, level)
51-
variable_mapping = Activity::DSL::Linear::VariableMapping
52-
53-
input_pipe = row[1].instance_variable_get(:@pipe) # this is again a {TaskWrap::Pipeline}.
54-
pp input_pipe
55-
filters = input_pipe.to_a.collect do |id, filter|
56-
id, class_name, info =
57-
if filter.is_a?(Class) && filter < variable_mapping::Runtime::FilterStep::MergeVariables
58-
# # TODO: grab user_filter here if needed for understanding
59-
# # _info = filter.instance_variable_get(:@user_filter).inspect # we could even grab the source code for callables here!
60-
_info = filter.superclass.inspect
61-
# r
62-
[id, filter.class.to_s.match(/VariableMapping::.+/), _info]
63-
# [id, id, _info]
64-
else # generic VariableMapping::Runtime step such as {VariableMapping.scope}
65-
_name = filter.inspect.match(/VariableMapping::Runtime\.\w+/)
66-
67-
[id.to_s, _name, ""]
68-
end
69-
70-
text = "#{id.ljust(45, ".")} #{info.ljust(45, ".")} #{class_name}"
71-
72-
[level+1, text]
73-
end
60+
# Default renderer for tW step.
61+
def render_task_wrap_step(id, task, level)
62+
type = task.is_a?(Class) ? task.class.to_s : task.to_s
63+
64+
return id, type
65+
end
66+
67+
def render_method(id, method, level)
68+
name, _ = "#{method.to_s}".split(" /")
69+
70+
name = name.sub("Trailblazer::Activity::DSL::Linear", "") # DISCUSS: too specific.
7471

75-
# pp filters
76-
render_task_wrap_step(row, level) + filters
77-
# render_task_wrap_step(row, level)
72+
return id, name
7873
end
7974
end
8075
end

test/render_task_wrap_test.rb

Lines changed: 28 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -19,38 +19,37 @@ def assert_inspect(asserted, expected)
1919
#@ no special tW
2020
node, activity, _ = Trailblazer::Developer::Introspect.find_path(activity, [:a])
2121
pipe = Trailblazer::Developer::Render::TaskWrap.render_for(activity, node)
22-
assert_inspect pipe, %{#<Trailblazer::Activity:xxx>
22+
assert_inspect pipe, %(#<Trailblazer::Activity:xxx>
2323
`-- a
24-
`-- task_wrap.call_task..............Method}
24+
`-- task_wrap.call_task.......................................#<Method: Trailblazer::Activity::TaskWrap.call_task(wrap_ctx, flow_options, _))
2525

2626
#@ only In() set
2727
node, activity, _ = Trailblazer::Developer::Introspect.find_path(activity, [:b])
2828
pipe = Trailblazer::Developer::Render::TaskWrap.render_for(activity, node)
29-
assert_inspect pipe, %{#<Trailblazer::Activity:xxx>
29+
assert_inspect pipe, %(#<Trailblazer::Activity:xxx>
3030
`-- b
31-
|-- task_wrap.input..................Trailblazer::Activity::DSL::Linear::VariableMapping::Pipe::Input
32-
| |-- In {:model > :model}......................... .............................................
33-
| |-- In {:user > :current_user}................... .............................................
34-
| `-- input.scope.................................. ............................................. VariableMapping::Runtime.build_context
35-
|-- task_wrap.call_task..............Method
36-
`-- task_wrap.output.................Trailblazer::Activity::DSL::Linear::VariableMapping::Pipe::Output
37-
|-- output.default_output........................ ............................................. VariableMapping::Runtime.default_output_ctx
38-
`-- output.merge_with_original................... ............................................. VariableMapping::Runtime.merge_with_original}
31+
|-- task_wrap.input...........................................#<Trailblazer::Activity::DSL::Linear::VariableMapping::Pipe::Input:xxx>
32+
| |-- In {:model > :model}..................................Class
33+
| |-- In {:user > :current_user}............................Class
34+
| `-- input.scope...........................................#<Method: ::VariableMapping::Runtime.build_context(wrap_ctx, flow_options, _)
35+
|-- task_wrap.call_task.......................................#<Method: Trailblazer::Activity::TaskWrap.call_task(wrap_ctx, flow_options, _)
36+
`-- task_wrap.output..........................................#<Trailblazer::Activity::DSL::Linear::VariableMapping::Pipe::Output:xxx>
37+
|-- output.default_output.................................#<Method: ::VariableMapping::Runtime.default_output_ctx(wrap_ctx, flow_options, _)
38+
`-- output.merge_with_original............................#<Method: ::VariableMapping::Runtime.merge_with_original(wrap_ctx, flow_options, _))
3939

4040
#@ only with Inject()
4141
node, activity, _ = Trailblazer::Developer::Introspect.find_path(activity, [:c])
4242
pipe = Trailblazer::Developer::Render::TaskWrap.render_for(activity, node)
43-
assert_inspect pipe, %{#<Trailblazer::Activity:xxx>
43+
assert_inspect pipe, %(#<Trailblazer::Activity:xxx>
4444
`-- c
45-
|-- task_wrap.input..................Trailblazer::Activity::DSL::Linear::VariableMapping::Pipe::Input
46-
| |-- input.default_input.......................... ............................................. VariableMapping::Runtime.default_input_ctx
47-
| |-- input.add_variables.Inject{:current_user}.... ............................................. VariableMapping::SetVariable::Conditioned
48-
| `-- input.scope.................................. ............................................. VariableMapping::Runtime.scope
49-
|-- task_wrap.call_task..............Method
50-
`-- task_wrap.output.................Trailblazer::Activity::DSL::Linear::VariableMapping::Pipe::Output
51-
|-- output.add_variables.Out{:model}............. ............................................. VariableMapping::SetVariable::Output
52-
`-- output.merge_with_original................... ............................................. VariableMapping::Runtime.merge_with_original}
53-
45+
|-- task_wrap.input...........................................#<Trailblazer::Activity::DSL::Linear::VariableMapping::Pipe::Input:xxx>
46+
| |-- input.default_input...................................#<Method: ::VariableMapping::Runtime.default_input_ctx(pipe_ctx, flow_options, _)
47+
| |-- Inject {:current_user}................................Class
48+
| `-- input.scope...........................................#<Method: ::VariableMapping::Runtime.build_context(wrap_ctx, flow_options, _)
49+
|-- task_wrap.call_task.......................................#<Method: Trailblazer::Activity::TaskWrap.call_task(wrap_ctx, flow_options, _)
50+
`-- task_wrap.output..........................................#<Trailblazer::Activity::DSL::Linear::VariableMapping::Pipe::Output:xxx>
51+
|-- Out {:model > :model}.................................Class
52+
`-- output.merge_with_original............................#<Method: ::VariableMapping::Runtime.merge_with_original(wrap_ctx, flow_options, _))
5453
end
5554

5655
it "allows path to step/activity" do
@@ -65,15 +64,15 @@ def assert_inspect(asserted, expected)
6564

6665
node, activity, _ = Trailblazer::Developer::Introspect.find_path(activity, [:B, :b])
6766
pipe = Trailblazer::Developer::Render::TaskWrap.render_for(activity, node)
68-
assert_inspect pipe, %{#<Trailblazer::Activity:xxx>
67+
assert_inspect pipe, %(#<Trailblazer::Activity:xxx>
6968
`-- b
70-
|-- task_wrap.input..................Trailblazer::Activity::DSL::Linear::VariableMapping::Pipe::Input
71-
| |-- input.add_variables.In{:current_user}........ ............................................. VariableMapping::SetVariable
72-
| `-- input.scope.................................. ............................................. VariableMapping::Runtime.scope
73-
|-- task_wrap.call_task..............Method
74-
`-- task_wrap.output.................Trailblazer::Activity::DSL::Linear::VariableMapping::Pipe::Output
75-
|-- output.default_output........................ ............................................. VariableMapping::Runtime.default_output_ctx
76-
`-- output.merge_with_original................... ............................................. VariableMapping::Runtime.merge_with_original}
69+
|-- task_wrap.input...........................................#<Trailblazer::Activity::DSL::Linear::VariableMapping::Pipe::Input:xxx>
70+
| |-- In {:current_user > :current_user}....................Class
71+
| `-- input.scope...........................................#<Method: ::VariableMapping::Runtime.build_context(wrap_ctx, flow_options, _)
72+
|-- task_wrap.call_task.......................................#<Method: Trailblazer::Activity::TaskWrap.call_task(wrap_ctx, flow_options, _)
73+
`-- task_wrap.output..........................................#<Trailblazer::Activity::DSL::Linear::VariableMapping::Pipe::Output:xxx>
74+
|-- output.default_output.................................#<Method: ::VariableMapping::Runtime.default_output_ctx(wrap_ctx, flow_options, _)
75+
`-- output.merge_with_original............................#<Method: ::VariableMapping::Runtime.merge_with_original(wrap_ctx, flow_options, _))
7776
end
7877
end
7978

0 commit comments

Comments
 (0)