trailblazer-activity 0.6.0 → 0.6.1

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: 656825c80059310cf4a2d89a2214a20618bbd688c182285e8d3c24c8475b8233
4
- data.tar.gz: 4fb358255fe07a12fcb94172a05f466ce3ac0cb66d3b9502a3622d2a4392a71f
3
+ metadata.gz: 7dbf0f6f475965ca903f16b2a584bc82131c21d91ed9a8897bf78c2c2abe91fb
4
+ data.tar.gz: f78fb64dbd9ff29f50be67f569548826f38f50186b8b63c3d6f16b8138a9edad
5
5
  SHA512:
6
- metadata.gz: 74361dffc29b0108e198a3a2bee9637b6519b2ad56b4fb9172aceffc92bee1bcb5dc5e1417dab33d9ba48340749bd5f4306bd16a2c22a1f5eb66cfea4991f04b
7
- data.tar.gz: ad2fd6a2d6d4df1a2c8e167fd395ef05899f7389b95f8219239d4533c75632001992ff38eb240b300d1b2260544b85abd276372783f8a4c2480afc2a3e45a1a5
6
+ metadata.gz: c32b52c355f304c8dc0bc4e167e89ce0915a342ed985027879b42f9ffba327f49d74e14dc3b740d7f263276b78940f17557560a03b57ac8bf85b54a2fc7407aa
7
+ data.tar.gz: 5917d86ee194c90052e5568593090a5de90684f3438d736c766ddae5b055bbf9a00191286a27bf16bae159a95068caa6994bdf17d7a1b8cfe10b1c5694d81891
data/CHANGES.md CHANGED
@@ -1,3 +1,7 @@
1
+ # 0.6.1
2
+
3
+ * Make `:input` and `:output` standard options of the DSL to create variable mappings.
4
+
1
5
  # 0.6.0
2
6
 
3
7
  * The `:task` option in `Circuit::call` is now named `:start_task` for consistency.
@@ -100,6 +100,9 @@ module Trailblazer
100
100
 
101
101
  task Activity::TaskBuilder::Binary( method(:initialize_extension_option) ), id: "initialize_extension_option"
102
102
  task Activity::TaskBuilder::Binary( method(:normalize_for_macro) ), id: "normalize_for_macro"
103
+
104
+ task Activity::TaskBuilder::Binary( Activity::TaskWrap::VariableMapping.method(:normalizer_step_for_input_output) )
105
+
103
106
  task Activity::TaskBuilder::Binary( method(:split_options) ), id: "split_options"
104
107
  task Activity::TaskBuilder::Binary( method(:initialize_plus_poles) ), id: "initialize_plus_poles"
105
108
  # task ->((ctx, _), **) { pp ctx; [Activity::Right, [ctx, _]] }
@@ -0,0 +1,157 @@
1
+ require "trailblazer/context"
2
+
3
+ class Trailblazer::Activity < Module
4
+ module TaskWrap
5
+ # Creates taskWrap steps to map variables before and after the actual step.
6
+ # We hook into the Normalizer, process `:input` and `:output` directives and
7
+ # translate them into a {DSL::Extension}.
8
+ #
9
+ # Note that the two options are not the only way to create filters, you can use the
10
+ # more low-level {Scoped()} etc., too, and write your own filter logic.
11
+ module VariableMapping
12
+ # DSL step for Magnetic::Normalizer.
13
+ # Translates `:input` and `:output` into VariableMapping taskWrap extensions.
14
+ def self.normalizer_step_for_input_output(ctx, *)
15
+ options, io_config = Magnetic::Options.normalize( ctx[:options], [:input, :output] )
16
+
17
+ return if io_config.empty?
18
+
19
+ ctx[:options] = options # without :input and :output
20
+ ctx[:options] = options.merge(Trailblazer::Activity::TaskWrap::VariableMapping(io_config) => true)
21
+ end
22
+ end
23
+
24
+ # @private
25
+ def self.filter_for(filter)
26
+ if filter.is_a?(Proc)
27
+ filter
28
+ elsif filter.is_a?(Symbol)
29
+ filter # TODO: test this properly?
30
+ else
31
+ TaskWrap::DSL.filter_from_dsl(filter)
32
+ end
33
+ end
34
+
35
+ # Returns an Extension instance to be thrown into the `step` DSL arguments.
36
+ def self.VariableMapping(input:, output:)
37
+ input = Input.new(
38
+ Input::Scoped.new(
39
+ Trailblazer::Option::KW( filter_for(input) ) ) )
40
+
41
+ output = Output.new(
42
+ Output::Unscoped.new(
43
+ Trailblazer::Option::KW( filter_for(output) ) ) )
44
+
45
+ Trailblazer::Activity::DSL::Extension.new(
46
+ Merge.new(
47
+ Module.new do
48
+ extend Path::Plan()
49
+
50
+ task input, id: "task_wrap.input", before: "task_wrap.call_task"
51
+ task output, id: "task_wrap.output", before: "End.success", group: :end
52
+ end
53
+ )
54
+ )
55
+ end
56
+
57
+ # TaskWrap step to compute the incoming {Context} for the wrapped task.
58
+ # This allows renaming, filtering, hiding, of the options passed into the wrapped task.
59
+ #
60
+ # Both Input and Output are typically to be added before and after task_wrap.call_task.
61
+ #
62
+ # @note Assumption: we always have :input _and_ :output, where :input produces a Context and :output decomposes it.
63
+ class Input
64
+ def initialize(filter)
65
+ @filter = filter
66
+ end
67
+
68
+ # `original_args` are the actual args passed to the wrapped task: [ [options, ..], circuit_options ]
69
+ #
70
+ def call( (wrap_ctx, original_args), circuit_options )
71
+ # let user compute new ctx for the wrapped task.
72
+ input_ctx = apply_filter(*original_args) # FIXME: THIS SHOULD ALWAYS BE A _NEW_ Context.
73
+
74
+ wrap_ctx = wrap_ctx.merge( vm_original_ctx: original_args[0][0] ) # remember the original ctx
75
+
76
+ # decompose the original_args since we want to modify them.
77
+ (original_ctx, original_flow_options), original_circuit_options = original_args
78
+
79
+ # instead of the original Context, pass on the filtered `input_ctx` in the wrap.
80
+ return Trailblazer::Activity::Right, [ wrap_ctx, [[input_ctx, original_flow_options], original_circuit_options] ]
81
+ end
82
+
83
+ private
84
+
85
+ def apply_filter((ctx, original_flow_options), original_circuit_options)
86
+ @filter.( ctx, original_circuit_options ) # returns {new_ctx}.
87
+ end
88
+
89
+ class Scoped
90
+ def initialize(filter)
91
+ @filter = filter
92
+ end
93
+
94
+ def call(original_ctx, circuit_options)
95
+ Trailblazer::Context( # TODO: make this interchangeable so we can work on faster contexts?
96
+ @filter.(original_ctx, **circuit_options)
97
+ )
98
+ end
99
+ end
100
+ end
101
+
102
+ module DSL
103
+ # The returned filter compiles a new hash for Scoped/Unscoped that only contains
104
+ # the desired i/o variables.
105
+ def self.filter_from_dsl(map)
106
+ hsh = DSL.hash_for(map)
107
+
108
+ ->(incoming_ctx, kwargs) { Hash[hsh.collect { |from_name, to_name| [to_name, incoming_ctx[from_name]] }] }
109
+ end
110
+
111
+ def self.hash_for(ary)
112
+ return ary if ary.instance_of?(::Hash)
113
+ Hash[ary.collect { |name| [name, name] }]
114
+ end
115
+ end
116
+
117
+ # TaskWrap step to compute the outgoing {Context} from the wrapped task.
118
+ # This allows renaming, filtering, hiding, of the options returned from the wrapped task.
119
+ class Output
120
+ def initialize(filter)
121
+ @filter = filter
122
+ end
123
+
124
+ # Runs the user filter and replaces the ctx in `wrap_ctx[:return_args]` with the filtered one.
125
+ def call( (wrap_ctx, original_args), **circuit_options )
126
+ (original_ctx, original_flow_options), original_circuit_options = original_args
127
+
128
+ returned_ctx, _ = wrap_ctx[:return_args] # this is the Context returned from `call`ing the task.
129
+ original_ctx = wrap_ctx[:vm_original_ctx]
130
+
131
+ # let user compute the output.
132
+ output_ctx = @filter.(original_ctx, returned_ctx, **original_circuit_options)
133
+
134
+ wrap_ctx = wrap_ctx.merge( return_args: [output_ctx, original_flow_options] )
135
+
136
+ # and then pass on the "new" context.
137
+ return Trailblazer::Activity::Right, [ wrap_ctx, original_args ]
138
+ end
139
+
140
+ private
141
+
142
+ # Merge the resulting {@filter.()} hash back into the original ctx.
143
+ # DISCUSS: do we need the original_ctx as a filter argument?
144
+ class Unscoped
145
+ def initialize(filter)
146
+ @filter = filter
147
+ end
148
+
149
+ def call(original_ctx, new_ctx, **circuit_options)
150
+ original_ctx.merge(
151
+ @filter.(new_ctx, **circuit_options)
152
+ )
153
+ end
154
+ end
155
+ end
156
+ end # Wrap
157
+ end
@@ -1,5 +1,5 @@
1
1
  module Trailblazer
2
2
  class Activity < Module
3
- VERSION = "0.6.0"
3
+ VERSION = "0.6.1"
4
4
  end
5
5
  end
@@ -105,6 +105,7 @@ require "trailblazer/activity/task_wrap/call_task"
105
105
  require "trailblazer/activity/task_wrap/trace"
106
106
  require "trailblazer/activity/task_wrap/runner"
107
107
  require "trailblazer/activity/task_wrap/merge"
108
+ require "trailblazer/activity/task_wrap/variable_mapping"
108
109
 
109
110
  require "trailblazer/activity/trace"
110
111
  require "trailblazer/activity/present"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: trailblazer-activity
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.0
4
+ version: 0.6.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nick Sutterer
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-05-03 00:00:00.000000000 Z
11
+ date: 2018-05-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: hirb
@@ -149,6 +149,7 @@ files:
149
149
  - lib/trailblazer/activity/task_wrap/merge.rb
150
150
  - lib/trailblazer/activity/task_wrap/runner.rb
151
151
  - lib/trailblazer/activity/task_wrap/trace.rb
152
+ - lib/trailblazer/activity/task_wrap/variable_mapping.rb
152
153
  - lib/trailblazer/activity/testing.rb
153
154
  - lib/trailblazer/activity/trace.rb
154
155
  - lib/trailblazer/activity/version.rb