module Ardm::Equalizer

Public Instance Methods

equalize(*methods) click to toggle source
# File lib/ardm/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/ardm/support/equalizer.rb, line 11
    def define_eql_method(methods)
      class_eval <<-RUBY, __FILE__, __LINE__ + 1
        def eql?(other)
          return true if equal?(other)
          instance_of?(other.class) &&
          #{methods.map { |method| "#{method}.eql?(other.#{method})" }.join(' && ')}
        end
      RUBY
    end
define_equivalent_method(methods) click to toggle source
# File lib/ardm/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 <<-RUBY, __FILE__, __LINE__ + 1
        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
      RUBY
    end
define_hash_method(methods) click to toggle source
# File lib/ardm/support/equalizer.rb, line 40
    def define_hash_method(methods)
      class_eval <<-RUBY, __FILE__, __LINE__ + 1
        def hash
          self.class.hash ^ #{methods.map { |method| "#{method}.hash" }.join(' ^ ')}
        end
      RUBY
    end