Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def method_missing(method_name, *args, &block)
- if method_name =~ /^swap_(.+)_with(!?)$/
- property = $1
- is_bang = $2 == '!'
- sibling = args[0]
- #errors
- errors = {
- :no_response => 'does not respond to "%s"' % property,
- :bang => 'Can not execute bang method on objects that do not respond to "save" method'
- }
- raise '%s %s' % [self.class.name, errors[:no_response]] unless self.respond_to?(property)
- raise '%s %s' % [sibling.class.name, errors[:no_response]] unless sibling.respond_to?(property)
- raise errors[:bang] if is_bang && (!self.respond_to?('save') || !sibling.respond_to?('save'))
- old_sibling_property = sibling.send(property)
- sibling.send("#{property}=", self.send(property))
- self.send("#{property}=", old_sibling_property)
- [self, sibling].collect(&:save) if is_bang
- return self
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement