Created
April 22, 2009 02:52
-
-
Save rx/99555 to your computer and use it in GitHub Desktop.
Required paramters in a multi parameter method
This file contains 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 drequiredopts(*names) | |
names.each do |name| | |
if self.send(name).nil? | |
devfail("%s is a required option for %s" % [name, self.class]) | |
end | |
end | |
end | |
# Iterate over a hash, treating each member as an attribute. | |
def set_options(options) | |
options.each do |param,value| | |
method = param.to_s + "=" | |
begin | |
self.send(method, value) | |
rescue NoMethodError | |
raise ArgumentError, "Invalid parameter %s to object class %s" % | |
[param,self.class.to_s] | |
end | |
end | |
end | |
# Take a hash and convert all of the keys to symbols if possible. | |
def symbolize_options(options) | |
options.inject({}) do |hash, opts| | |
if opts[0].respond_to? :intern | |
hash[opts[0].intern] = opts[1] | |
else | |
hash[opts[0]] = opts[1] | |
end | |
hash | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment