Ran into a case where depending on the job, I'd like to wrap portions of code in an if_enable block, but conditionally.
If we're running our characterization job, then we partition tests based off testcodes to reduce test time on our experiments. For any other job, these tests are all always ran for production testing.
The orignal code is in this format:
Flow.create() do |options|
test_a
test_b
end
My current solution to work around this is:
def a_tests
if job == :charz
if_enable '$enableA' do
yield
end
else
yield
end
end
def b_tests
if job == :charz
if_enable '$enableB' do
yield
end
else
yield
end
end
Flow.create() do |options|
a_tests do
test_a
end
b_tests do
test_b
end
end
What I'd like to do is something like this:
Flow.create() do |options|
a_flag = job == :charz ? '$enableA' : ''
b_flag = job == :charz ? '$enableB' : ''
if_enable a_flag do
test_a
end
if_enable b_flag do
test_b
end
end
Where passing an empty (or could be nil or false) value would result in the tests within the if_enable block still being generated but not gated by any variable checking. Does this functionality exist somewhere already or is my current solution currently the best way? Or is there some other cleaner ruby solution?
One purely flow-level solution using existing APIs would be to force the enable at runtime for non-cz jobs, like this:
Your current solution is also pretty good, though I would enhance it by moving the method definition to your interface, and making it more generic:
Then you have a nice helper to keep your flow logic very clean: