https://github.com/will/switchbox
declarative ruby conditions
https://github.com/will/switchbox
Last synced: 9 months ago
JSON representation
declarative ruby conditions
- Host: GitHub
- URL: https://github.com/will/switchbox
- Owner: will
- Created: 2008-08-14T04:33:27.000Z (over 17 years ago)
- Default Branch: master
- Last Pushed: 2008-08-22T05:34:45.000Z (over 17 years ago)
- Last Synced: 2025-04-05T03:27:25.365Z (12 months ago)
- Language: Ruby
- Homepage:
- Size: 75.2 KB
- Stars: 3
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
SwitchBox
=========
Will Leinweber – [blog](http://bitfission.com)
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.