trailblazer-macro 2.1.0.rc1 → 2.1.0.rc11

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,5 +1,4 @@
1
1
  require "test_helper"
2
- require "trailblazer/operation/policy"
3
2
 
4
3
  class PolicyTest < Minitest::Spec
5
4
  Song = Struct.new(:id) do
@@ -1,8 +1,9 @@
1
- require "pp"
2
1
  require "delegate" # Ruby 2.2
3
- require "trailblazer/macro"
4
2
  require "minitest/autorun"
5
3
 
4
+ require "trailblazer/macro"
5
+
6
+
6
7
  module Mock
7
8
  class Result
8
9
  def initialize(bool); @bool = bool end
@@ -22,7 +23,7 @@ end
22
23
 
23
24
  require "pp"
24
25
 
25
- Minitest::Spec::Operation = Trailblazer::Operation
26
+ # Minitest::Spec::Operation = Trailblazer::Operation
26
27
 
27
28
  Memo = Struct.new(:id, :body) do
28
29
  def self.find(id)
@@ -31,4 +32,13 @@ Memo = Struct.new(:id, :body) do
31
32
  end
32
33
  end
33
34
 
34
- require_relative "lib/methods"
35
+ require "trailblazer/activity/testing"
36
+ T = Trailblazer::Activity::Testing
37
+
38
+ module Rehash
39
+ def rehash(ctx, seq:, rehash_raise: false, **)
40
+ seq << :rehash
41
+ raise if rehash_raise
42
+ true
43
+ end
44
+ end
@@ -4,7 +4,7 @@ require 'trailblazer/macro/version'
4
4
 
5
5
  Gem::Specification.new do |spec|
6
6
  spec.name = "trailblazer-macro"
7
- spec.version = Trailblazer::Macro::VERSION
7
+ spec.version = Trailblazer::Version::Macro::VERSION
8
8
  spec.authors = ["Nick Sutterer", "Marc Tich"]
9
9
  spec.email = ["apotonick@gmail.com", "marc@mudsu.com"]
10
10
  spec.description = "Macros for Trailblazer's operation"
@@ -19,13 +19,16 @@ Gem::Specification.new do |spec|
19
19
 
20
20
  spec.add_development_dependency "bundler"
21
21
 
22
- spec.add_development_dependency "trailblazer-operation", ">= 0.4.1", "< 0.5.0"
22
+ # spec.add_development_dependency "trailblazer-operation", ">= 0.4.1", "< 0.5.0"
23
23
 
24
24
  spec.add_development_dependency "minitest"
25
25
  spec.add_development_dependency "rake"
26
26
 
27
- spec.add_development_dependency "roar"
28
27
  spec.add_development_dependency "multi_json"
28
+ spec.add_development_dependency "roar"
29
+
30
+ spec.add_dependency "trailblazer-activity-dsl-linear", ">= 0.1.5", "< 0.2.0" # TODO: this dependency will be removed.
31
+ spec.add_dependency "trailblazer-operation", ">= 0.5.0" # TODO: this dependency will be removed.
29
32
 
30
33
  spec.required_ruby_version = ">= 2.0.0"
31
34
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: trailblazer-macro
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.1.0.rc1
4
+ version: 2.1.0.rc11
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nick Sutterer
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2018-06-18 00:00:00.000000000 Z
12
+ date: 2019-06-16 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler
@@ -26,27 +26,21 @@ dependencies:
26
26
  - !ruby/object:Gem::Version
27
27
  version: '0'
28
28
  - !ruby/object:Gem::Dependency
29
- name: trailblazer-operation
29
+ name: minitest
30
30
  requirement: !ruby/object:Gem::Requirement
31
31
  requirements:
32
32
  - - ">="
33
33
  - !ruby/object:Gem::Version
34
- version: 0.4.1
35
- - - "<"
36
- - !ruby/object:Gem::Version
37
- version: 0.5.0
34
+ version: '0'
38
35
  type: :development
39
36
  prerelease: false
40
37
  version_requirements: !ruby/object:Gem::Requirement
41
38
  requirements:
42
39
  - - ">="
43
40
  - !ruby/object:Gem::Version
44
- version: 0.4.1
45
- - - "<"
46
- - !ruby/object:Gem::Version
47
- version: 0.5.0
41
+ version: '0'
48
42
  - !ruby/object:Gem::Dependency
49
- name: minitest
43
+ name: rake
50
44
  requirement: !ruby/object:Gem::Requirement
51
45
  requirements:
52
46
  - - ">="
@@ -60,7 +54,7 @@ dependencies:
60
54
  - !ruby/object:Gem::Version
61
55
  version: '0'
62
56
  - !ruby/object:Gem::Dependency
63
- name: rake
57
+ name: multi_json
64
58
  requirement: !ruby/object:Gem::Requirement
65
59
  requirements:
66
60
  - - ">="
@@ -88,19 +82,39 @@ dependencies:
88
82
  - !ruby/object:Gem::Version
89
83
  version: '0'
90
84
  - !ruby/object:Gem::Dependency
91
- name: multi_json
85
+ name: trailblazer-activity-dsl-linear
92
86
  requirement: !ruby/object:Gem::Requirement
93
87
  requirements:
94
88
  - - ">="
95
89
  - !ruby/object:Gem::Version
96
- version: '0'
97
- type: :development
90
+ version: 0.1.5
91
+ - - "<"
92
+ - !ruby/object:Gem::Version
93
+ version: 0.2.0
94
+ type: :runtime
98
95
  prerelease: false
99
96
  version_requirements: !ruby/object:Gem::Requirement
100
97
  requirements:
101
98
  - - ">="
102
99
  - !ruby/object:Gem::Version
103
- version: '0'
100
+ version: 0.1.5
101
+ - - "<"
102
+ - !ruby/object:Gem::Version
103
+ version: 0.2.0
104
+ - !ruby/object:Gem::Dependency
105
+ name: trailblazer-operation
106
+ requirement: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: 0.5.0
111
+ type: :runtime
112
+ prerelease: false
113
+ version_requirements: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: 0.5.0
104
118
  description: Macros for Trailblazer's operation
105
119
  email:
106
120
  - apotonick@gmail.com
@@ -120,14 +134,14 @@ files:
120
134
  - Rakefile
121
135
  - lib/trailblazer-macro.rb
122
136
  - lib/trailblazer/macro.rb
137
+ - lib/trailblazer/macro/guard.rb
138
+ - lib/trailblazer/macro/model.rb
139
+ - lib/trailblazer/macro/nested.rb
140
+ - lib/trailblazer/macro/policy.rb
141
+ - lib/trailblazer/macro/pundit.rb
142
+ - lib/trailblazer/macro/rescue.rb
123
143
  - lib/trailblazer/macro/version.rb
124
- - lib/trailblazer/operation/guard.rb
125
- - lib/trailblazer/operation/model.rb
126
- - lib/trailblazer/operation/nested.rb
127
- - lib/trailblazer/operation/policy.rb
128
- - lib/trailblazer/operation/pundit.rb
129
- - lib/trailblazer/operation/rescue.rb
130
- - lib/trailblazer/operation/wrap.rb
144
+ - lib/trailblazer/macro/wrap.rb
131
145
  - test/docs/guard_test.rb
132
146
  - test/docs/macro_test.rb
133
147
  - test/docs/model_test.rb
@@ -135,9 +149,8 @@ files:
135
149
  - test/docs/pundit_test.rb
136
150
  - test/docs/rescue_test.rb
137
151
  - test/docs/wrap_test.rb
138
- - test/lib/methods.rb
152
+ - test/operation/integration_test.rb
139
153
  - test/operation/model_test.rb
140
- - test/operation/nested_test.rb
141
154
  - test/operation/pundit_test.rb
142
155
  - test/test_helper.rb
143
156
  - trailblazer-macro.gemspec
@@ -173,8 +186,7 @@ test_files:
173
186
  - test/docs/pundit_test.rb
174
187
  - test/docs/rescue_test.rb
175
188
  - test/docs/wrap_test.rb
176
- - test/lib/methods.rb
189
+ - test/operation/integration_test.rb
177
190
  - test/operation/model_test.rb
178
- - test/operation/nested_test.rb
179
191
  - test/operation/pundit_test.rb
180
192
  - test/test_helper.rb
@@ -1,98 +0,0 @@
1
- # per default, everything we pass into a circuit is immutable. it's the ops/act's job to allow writing (via a Context)
2
- module Trailblazer
3
- class Operation
4
- # {Nested} macro.
5
- def self.Nested(callable, id: "Nested(#{callable})", input: nil, output: nil)
6
- task_wrap_extensions = Module.new do
7
- extend Activity::Path::Plan()
8
- end
9
-
10
- input_output = Nested.input_output_extensions_for(input, output) # TODO: deprecate this?
11
-
12
- task, operation, is_dynamic = Nested.build(callable)
13
-
14
- if is_dynamic
15
- task_wrap_extensions.task task.method(:compute_nested_activity), id: ".compute_nested_activity", after: "Start.default", group: :start
16
- task_wrap_extensions.task task.method(:compute_return_signal), id: ".compute_return_signal", after: "task_wrap.call_task"
17
- end
18
-
19
- options = {
20
- task: task,
21
- id: id,
22
- Trailblazer::Activity::DSL::Extension.new(Trailblazer::Activity::TaskWrap::Merge.new(task_wrap_extensions)) => true,
23
- outputs: operation.outputs,
24
- }.merge(input_output)
25
- end
26
-
27
- # @private
28
- module Nested
29
- def self.input_output_extensions_for(input, output)
30
- return {} unless input || output
31
-
32
- input = input || ->(original_ctx, **) { original_ctx }
33
- output = output || ->(new_ctx, **) { new_ctx.decompose.last } # merges "mutable" part into original, since it's in Unscoped.
34
-
35
- {
36
- Activity::TaskWrap::VariableMapping(input: input, output: output) => true
37
- }
38
- end
39
-
40
- # DISCUSS: use builders here?
41
- def self.build(nested_operation)
42
- return dynamic = Dynamic.new(nested_operation), dynamic, true unless nestable_object?(nested_operation)
43
-
44
- # The returned {Nested} instance is a valid circuit element and will be `call`ed in the circuit.
45
- # It simply returns the nested activity's `signal,options,flow_options` return set.
46
- # The actual wiring - where to go with that - is done by the step DSL.
47
- return nested_operation, nested_operation, false
48
- end
49
-
50
- def self.nestable_object?(object)
51
- object.is_a?( Trailblazer::Activity::Interface )
52
- end
53
-
54
- def self.operation_class
55
- Operation
56
- end
57
-
58
- # For dynamic `Nested`s that do not expose an {Activity} interface.
59
- # Since we do not know its outputs, we have to map them to :success and :failure, only.
60
- #
61
- # This is what {Nested} in 2.0 used to do, where the outcome could only be true/false (or success/failure).
62
- class Dynamic
63
- def initialize(nested_activity)
64
- @nested_activity = Trailblazer::Option::KW(nested_activity)
65
- @outputs = {
66
- :success => Activity::Output( Railway::End::Success.new(semantic: :success), :success ),
67
- :failure => Activity::Output( Railway::End::Failure.new(semantic: :failure), :failure ),
68
- }
69
- end
70
-
71
- attr_reader :outputs
72
-
73
- # TaskWrap step.
74
- def compute_nested_activity((wrap_ctx, original_args), **circuit_options)
75
- (ctx,), original_circuit_options = original_args
76
-
77
- # TODO: evaluate the option to get the actual "object" to call.
78
- activity = @nested_activity.call(ctx, original_circuit_options)
79
-
80
- # Overwrite :task so task_wrap.call_task will call this activity.
81
- # This is a trick so we don't have to repeat logic from #call_task here.
82
- wrap_ctx[:task] = activity
83
-
84
- return Activity::Right, [wrap_ctx, original_args]
85
- end
86
-
87
- def compute_return_signal((wrap_ctx, original_args), **circuit_options)
88
- # Translate the genuine nested signal to the generic Dynamic end (success/failure, only).
89
- # Note that here we lose information about what specific event was emitted.
90
- wrap_ctx[:return_signal] = wrap_ctx[:return_signal].kind_of?(Railway::End::Success) ?
91
- @outputs[:success].signal : @outputs[:failure].signal
92
-
93
- return Activity::Right, [wrap_ctx, original_args]
94
- end
95
- end
96
- end
97
- end
98
- end
@@ -1,42 +0,0 @@
1
- class Trailblazer::Operation
2
- NoopHandler = lambda { |*| }
3
-
4
- def self.Rescue(*exceptions, handler: NoopHandler, &block)
5
- exceptions = [StandardError] unless exceptions.any?
6
-
7
- handler = Rescue.deprecate_positional_handler_signature(handler)
8
- handler = Trailblazer::Option(handler)
9
-
10
- # This block is evaluated by {Wrap}.
11
- rescue_block = ->((ctx, flow_options), **circuit_options, &nested_activity) do
12
- begin
13
- nested_activity.call
14
- rescue *exceptions => exception
15
- # DISCUSS: should we deprecate this signature and rather apply the Task API here?
16
- handler.call(exception, ctx, **circuit_options) # FIXME: when there's an error here, it shows the wrong exception!
17
-
18
- [ Trailblazer::Operation::Railway.fail!, [ctx, flow_options] ]
19
- end
20
- end
21
-
22
- Wrap( rescue_block, id: "Rescue(#{rand(100)})", &block )
23
- # FIXME: name
24
- # [ step, name: "Rescue:#{block.source_location.last}" ]
25
- end
26
-
27
- # TODO: remove me in 2.2.
28
- module Rescue
29
- def self.deprecate_positional_handler_signature(handler)
30
- return handler if handler.is_a?(Symbol) # can't do nutting about this.
31
-
32
- arity = handler.is_a?(Class) ? handler.method(:call).arity : handler.arity
33
-
34
- return handler if arity != 2 # means (exception, (ctx, flow_options), *, &block), "new style"
35
-
36
- ->(exception, (ctx, flow_options), **circuit_options, &block) do
37
- warn "[Trailblazer] Rescue handlers have a new signature: (exception, *, &block)"
38
- handler.(exception, ctx, &block)
39
- end
40
- end
41
- end
42
- end
@@ -1,83 +0,0 @@
1
- class Trailblazer::Operation
2
- def self.Wrap(user_wrap, id: "Wrap/#{rand(100)}", &block)
3
- operation_class = Wrap.create_operation(block)
4
- wrapped = Wrap::Wrapped.new(operation_class, user_wrap)
5
-
6
- { task: wrapped, id: id, outputs: operation_class.outputs }
7
- end
8
-
9
- module Wrap
10
- def self.create_operation(block)
11
- Class.new( Nested.operation_class, &block ) # Usually resolves to Trailblazer::Operation.
12
- end
13
-
14
- # behaves like an operation so it plays with Nested and simply calls the operation in the user-provided block.
15
- class Wrapped #< Trailblazer::Operation # FIXME: the inheritance is only to satisfy Nested( Wrapped.new )
16
- include Trailblazer::Activity::Interface
17
-
18
- private def deprecate_positional_wrap_signature(user_wrap)
19
- parameters = user_wrap.is_a?(Module) ? user_wrap.method(:call).parameters : user_wrap.parameters
20
-
21
- return user_wrap if parameters[0] == [:req] # means ((ctx, flow_options), *, &block), "new style"
22
-
23
- ->((ctx, flow_options), **circuit_options, &block) do
24
- warn "[Trailblazer] Wrap handlers have a new signature: ((ctx), *, &block)"
25
- user_wrap.(ctx, &block)
26
- end
27
- end
28
-
29
- def initialize(operation, user_wrap)
30
- user_wrap = deprecate_positional_wrap_signature(user_wrap)
31
-
32
- @operation = operation
33
- @user_wrap = user_wrap
34
-
35
- # Since in the user block, you can return Railway.pass! etc, we need to map
36
- # those to the actual wrapped operation's end.
37
- outputs = @operation.outputs
38
- @signal_to_output = {
39
- Railway.pass! => outputs[:success].signal,
40
- Railway.fail! => outputs[:failure].signal,
41
- Railway.pass_fast! => outputs[:pass_fast].signal,
42
- Railway.fail_fast! => outputs[:fail_fast].signal,
43
- true => outputs[:success].signal,
44
- false => outputs[:failure].signal,
45
- nil => outputs[:failure].signal,
46
- }
47
- end
48
-
49
- def call( (ctx, flow_options), **circuit_options )
50
- block_calling_wrapped = -> {
51
- activity = @operation.to_h[:activity]
52
-
53
- activity.( [ctx, flow_options], **circuit_options )
54
- }
55
-
56
- # call the user's Wrap {} block in the operation.
57
- # This will invoke block_calling_wrapped above if the user block yields.
58
- returned = @user_wrap.( [ctx, flow_options], **circuit_options, &block_calling_wrapped )
59
-
60
- # {returned} can be
61
- # 1. {circuit interface return} from the begin block, because the wrapped OP passed
62
- # 2. {task interface return} because the user block returns "customized" signals, true of fale
63
-
64
- if returned.is_a?(Array) # 1. {circuit interface return}, new style.
65
- signal, (ctx, flow_options) = returned
66
- else # 2. {task interface return}, only a signal (or true/false)
67
- # TODO: deprecate this?
68
- signal = returned
69
- end
70
-
71
- # Use the original {signal} if there's no mapping.
72
- # This usually means signal is an End instance or a custom signal.
73
- signal = @signal_to_output.fetch(signal, signal)
74
-
75
- return signal, [ctx, flow_options]
76
- end
77
-
78
- def outputs
79
- @operation.outputs
80
- end
81
- end
82
- end # Wrap
83
- end
@@ -1,25 +0,0 @@
1
- module Test
2
- module Methods
3
- def find_model(ctx, seq:, **)
4
- seq << :find_model
5
- end
6
-
7
- def update(ctx, seq:, **)
8
- seq << :update
9
- end
10
-
11
- def notify(ctx, seq:, **)
12
- seq << :notify
13
- end
14
-
15
- def rehash(ctx, seq:, rehash_raise:false, **)
16
- seq << :rehash
17
- raise if rehash_raise
18
- true
19
- end
20
-
21
- def log_error(ctx, seq:, **)
22
- seq << :log_error
23
- end
24
- end
25
- end