Equatable
This mixin provides methods of equality based on a single identity method which must return a list of accessors used as the identity keys.
It also provides a "shortcut" for creating the identity method based on given accessors and returns the Equatable module for inclusion.
include Equatable(:a, :b)
is equivalent to including a module containing:
def ==(other) self.a == other.a && self.b == other.b end def eql?(other) self.a.eql?(other.a) && self.b.eql?(other.b) end def hash() self.a.hash ^ self.b.hash end
Methods
Public Class methods
[ show source ]
# File lib/facets/equatable.rb, line 73 def self.identify(base, *accessors) base.send(:define_method, :identity){ accessors } self end
Public Instance methods
[ show source ]
# File lib/facets/equatable.rb, line 78 def ==(o) identity.all?{ |a| send(a) == o.send(a) } end
[ show source ]
# File lib/facets/equatable.rb, line 82 def eql?(o) identity.all?{ |a| send(a).eql?(o.send(a)) } end
[ show source ]
# File lib/facets/equatable.rb, line 86 def hash identity.inject(0){ |memo, a| memo ^ send(a).hash } end