test/docs/nested_test.rb in trailblazer-macro-2.1.0.rc13 vs test/docs/nested_test.rb in trailblazer-macro-2.1.0.rc14

- old
+ new

@@ -14,26 +14,37 @@ step :d include T.def_steps(:d) end end + class Validate < Trailblazer::Operation + step :validate + # ... more steps ... + include T.def_steps(:validate) + end + it "Nested(Edit), without any options" do - edit = self.edit + module A - create = Class.new(Trailblazer::Operation) do - step :a - step Nested( edit ) - step :b + create = + #:nested + class Create < Trailblazer::Operation + step :create + step Nested(Validate) + step :save + #~meths + include T.def_steps(:create, :save) + #~meths end + end + #:nested end - include T.def_steps(:a, :b) - end - - # this will print a DEPRECATION warning. - # success - create.(seq: []).inspect(:seq).must_equal %{<Result:true [[:a, :c, :b]] >} - # failure in Nested - create.(seq: [], c: false).inspect(:seq).must_equal %{<Result:false [[:a, :c]] >} + # this will print a DEPRECATION warning. + # success + create.(seq: []).inspect(:seq).must_equal %{<Result:true [[:create, :validate, :save]] >} + # failure in Nested + create.(seq: [], validate: false).inspect(:seq).must_equal %{<Result:false [[:create, :validate]] >} + end end it "Nested(Edit), with Output rewiring" do edit = self.edit @@ -50,35 +61,75 @@ # failure in Nested create.(seq: [], c: false).inspect(:seq).must_equal %{<Result:true [[:a, :c, :b]] >} end it "Nested(:method)" do - create = Class.new(Trailblazer::Operation) do - step :a - step Nested(:compute_edit) - step :b + module B + create = + #:nested-dynamic + class Create < Trailblazer::Operation + step :create + step Nested(:compute_nested) + step :save - def compute_edit(ctx, what:, **) - what + def compute_nested(ctx, params:, **) + params.is_a?(Hash) ? Validate : JsonValidate + end + #~meths + include T.def_steps(:create, :save) + #~meths end end + #:nested-dynamic end - include T.def_steps(:a, :b) - end - + class JsonValidate < Validate + step :json + include T.def_steps(:json) + end # `edit` and `update` can be called from Nested() # edit/success - create.(seq: [], what: edit).inspect(:seq).must_equal %{<Result:true [[:a, :c, :b]] >} + create.(seq: [], params: {}).inspect(:seq).must_equal %{<Result:true [[:create, :validate, :save]] >} # update/success - create.(seq: [], what: update).inspect(:seq).must_equal %{<Result:true [[:a, :d, :b]] >} + create.(seq: [], params: nil).inspect(:seq).must_equal %{<Result:true [[:create, :validate, :json, :save]] >} # wiring of fail: # edit/failure - create.(seq: [], what: edit, c: false).inspect(:seq).must_equal %{<Result:false [[:a, :c]] >} + create.(seq: [], params: {}, validate: false).inspect(:seq).must_equal %{<Result:false [[:create, :validate]] >} # update/failure - create.(seq: [], what: update, d: false).inspect(:seq).must_equal %{<Result:false [[:a, :d]] >} + create.(seq: [], params: nil, json: false).inspect(:seq).must_equal %{<Result:false [[:create, :validate, :json]] >} + end + end + + it "Nested(:method), input: :my_input" do + module C + #:nested-dynamic + class Create < Trailblazer::Operation + step :create + step Nested(:compute_nested), input: ->(ctx, *) {{foo: :bar, seq: ctx[:seq]}} + step :save + + def compute_nested(ctx, params:, **) + params.is_a?(Hash) ? Validate : JsonValidate + end + + #~meths + include T.def_steps(:create, :save) + #~meths end + end + #:nested-dynamic end + + class JsonValidate < Validate + step :json + include T.def_steps(:json) + end + + # `edit` and `update` can be called from Nested() + end + + C::Create.(seq: [], params: {}).inspect(:seq).must_equal %{<Result:true [[:create, :validate, :save]] >} + C::Create.(seq: [], params: nil).inspect(:seq).must_equal %{<Result:true [[:create, :validate, :json, :save]] >} end let(:compute_edit) { ->(ctx, what:, **) { what } }