41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
|
# File 'lib/attribute_driven.rb', line 41
def equality_based_on(*args)
attributes = AttributeDriven.attributes_from(args)
options = AttributeDriven.options_from(args)
raise "unknown option value of :class_check => #{options[:class_check].inspect}" unless [nil, :equal?, :kind_of?].include?(options[:class_check])
class_eval(" def eql?(other)\n return true if equal?(other)\n \#{options[:class_check] ? \"return false unless \#{options[:class_check] == :equal? ? 'other.class.equal?(self.class)' : 'other.kind_of?(self.class)'}\" : \"\"}\n \#{attributes.inspect}.inject(true) do |result, attribute|\n result &&= (self.send(attribute) == other.send(attribute))\n end\n end\n alias_method :==, :eql?\n \n def hash\n \#{attributes.inspect}.inject(0) do |result, attribute|\n result += self.send(attribute).hash if self.send(attribute)\n result\n end\n end\n \n protected\n \#{attributes.inspect}.each do |attribute|\n attr_reader attribute unless instance_methods.include?(attribute)\n end\n EOS\nend\n", __FILE__, __LINE__ + 1)
|