@@ -37,6 +37,64 @@ private class OperationWithCallbacks < Avram::Operation
3737 end
3838end
3939
40+ class OperationWithIf < Avram::Operation
41+ include TestableOperation
42+ attribute number : Int32 = 1
43+
44+ before_run :update_number , if: :should_run_callback?
45+
46+ def run
47+ number.value
48+ end
49+
50+ private def update_number
51+ mark_callback(" before_run_update_number" )
52+ number.value = 10
53+ end
54+
55+ private def should_run_callback?
56+ true
57+ end
58+ end
59+
60+ class OperationWithUnless < Avram::Operation
61+ include TestableOperation
62+ attribute number : Int32 = 1
63+
64+ before_run :update_number , unless: :skip_callback?
65+
66+ def run
67+ number.value
68+ end
69+
70+ private def update_number
71+ mark_callback(" before_run_update_number" )
72+ number.value = 20
73+ end
74+
75+ private def skip_callback?
76+ true
77+ end
78+ end
79+
80+ class OperationWithBlockUnless < Avram::Operation
81+ include TestableOperation
82+ attribute number : Int32 = 1
83+
84+ before_run(unless: :skip_callback? ) do
85+ mark_callback(" before_run_block" )
86+ number.value = 30
87+ end
88+
89+ def run
90+ number.value
91+ end
92+
93+ private def skip_callback?
94+ false
95+ end
96+ end
97+
4098describe " Avram::Operation callbacks" do
4199 it " runs before_run and after_run callbacks" do
42100 OperationWithCallbacks .run do |operation , value |
@@ -49,4 +107,25 @@ describe "Avram::Operation callbacks" do
49107 operation.callbacks_that_ran.should contain " after_run_in_a_block with 4"
50108 end
51109 end
110+
111+ it " runs before_run with if condition" do
112+ OperationWithIf .run do |operation , value |
113+ operation.callbacks_that_ran.should contain " before_run_update_number"
114+ value.should eq 10
115+ end
116+ end
117+
118+ it " does not run before_run with unless condition" do
119+ OperationWithUnless .run do |operation , value |
120+ operation.callbacks_that_ran.should_not contain " before_run_update_number"
121+ value.should eq 1
122+ end
123+ end
124+
125+ it " runs before_run block with unless condition" do
126+ OperationWithBlockUnless .run do |operation , value |
127+ operation.callbacks_that_ran.should contain " before_run_block"
128+ value.should eq 30
129+ end
130+ end
52131end
0 commit comments