diff --git a/test/docs/mechanics_test.rb b/test/docs/mechanics_test.rb new file mode 100644 index 0000000..e07290d --- /dev/null +++ b/test/docs/mechanics_test.rb @@ -0,0 +1,112 @@ +require "test_helper" + +module Y + class DocsMechanicsTest < Minitest::Spec + Memo = Module.new + it "what" do + #:instance-method + module Memo::Operation + class Create < Trailblazer::Operation + step :validate + + #~meths + def validate(ctx, params:, **) + params.key?(:memo) ? true : false # return value matters! + end + #~meths end + end + end + #:instance-method end + + #:instance-method-call + result = Memo::Operation::Create.call(params: {memo: nil}) + #:instance-method-call end + assert_equal result.success?, true + + #:instance-method-implicit-call + result = Memo::Operation::Create.(params: {memo: nil}) + #:instance-method-implicit-call end + assert_equal result.success?, true + end + end +end + +class Classmethod_DocsMechanicsTest < Minitest::Spec + Memo = Module.new + it "what" do + #:class-method + module Memo::Operation + class Create < Trailblazer::Operation + #~meths + # Define {Memo::Operation::Create.validate} + def self.validate(ctx, params:, **) + params.key?(:memo) ? true : false # return value matters! + end + #~meths end + + step method(:validate) + end + end + #:class-method end + end +end + +class Module_Classmethod_DocsMechanicsTest < Minitest::Spec + Memo = Module.new + it "what" do + #:module-step + # Reusable steps in a module. + module Steps + def self.validate(ctx, params:, **) + params.key?(:memo) ? true : false # return value matters! + end + end + #:module-step end + + #:module-method + module Memo::Operation + class Create < Trailblazer::Operation + step Steps.method(:validate) + end + end + #:module-method end + end +end + +class Callable_DocsMechanicsTest < Minitest::Spec + Memo = Module.new + it "what" do + #:callable-step + module Validate + def self.call(ctx, params:, **) + valid?(params) ? true : false # return value matters! + end + + def valid?(params) + params.key?(:memo) + end + end + #:callable-step end + + #:callable-method + module Memo::Operation + class Create < Trailblazer::Operation + step Validate + end + end + #:callable-method end + end +end + +class Lambda_DocsMechanicsTest < Minitest::Spec + Memo = Module.new + it "what" do + #:lambda-step + module Memo::Operation + class Create < Trailblazer::Operation + step ->(ctx, params:, **) { p params.inspect } + end + end + #:lambda-step end + end +end