Created
March 13, 2010 07:50
-
-
Save davidlee/331194 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def ensure_argument list | |
list.each do |value, tests| | |
[tests].flatten.each do |test| | |
case test.to_s | |
when /^is_an?_(.*)$/ | |
value.is_a? $1.classify.constantize | |
when /^is_(.*)$/ | |
value.respond_to?($1) && value.send($1) | |
when /^(has|can)_(.*)/ | |
value.respond_to? $2 | |
end || raise(ArgumentError, "Expected #{value} to #{test}") | |
end | |
end | |
end | |
def debit(debitor, balance, descr) | |
ensure_argument debitor => [:has_account, :is_active], | |
balance => :is_a_bignum, descr => :is_a_string | |
# ... | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment