Sha256: b82792691d927b5cec586ef486abcc8b4a529807ef4ba2e0ad83cd84f4ff9463

Contents?: true

Size: 1.49 KB

Versions: 7

Compression:

Stored size: 1.49 KB

Contents

# = Equitable
#
# 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 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
#
module Equitable

  def self.identify(base, *accessors)
    base.send(:define_method, :identity){ accessors }
    self
  end

  def ==(o)
    identity.all?{ |a| send(a) == o.send(a) }
  end

  def eql?(o)
    identity.all?{ |a| send(a).eql?(o.send(a)) }
  end

  def hash
    identity.inject(0){ |memo, a| memo ^ send(a).hash }
  end

end

class Module

  # This function provided a "shortcut" for creating the
  # #identity 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
  #

  def Equitable(*accessors)
    Equitable.identify(self, *accessors)
  end

end

Version data entries

7 entries across 7 versions & 3 rubygems

Version Path
facets-glimmer-3.2.0 lib/standard/facets/equitable.rb
facets-3.1.0 lib/standard/facets/equitable.rb
facets-3.0.0 lib/standard/facets/equitable.rb
facets-2.9.3 lib/standard/facets/equitable.rb
mixers-1.2.0 lib/mixers/equitable.rb
mixers-1.1.0 lib/mixers/equitable.rb
mixers-1.0.0 lib/mixers/equitable.rb