module DataMapper::Equalizer
Public Instance Methods
equalize(*methods)
click to toggle source
# File lib/dm-core/support/equalizer.rb, line 3 def equalize(*methods) define_eql_method(methods) define_equivalent_method(methods) define_hash_method(methods) end
Private Instance Methods
define_eql_method(methods)
click to toggle source
# File lib/dm-core/support/equalizer.rb, line 11 def define_eql_method(methods) class_eval " def eql?(other) return true if equal?(other) instance_of?(other.class) && #{methods.map { |method| "#{method}.eql?(other.#{method})" }.join(' && ')} end ", __FILE__, __LINE__ + 1 end
define_equivalent_method(methods)
click to toggle source
# File lib/dm-core/support/equalizer.rb, line 21 def define_equivalent_method(methods) respond_to = [] equivalent = [] methods.each do |method| respond_to << "other.respond_to?(#{method.inspect})" equivalent << "#{method} == other.#{method}" end class_eval " def ==(other) return true if equal?(other) return false unless kind_of?(other.class) || other.kind_of?(self.class) #{respond_to.join(' && ')} && #{equivalent.join(' && ')} end ", __FILE__, __LINE__ + 1 end
define_hash_method(methods)
click to toggle source
# File lib/dm-core/support/equalizer.rb, line 40 def define_hash_method(methods) class_eval " def hash self.class.hash ^ #{methods.map { |method| "#{method}.hash" }.join(' ^ ')} end ", __FILE__, __LINE__ + 1 end