Sha256: 170500c0a21e51786796cbf8674dbc8217863c57ebb0a61c499acd692b5fff4d

Contents?: true

Size: 1.41 KB

Versions: 26

Compression:

Stored size: 1.41 KB

Contents

#--
# NOTE Better off as a parameterized module?
#++

class Module

  # Generates identity/key methods based on specified attributes.
  #
  #  equate_on :a, :b
  #
  # _is equivalent to_
  #
  #   def ==(o)
  #     self.a == o.a && self.b == o.b
  #   end
  #
  #   def eql?(o)
  #     self.a.eql?(o.a) && self.b.eql?(o.b)
  #   end
  #
  #   def hash()
  #     self.a.hash ^ self.b.hash
  #   end
  #
  def equate_on(*fields)
    code = ""
    code << "def ==(o) "   << fields.map {|f| "self.#{f} == o.#{f}" }.join(" && ")    << " end\n"
    code << "def eql?(o) " << fields.map {|f| "self.#{f}.eql?(o.#{f})" }.join(" && ") << " end\n"
    code << "def hash() "  << fields.map {|f| "self.#{f}.hash" }.join(" ^ ")          << " end\n"
    class_eval( code )
    fields
  end

  # Deprecated usage.
  alias_method :key_attributes, :equate_on

end



#  _____         _
# |_   _|__  ___| |_
#   | |/ _ \/ __| __|
#   | |  __/\__ \ |_
#   |_|\___||___/\__|
#
=begin test

  require 'test/unit'

  class TCModule < Test::Unit::TestCase

    def test_equate_on
      c = Class.new
      c.class_eval { attr_accessor :a, :b ; equate_on :a,:b }
      c1,c2 = c.new,c.new
      c1.a = 10; c1.b = 20
      c2.a = 10; c2.b = 20
      assert_equal( c1, c2 )
      c1.a = 10; c1.b = 10
      c2.a = 10; c2.b = 20
      assert_not_equal( c1, c2 )
      c1.a = 10; c1.b = 20
      c2.a = 20; c2.b = 20
      assert_not_equal( c1, c2 )
    end

  end

=end

Version data entries

26 entries across 26 versions & 1 rubygems

Version Path
facets-0.9.0 lib/nano/module/equate_on.rb
facets-1.0.3 packages/core/lib/facet/module/equate_on.rb
facets-1.0.0 lib/facet/module/equate_on.rb
facets-1.2.1 lib/facets/core/module/equate_on.rb
facets-1.1.0 lib/facet/module/equate_on.rb
facets-1.2.0 lib/facets/core/module/equate_on.rb
facets-1.3.0 lib/facets/core/module/equate_on.rb
facets-1.3.1 lib/facets/core/module/equate_on.rb
facets-1.3.3 lib/facets/core/module/equate_on.rb
facets-1.3.2 lib/facets/core/module/equate_on.rb
facets-1.4.2 lib/facets/core/module/equate_on.rb
facets-1.4.0 lib/facets/core/module/equate_on.rb
facets-1.4.1 lib/facets/core/module/equate_on.rb
facets-1.4.3 lib/facets/core/module/equate_on.rb
facets-1.4.4 lib/facets/core/module/equate_on.rb
facets-1.4.5 lib/facets/core/module/equate_on.rb
facets-1.7.0 lib/facets/core/module/equate_on.rb
facets-1.7.30 lib/facets/core/module/equate_on.rb
facets-1.7.38 lib/facets/core/module/equate_on.rb
facets-1.7.46 lib/facets/core/module/equate_on.rb