trailblazer 2.0.7 → 2.1.0.beta1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (63) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGES.md +35 -1
  3. data/Gemfile +6 -12
  4. data/README.md +3 -1
  5. data/Rakefile +6 -17
  6. data/lib/trailblazer.rb +7 -4
  7. data/lib/trailblazer/deprecation/call.rb +46 -0
  8. data/lib/trailblazer/deprecation/context.rb +43 -0
  9. data/lib/trailblazer/operation/contract.rb +40 -9
  10. data/lib/trailblazer/operation/deprecations.rb +21 -0
  11. data/lib/trailblazer/operation/guard.rb +5 -5
  12. data/lib/trailblazer/operation/model.rb +15 -10
  13. data/lib/trailblazer/operation/nested.rb +56 -85
  14. data/lib/trailblazer/operation/persist.rb +4 -2
  15. data/lib/trailblazer/operation/policy.rb +16 -7
  16. data/lib/trailblazer/operation/pundit.rb +3 -3
  17. data/lib/trailblazer/operation/representer.rb +5 -0
  18. data/lib/trailblazer/operation/rescue.rb +12 -9
  19. data/lib/trailblazer/operation/validate.rb +36 -29
  20. data/lib/trailblazer/operation/wrap.rb +49 -11
  21. data/lib/trailblazer/task.rb +20 -0
  22. data/lib/trailblazer/version.rb +1 -1
  23. data/test/benchmark.rb +63 -0
  24. data/test/deprecation/call_test.rb +42 -0
  25. data/test/deprecation/context_test.rb +19 -0
  26. data/test/docs/contract_test.rb +73 -53
  27. data/test/docs/dry_test.rb +2 -2
  28. data/test/docs/fast_test.rb +133 -13
  29. data/test/docs/guard_test.rb +28 -35
  30. data/test/docs/macro_test.rb +1 -1
  31. data/test/docs/model_test.rb +13 -13
  32. data/test/docs/nested_test.rb +54 -122
  33. data/test/docs/operation_test.rb +42 -43
  34. data/test/docs/pundit_test.rb +16 -16
  35. data/test/docs/representer_test.rb +18 -18
  36. data/test/docs/rescue_test.rb +29 -29
  37. data/test/docs/trace_test.rb +82 -0
  38. data/test/docs/wrap_test.rb +59 -26
  39. data/test/module_test.rb +75 -75
  40. data/test/nested_test.rb +293 -0
  41. data/test/operation/contract_test.rb +23 -153
  42. data/test/operation/dsl/contract_test.rb +9 -9
  43. data/test/operation/dsl/representer_test.rb +169 -169
  44. data/test/operation/model_test.rb +15 -21
  45. data/test/operation/persist_test.rb +18 -11
  46. data/test/operation/pundit_test.rb +25 -23
  47. data/test/operation/representer_test.rb +254 -254
  48. data/test/test_helper.rb +5 -2
  49. data/test/variables_test.rb +158 -0
  50. data/trailblazer.gemspec +1 -1
  51. data/untitled +33 -0
  52. metadata +25 -27
  53. data/lib/trailblazer/operation/callback.rb +0 -35
  54. data/lib/trailblazer/operation/procedural/contract.rb +0 -15
  55. data/lib/trailblazer/operation/procedural/validate.rb +0 -22
  56. data/test/operation/callback_test.rb +0 -70
  57. data/test/operation/dsl/callback_test.rb +0 -106
  58. data/test/operation/params_test.rb +0 -36
  59. data/test/operation/pipedream_test.rb +0 -59
  60. data/test/operation/pipetree_test.rb +0 -104
  61. data/test/operation/present_test.rb +0 -24
  62. data/test/operation/resolver_test.rb +0 -47
  63. data/test/operation_test.rb +0 -143
@@ -1,143 +0,0 @@
1
- require "test_helper"
2
-
3
- module Inspect
4
- def inspect
5
- "<#{self.class.to_s.split("::").last} @model=#{@model}>"
6
- end
7
- alias_method :to_s, :inspect
8
- end
9
-
10
- class OperationRunTest < MiniTest::Spec
11
- class Operation < Trailblazer::Operation
12
-
13
- require "trailblazer/operation/contract"
14
- include Contract::Explicit
15
-
16
- # allow providing your own contract.
17
- self["contract.default.class"] = class MyContract
18
- def initialize(*)
19
- end
20
- def call(params)
21
- Mock::Result.new(params)
22
- end
23
-
24
- def errors
25
- Struct.new(:to_s).new("Op just calls #to_s on Errors!")
26
- end
27
- self
28
- end
29
-
30
- def process(params)
31
- model = Object
32
- validate(params, model: model)
33
- end
34
-
35
- include Inspect
36
- end
37
-
38
-
39
- describe "Raise" do
40
- class Follow < Trailblazer::Operation
41
- require "trailblazer/operation/raise"
42
- require "trailblazer/operation/contract"
43
- include Contract::Explicit
44
- contract do
45
- end
46
-
47
- module Validate
48
- def validate(is_valid)
49
- is_valid
50
- end
51
- end
52
- include Validate
53
- include Contract::Raise
54
-
55
- def process(params)
56
- validate(params[:is_valid])
57
- end
58
- end
59
- # #validate raises exception when invalid.
60
- it do
61
- exception = assert_raises(Trailblazer::Operation::InvalidContract) { Follow.(is_valid: false) }
62
- # exception.message.must_equal "Op just calls #to_s on Errors!"
63
- end
64
- it { Follow.(is_valid:true).success?.must_equal true }
65
- end
66
- end
67
-
68
-
69
- class OperationTest < MiniTest::Spec
70
- # #validate yields contract when valid
71
- class OperationWithValidateBlock < Trailblazer::Operation
72
- require "trailblazer/operation/contract"
73
- include Contract::Explicit
74
- self["contract.default.class"] = class Contract
75
- def initialize(*)
76
- end
77
-
78
- def call(params)
79
- Mock::Result.new(params)
80
- end
81
-
82
- attr_reader :errors
83
- self
84
- end
85
-
86
- def process(params)
87
- validate(params, model: Object.new) do |c|
88
- self["secret_contract"] = c.class
89
- end
90
- end
91
- end
92
-
93
- it { OperationWithValidateBlock.(false)["secret_contract"].must_equal nil }
94
- it { OperationWithValidateBlock.(true)["secret_contract"].to_s.must_equal "Mock::Result" }
95
-
96
-
97
- # test validate wit if/else
98
- class OperationWithValidateAndIf < Trailblazer::Operation
99
- require "trailblazer/operation/contract"
100
- include Contract::Explicit
101
- self["contract.default.class"] = class Contract
102
- def initialize(*)
103
- end
104
-
105
- def call(params)
106
- Mock::Result.new(params)
107
- end
108
- attr_reader :errors
109
- self
110
- end
111
-
112
- def process(params)
113
- if validate(params, model: Object.new)
114
- self["secret_contract"] = contract.class
115
- else
116
- self["secret_contract"] = "so wrong!"
117
- end
118
- end
119
- end
120
-
121
- it { OperationWithValidateAndIf.(false)["secret_contract"].must_equal "so wrong!" }
122
- it { OperationWithValidateAndIf.(true)["secret_contract"].must_equal OperationWithValidateAndIf::Contract }
123
- end
124
-
125
-
126
- class OperationErrorsTest < MiniTest::Spec
127
- class Operation < Trailblazer::Operation
128
- require "trailblazer/operation/contract"
129
- include Contract::Explicit
130
- contract do
131
- property :title, validates: { presence: true }
132
- end
133
-
134
- def process(params)
135
- validate(params, model: OpenStruct.new) {}
136
- end
137
- end
138
-
139
- it do
140
- result = Operation.({})
141
- result["errors.contract"].to_s.must_equal "{:title=>[\"can't be blank\"]}"
142
- end
143
- end