Created
March 22, 2012 22:19
-
-
Save kenichi/2165047 to your computer and use it in GitHub Desktop.
bizzarro dynamic attributes
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 method_missing(meth, *args) | |
meth_s = meth.to_s | |
key = meth_s.sub(/=$/,'') | |
aattr = self.account_attributes.find_by_key key | |
if aattr | |
case meth_s | |
when aattr.key | |
aattr.value | |
when "#{aattr.key}=" | |
if args[0].nil? | |
aattr.destroy | |
else | |
aattr.value = args[0] | |
aattr.save! | |
end | |
end | |
elsif meth_s[meth_s.length - 1] == '=' | |
self.account_attributes.create! :key => key, :value => args[0] | |
else | |
super(meth, *args) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
bizzarro attribute is bizzarro attribute