values 1.0.1 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +8 -2
- data/VERSION +1 -1
- data/lib/values.rb +22 -0
- data/spec/values_spec.rb +21 -0
- data/values.gemspec +1 -1
- metadata +3 -3
data/README.md
CHANGED
@@ -16,8 +16,14 @@ These mostly look like classes created using Struct, but fix two problems with t
|
|
16
16
|
Values fixes both of these:
|
17
17
|
Point = Value.new(:x, :y)
|
18
18
|
Point.new(1)
|
19
|
-
=>
|
19
|
+
=> ArgumentError: wrong number of arguments, 1 for 2
|
20
|
+
from /Users/tcrayford/Projects/ruby/values/lib/values.rb:7:in `block (2 levels) in new
|
21
|
+
from (irb):5:in new
|
22
|
+
from (irb):5
|
23
|
+
from /usr/local/bin/irb:12:in `<main>
|
20
24
|
|
21
25
|
p = Point.new(1,2)
|
22
26
|
p.x = 1
|
23
|
-
=>
|
27
|
+
=> NoMethodError: undefined method x= for #<Point:0x00000100943788 @x=0, @y=1>
|
28
|
+
from (irb):6
|
29
|
+
from /usr/local/bin/irb:12:in <main>
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
1.0
|
1
|
+
1.1.0
|
data/lib/values.rb
CHANGED
@@ -8,8 +8,30 @@ class Value
|
|
8
8
|
fields.each_with_index do |field, index|
|
9
9
|
instance_variable_set('@' + field.to_s, input_fields[index])
|
10
10
|
end
|
11
|
+
@__fields = fields
|
11
12
|
self.freeze
|
12
13
|
end
|
14
|
+
|
15
|
+
def ==(other)
|
16
|
+
self.eql?(other)
|
17
|
+
end
|
18
|
+
|
19
|
+
def eql?(other)
|
20
|
+
return false if other.class != self.class
|
21
|
+
result = true
|
22
|
+
@__fields.each do |field|
|
23
|
+
result = result && self.send(field) == other.send(field)
|
24
|
+
end
|
25
|
+
return result
|
26
|
+
end
|
27
|
+
|
28
|
+
def hash
|
29
|
+
result = 0
|
30
|
+
@__fields.each do |field|
|
31
|
+
result += field.hash
|
32
|
+
end
|
33
|
+
return result + self.class.hash
|
34
|
+
end
|
13
35
|
end
|
14
36
|
end
|
15
37
|
end
|
data/spec/values_spec.rb
CHANGED
@@ -49,4 +49,25 @@ describe 'values' do
|
|
49
49
|
m = Money.new(1, 'USD')
|
50
50
|
lambda {m.instance_variable_set('@amount',2)}.should raise_error
|
51
51
|
end
|
52
|
+
|
53
|
+
describe '#hash and equality' do
|
54
|
+
Y = Value.new(:x, :y)
|
55
|
+
|
56
|
+
it 'is equal to another value with the same fields' do
|
57
|
+
Point.new(0,0).should == Point.new(0,0)
|
58
|
+
end
|
59
|
+
|
60
|
+
it 'is not equal to an object with a different class' do
|
61
|
+
Point.new(0,0).should_not == Y.new(0,0)
|
62
|
+
end
|
63
|
+
|
64
|
+
it 'has an equal hash if the fields are equal' do
|
65
|
+
p = Point.new(0,0)
|
66
|
+
p.hash.should == Point.new(0,0).hash
|
67
|
+
end
|
68
|
+
|
69
|
+
it 'does not have an equal hash if the class is different' do
|
70
|
+
Point.new(0,0).hash.should_not == Y.new(0,0).hash
|
71
|
+
end
|
72
|
+
end
|
52
73
|
end
|
data/values.gemspec
CHANGED
metadata
CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
|
|
4
4
|
prerelease: false
|
5
5
|
segments:
|
6
6
|
- 1
|
7
|
-
- 0
|
8
7
|
- 1
|
9
|
-
|
8
|
+
- 0
|
9
|
+
version: 1.1.0
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Tom Crayford
|
@@ -111,7 +111,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
111
111
|
requirements:
|
112
112
|
- - ">="
|
113
113
|
- !ruby/object:Gem::Version
|
114
|
-
hash:
|
114
|
+
hash: 2901597111501063009
|
115
115
|
segments:
|
116
116
|
- 0
|
117
117
|
version: "0"
|