| Path: | README |
| Last Update: | Tue Nov 13 14:02:53 +0100 2007 |
RConditions generates a bang method (for example, Array#empty!) for each predicate method (for example, Array#empty?). The bang method will pass if the predicate method passes, otherwise it will throw an exception. The exception will include auto-generated modules that give information which predicate methods passed or failed in the bang method‘s execution.
RConditions is hosted on RubyForge, and published as a gem. Installation is straightforward:
You may also download the gem on RConditions's RubyForge project page.
require "rconditions"
class Posting
attr_writer :spam, :active
def spam?
@spam
end
def active?
@active
end
def visible?
!spam? && active?
end
end
class User
attr_writer :admin
def admin?
@admin
end
def can_view_posting?(posting)
admin? || posting.visible?
end
end
user = User.new
user.admin = false
posting = Posting.new
posting.active = false
posting.spam = true
begin
user.can_view_posting!(posting)
rescue Posting::SpamError => e
p e
rescue Posting::NotActiveError
puts "should not get here"
end
# => #<Exception: User::NotAdminError, Posting::SpamError,
# Posting::NotVisibleError, User::NotCanViewPostingError>
As you can see, the raised exception includes modules for each of the evaluated predicate methods: User#can_view_posting!(posting) in the context above called User#can_view_posting?(posting), which called User#admin? and Posting#visible?, which called Posting#spam?.
The results were
The names of the included exception modules are derived from the names of the predicate methods:
As almost all predicate methods are extended by default, RConditions carries a performance penalty which varies with the number of calls to predicate methods. A simple test setup using mongrel/rails was slowed down by more than 30% by adding require "rconditions" to the bottom of its enviroment.rb.
The performance penalty can be avoided by applying RConditions only to new methods, that is, methods defined after require "rconditions". To achieve this, set ::RCONDITIONS_ONLY_FOR_NEW_METHODS = true before the require call. Our test setup had no measurable slowdown afterwards.
The test suite of RConditions passes for
If you use another ruby version, please check whether the tests pass.
RConditions is released under the MIT license.
RConditions is written by Norman Timmler and Tammo Freese.