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 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
- => SOME EXCEPTION
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
- => SOME EXCEPTION
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.1.0
@@ -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
@@ -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
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{values}
8
- s.version = "1.0.1"
8
+ s.version = "1.1.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Tom Crayford"]
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
- version: 1.0.1
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: -580871131718748459
114
+ hash: 2901597111501063009
115
115
  segments:
116
116
  - 0
117
117
  version: "0"