aboutsummaryrefslogtreecommitdiffstats
path: root/activeresource/lib
diff options
context:
space:
mode:
authorJeremy Kemper <jeremy@bitsweat.net>2006-12-22 21:42:52 +0000
committerJeremy Kemper <jeremy@bitsweat.net>2006-12-22 21:42:52 +0000
commitc172154054f2f1908820459bb63e8e8645136ea7 (patch)
tree03d4caf01cd4c822f56ff9e54b5dddedddcc5bde /activeresource/lib
parent68d2926ab09d49086eb4101c53d8ae604bd4b334 (diff)
downloadrails-c172154054f2f1908820459bb63e8e8645136ea7.tar.gz
rails-c172154054f2f1908820459bb63e8e8645136ea7.tar.bz2
rails-c172154054f2f1908820459bb63e8e8645136ea7.zip
Base#==, eql?, and hash methods. == returns true if its argument is identical to self or if it's an instance of the same class, is not new?, and has the same id. eql? is an alias for ==. hash delegates to id.
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@5773 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activeresource/lib')
-rw-r--r--activeresource/lib/active_resource/base.rb16
1 files changed, 16 insertions, 0 deletions
diff --git a/activeresource/lib/active_resource/base.rb b/activeresource/lib/active_resource/base.rb
index 9a810e4adf..a8918b1eee 100644
--- a/activeresource/lib/active_resource/base.rb
+++ b/activeresource/lib/active_resource/base.rb
@@ -111,6 +111,22 @@ module ActiveResource
attributes[self.class.primary_key] = id
end
+ # True if and only if +other+ is the same object or is an instance of the same class, is not new?, and has the same id.
+ def ==(other)
+ other.equal?(self) || (other.instance_of?(self.class) && !other.new? && other.id == id)
+ end
+
+ # Delegates to ==
+ def eql?(other)
+ self == other
+ end
+
+ # Delegates to id in order to allow two resources of the same type and id to work with something like:
+ # [Person.find(1), Person.find(2)] & [Person.find(1), Person.find(4)] # => [Person.find(1)]
+ def hash
+ id.hash
+ end
+
def save
new? ? create : update
end