test/docs/operation_test.rb in trailblazer-2.0.7 vs test/docs/operation_test.rb in trailblazer-2.1.0.beta1
- old
+ new
@@ -9,59 +9,57 @@
#:invocation-dep
class Create < Trailblazer::Operation
step Model( Song, :new )
step :assign_current_user!
# ..
- def assign_current_user!(options)
- options["model"].created_by = options["current_user"]
+ def assign_current_user!(options, **)
+ options[:model].created_by = options[:current_user]
end
end
#:invocation-dep end
it do
current_user = User.new("Ema")
#:invocation-dep-call
- result = Create.( { title: "Roxanne" }, "current_user" => current_user )
+ result = Create.( params: { title: "Roxanne" }, :current_user => current_user )
#:invocation-dep-call end
#:invocation-dep-res
- result["current_user"] #=> #<User name="Ema">
- result["model"] #=> #<Song id=nil, title=nil, created_by=#<User name="Ema">>
+ result[:current_user] #=> #<User name="Ema">
+ result[:model] #=> #<Song id=nil, title=nil, created_by=#<User name="Ema">>
#:invocation-dep-res end
end
- it { Create.({ title: "Roxanne" }, "current_user" => Module).inspect("model").must_equal %{<Result:true [#<struct DocsOperationExampleTest::Song id=nil, title=nil, created_by=Module>] >} }
+ it { Create.(params: { title: "Roxanne" }, :current_user => Module).inspect(:model).must_equal %{<Result:true [#<struct DocsOperationExampleTest::Song id=nil, title=nil, created_by=Module>] >} }
#:op
class Song::Create < Trailblazer::Operation
- extend Contract::DSL
-
- contract do
+ class Form < Reform::Form
property :title
validates :title, presence: true
end
step Model( Song, :new )
step :assign_current_user!
- step Contract::Build()
+ step Contract::Build( constant: Form )
step Contract::Validate( )
failure :log_error!
step Contract::Persist( )
- def log_error!(options)
+ def log_error!(options, **)
# ..
end
- def assign_current_user!(options)
- options["model"].created_by =
- options["current_user"]
+ def assign_current_user!(options, **)
+ options[:model].created_by =
+ options[:current_user]
end
end
#:op end
- it { Song::Create.({ }).inspect("model").must_equal %{<Result:false [#<struct DocsOperationExampleTest::Song id=nil, title=nil, created_by=nil>] >} }
- it { Song::Create.({ title: "Nothin'" }, "current_user"=>Module).inspect("model").must_equal %{<Result:true [#<struct DocsOperationExampleTest::Song id=nil, title="Nothin'", created_by=Module>] >} }
+ it { Song::Create.(params: { }).inspect(:model).must_equal %{<Result:false [#<struct DocsOperationExampleTest::Song id=nil, title=nil, created_by=nil>] >} }
+ it { Song::Create.(params: { title: "Nothin'" }, :current_user=>Module).inspect(:model).must_equal %{<Result:true [#<struct DocsOperationExampleTest::Song id=nil, title="Nothin'", created_by=Module>] >} }
end
class DndTest < Minitest::Spec
class Create < Trailblazer::Operation
step :authorize!
@@ -81,12 +79,12 @@
step :model!
step :assign!
step :validate!
def model!(options, current_user:, **)
- options["model"] = Song.new
- options["model"].created_by = current_user
+ options[:model] = Song.new
+ options[:model].created_by = current_user
end
def assign!(*, params:, model:, **)
model.title= params[:title]
end
@@ -100,29 +98,29 @@
#:step-options end
it do
current_user = Struct.new(:email).new("nick@trailblazer.to")
#:step-res
- result = Song::Create.({ title: "Roxanne" }, "current_user" => current_user)
+ result = Song::Create.(params: { title: "Roxanne" }, :current_user => current_user)
- result["model"] #=> #<Song title="Roxanne", "created_by"=<User ...>
+ result[:model] #=> #<Song title="Roxanne", "created_by"=<User ...>
result["result.validate"] #=> true
#:step-res end
- result.inspect("current_user", "model").must_equal %{<Result:true [#<struct email=\"nick@trailblazer.to\">, #<struct DocsResultTest::Song id=nil, title="Roxanne", created_by=#<struct email=\"nick@trailblazer.to\">>] >}
+ result.inspect(:current_user, :model).must_equal %{<Result:true [#<struct email=\"nick@trailblazer.to\">, #<struct DocsResultTest::Song id=nil, title="Roxanne", created_by=#<struct email=\"nick@trailblazer.to\">>] >}
#:step-binary
result.success? #=> true
result.failure? #=> falsee
#:step-binary end
#:step-dep
- result["current_user"] #=> <User ...>
+ result[:current_user] #=> <User ...>
#:step-dep end
#:step-inspect
- result.inspect("current_user", "model") #=> "<Result:true [#<User email=\"nick@tra... "
+ result.inspect(:current_user, :model) #=> "<Result:true [#<User email=\"nick@tra... "
#:step-inspect end
end
end
class DocsDependencyTest < Minitest::Spec
@@ -131,10 +129,11 @@
end
Hit = Struct.new(:id)
#:dep-op
class Song::Create < Trailblazer::Operation
+ extend ClassDependencies
self["my.model.class"] = Song
#~dep-pipe
step :model!
@@ -192,23 +191,23 @@
step Contract::Build( constant: MyContract )
step Contract::Validate()
failure :log_error!
step Contract::Persist()
- def log_error!(options)
+ def log_error!(options, **)
# ..
end
- def assign_current_user!(options)
- options["model"].created_by =
- options["current_user"]
+ def assign_current_user!(options, **)
+ options[:model].created_by =
+ options[:current_user]
end
end
#:op-api end
- it { Song::Create.({ }).inspect("model").must_equal %{<Result:false [#<struct DocsOperationAPIExampleTest::Song id=nil, title=nil, created_by=nil>] >} }
- it { Song::Create.({ title: "Nothin'" }, "current_user"=>Module).inspect("model").must_equal %{<Result:true [#<struct DocsOperationAPIExampleTest::Song id=nil, title="Nothin'", created_by=Module>] >} }
+ it { Song::Create.(params: {}).inspect(:model).must_equal %{<Result:false [#<struct DocsOperationAPIExampleTest::Song id=nil, title=nil, created_by=nil>] >} }
+ it { Song::Create.(params: { title: "Nothin'" }, :current_user=>Module).inspect(:model).must_equal %{<Result:true [#<struct DocsOperationAPIExampleTest::Song id=nil, title="Nothin'", created_by=Module>] >} }
end
class DocsOperationInheritanceTest < Minitest::Spec
Song = Struct.new(:id, :title, :created_by) do
@@ -225,11 +224,11 @@
step Model( Song, :new )
step Contract::Build( constant: MyContract )
end
#:inh-new end
- puts New["pipetree"].inspect(style: :row)
+ puts Trailblazer::Operation::Inspect.(New, style: :row)
=begin
#:inh-new-pipe
0 =======================>>operation.new
1 ==========================&model.build
2 =======================>contract.build
@@ -241,11 +240,11 @@
step Contract::Validate()
step Contract::Persist()
end
#:inh-create end
- puts Create["pipetree"].inspect(style: :row)
+ puts Trailblazer::Operation::Inspect.(Create, style: :row)
=begin
#:inh-create-pipe
0 =======================>>operation.new
1 ==========================&model.build
2 =======================>contract.build
@@ -277,23 +276,23 @@
class Song::New < MyApp::Operation::New
step Model( Song, :new ), override: true
end
#:override-new end
- puts Song::New["pipetree"].inspect(style: :row)
+ puts Trailblazer::Operation::Inspect.(Song::New, style: :row)
=begin
#:override-pipe
Song::New["pipetree"].inspect(style: :row)
0 =======================>>operation.new
1 ==========================&model.build
2 =======================>contract.build
#:override-pipe end
=end
it do
- Song::New["pipetree"].inspect.must_equal %{[>operation.new,>model.build,>contract.build]}
- Song::New.().inspect("model").must_equal %{<Result:true [#<struct DocsOperationInheritanceTest::Song id=nil, title=nil, created_by=nil>] >}
+ Trailblazer::Operation::Inspect.(Song::New).must_equal %{[>model.build,>contract.build]}
+ Song::New.(params: {}).inspect(:model).must_equal %{<Result:true [#<struct DocsOperationInheritanceTest::Song id=nil, title=nil, created_by=nil>] >}
end
end
class DocsOperationStepOptionsTest < Minitest::Spec
Song = Struct.new(:title) do
@@ -307,11 +306,11 @@
class New < Trailblazer::Operation
step Model( Song, :new )
end
#:name-auto end
- puts New["pipetree"].inspect(style: :row)
+ puts Trailblazer::Operation::Inspect.(New, style: :row)
=begin
#:name-auto-pipe
0 =======================>>operation.new
1 ==========================&model.build
#:name-auto-pipe end
@@ -321,19 +320,19 @@
class Update < New
step Model(Song, :find_by), replace: "model.build"
end
#:replace-inh end
- puts Update["pipetree"].inspect(style: :row)
+ puts Trailblazer::Operation::Inspect.(Update, style: :row)
=begin
#:replace-inh-pipe
0 =======================>>operation.new
2 ==========================&model.build
#:replace-inh-pipe end
=end
- it { Update.({}).inspect("model").must_equal %{<Result:false [nil] >} }
+ it { Update.(params: {}).inspect(:model).must_equal %{<Result:false [nil] >} }
# #:delete-inh
# class Noop < New
# step nil, delete: "model.build"
@@ -347,23 +346,23 @@
# 0 =======================>>operation.new
# 2 ==========================&model.build
# #:delete-inh-pipe end
# =end
-# it { Noop.({}).inspect("model").must_equal %{<Result:false [nil] >} }
+# it { Noop.({}).inspect(:model).must_equal %{<Result:false [nil] >} }
end
class ManualNameTest < Minitest::Spec
#:name-manu
class New < Trailblazer::Operation
- step Model( Song, :new ), name: "build.song.model"
+ step(Model( Song, :new ), {name: "build.song.model"})
step :validate_params!, name: "my.params.validate"
# ..
end
#:name-manu end
- puts New["pipetree"].inspect(style: :row)
+ puts Trailblazer::Operation::Inspect.(New, style: :row)
=begin
#:name-manu-pipe
0 =======================>>operation.new
1 =====================&build.song.model
2 ===================&my.params.validate
@@ -378,25 +377,25 @@
step :validate_params!, before: "model.build"
# ..
end
#:pos-before end
- puts New["pipetree"].inspect(style: :row)
+ puts Trailblazer::Operation::Inspect.(New, style: :row)
=begin
#:pos-before-pipe
0 =======================>>operation.new
1 =====================&validate_params!
2 ==========================&model.build
#:pos-before-pipe end
=end
#:pos-inh
class Create < New
- step :policy!, after: "operation.new"
+ step :policy!, before: "model.build"
end
#:pos-inh end
- puts Create["pipetree"].inspect(style: :row)
+ puts Trailblazer::Operation::Inspect.(Create, style: :row)
=begin
#:pos-inh-pipe
0 =======================>>operation.new
1 ==============================&policy!
2 =====================&validate_params!