Equitable

Equitable

This mixin provides methods of equality based on a single # method which must return a list of accessors used as the identity keys.

It also provides a “shortcut” for creating the # method based on given accessors and returns the Equitable module for inclusion.

  include Equitable(: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

Public Class Methods

identify(base, *accessors) click to toggle source
    # File lib/more/facets/equitable.rb, line 73
73:   def self.identify(base, *accessors)
74:     base.send(:define_method, :identity){ accessors }
75:     self
76:   end

Public Instance Methods

==(o) click to toggle source
    # File lib/more/facets/equitable.rb, line 78
78:   def ==(o)
79:     identity.all?{ |a| send(a) == o.send(a) }
80:   end
eql?(o) click to toggle source
    # File lib/more/facets/equitable.rb, line 82
82:   def eql?(o)
83:     identity.all?{ |a| send(a).eql?(o.send(a)) }
84:   end
hash() click to toggle source
    # File lib/more/facets/equitable.rb, line 86
86:   def hash
87:     identity.inject(0){ |memo, a| memo ^ send(a).hash }
88:   end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.