forked from tcrayford/Values
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalues.rb
39 lines (33 loc) · 982 Bytes
/
values.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
class Value
def self.new(*fields)
return Class.new do
attr_reader *fields
define_method(:initialize) do |*input_fields|
raise ArgumentError.new("wrong number of arguments, #{input_fields.size} for #{fields.size}") if fields.size != input_fields.size
fields.each_with_index do |field, index|
instance_variable_set('@' + field.to_s, input_fields[index])
end
self.freeze
end
const_set :VALUE_ATTRS, fields
def ==(other)
self.eql?(other)
end
def eql?(other)
return false if other.class != self.class
result = true
self.class::VALUE_ATTRS.each do |field|
result = result && self.send(field) == other.send(field)
end
return result
end
def hash
result = 0
self.class::VALUE_ATTRS.each do |field|
result += field.hash
end
return result + self.class.hash
end
end
end
end