class Equalizer

Define equality, equivalence and inspection methods

Constants

VERSION

Gem version

Public Class Methods

new(*keys) click to toggle source

Initialize an Equalizer with the given keys

Will use the keys with which it is initialized to define cmp?, hash, and inspect

@param [Array<Symbol>] keys

@return [undefined]

@api private

# File lib/equalizer.rb, line 14
def initialize(*keys)
  @keys = keys
  define_methods
  freeze
end

Private Instance Methods

define_cmp_method() click to toggle source

Define an cmp? method based on the instance's values identified by keys

@return [undefined]

@api private

# File lib/equalizer.rb, line 51
def define_cmp_method
  keys = @keys
  define_method(:cmp?) do |comparator, other|
    keys.all? do |key|
      __send__(key).public_send(comparator, other.__send__(key))
    end
  end
  private :cmp?
end
define_hash_method() click to toggle source

Define a hash method based on the instance's values identified by keys

@return [undefined]

@api private

# File lib/equalizer.rb, line 66
def define_hash_method
  keys = @keys
  define_method(:hash) do | |
    keys.map(&method(:send)).push(self.class).hash
  end
end
define_inspect_method() click to toggle source

Define an inspect method that reports the values of the instance's keys

@return [undefined]

@api private

# File lib/equalizer.rb, line 78
def define_inspect_method
  keys = @keys
  define_method(:inspect) do | |
    klass = self.class
    name  = klass.name || klass.inspect
    "#<#{name}#{keys.map { |key| " #{key}=#{__send__(key).inspect}" }.join}>"
  end
end
define_methods() click to toggle source

Define the equalizer methods based on keys

@return [undefined]

@api private

# File lib/equalizer.rb, line 40
def define_methods
  define_cmp_method
  define_hash_method
  define_inspect_method
end
included(descendant) click to toggle source

Hook called when module is included

@param [Module] descendant

the module or class including Equalizer

@return [self]

@api private

Calls superclass method
# File lib/equalizer.rb, line 30
def included(descendant)
  super
  descendant.module_eval { include Methods }
end