will / switchbox

declarative ruby conditions

This URL has Read+Write access

Will Leinweber (author)
Thu Aug 21 22:34:17 -0700 2008
commit  8131dd1c37973e8e4ef3ef36ce1d0422c4b09780
tree    59b9c10b971a211e9c305e6d0bcc6f3c096229ac
parent  a3499a16f3f617a5dcc2c97cea9872a28ac78a2a
name age message
file README.md Loading commit data...
directory lib/
directory spec/
file test.rb
README.md

SwitchBox

Will Leinweber – blog

About

SwitchBox lets you declare simple conditions and combine them into more complex conditions. These are used to decide which action to run.

The upshot is that messy if/elsif/else blocks are no longer necessary. All conditions are lazily evaluated and will only be ran once.

Conditions

Conditions have a name and a block. They are only run when checking for which action to call, and their result is cached and only ran once. The block doesn't necessarily have to evaluate a boolean, but SwitchBox forces the result into a boolean value before caching.

record = some_active_record_object

box = SwitchBox.new
box.condition(:saved) { record.save }

In this example, a condition named "saved" is created. Behind-the-scenes, you also get a "not_saved" condition.

Actions

Actions are stored in the order they're declared, and the very first action to meet all of its conditions will be executed, and no others.

# ... continuing from the above code
box.action(:saved)     { render :text => "the record was saved!" }
box.action(:not_saved) { render :text => "there was a problem" }
box.go # will result in calling the first block

SwitchBox#go will look at the conditions for each action, and run appropriate action.

Alternate syntax

The above syntax is nice because you can pass the box around as needed, but for simple things, you can just do it all in one block

SwitchBox.new do
  condition(:always_true) { true }
  action(:always_true) { puts "always printed" }
end

In this case you don't need to call #go, it gets called at the end of the block.