values 1.2.0 → 1.2.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +4 -2
- data/VERSION +1 -1
- data/lib/values.rb +3 -5
- data/spec/values_spec.rb +10 -0
- data/values.gemspec +2 -2
- metadata +4 -4
data/README.md
CHANGED
@@ -1,12 +1,14 @@
|
|
1
1
|
Values is a tiny library for creating value objects in ruby.
|
2
2
|
These mostly look like classes created using Struct, but fix two problems with those:
|
3
3
|
|
4
|
-
|
4
|
+
Struct constructors can take less than the default number of arguments and set other fields as nil:
|
5
|
+
|
5
6
|
Point = Struct.new(:x,:y)
|
6
7
|
Point.new(1)
|
7
8
|
=> #<struct Point x=1, y=nil>
|
8
9
|
|
9
|
-
|
10
|
+
Structs are also mutable:
|
11
|
+
|
10
12
|
Point = Struct.new(:x,:y)
|
11
13
|
p = Point.new(1,2)
|
12
14
|
p.x = 2
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
1.2.
|
1
|
+
1.2.1
|
data/lib/values.rb
CHANGED
@@ -20,17 +20,15 @@ class Value
|
|
20
20
|
|
21
21
|
def eql?(other)
|
22
22
|
return false if other.class != self.class
|
23
|
-
|
24
|
-
|
25
|
-
result = result && self.send(field) == other.send(field)
|
23
|
+
self.class::VALUE_ATTRS.all? do |field|
|
24
|
+
self.send(field) == other.send(field)
|
26
25
|
end
|
27
|
-
return result
|
28
26
|
end
|
29
27
|
|
30
28
|
def hash
|
31
29
|
result = 0
|
32
30
|
self.class::VALUE_ATTRS.each do |field|
|
33
|
-
result += field.hash
|
31
|
+
result += self.send(field).hash
|
34
32
|
end
|
35
33
|
return result + self.class.hash
|
36
34
|
end
|
data/spec/values_spec.rb
CHANGED
@@ -61,11 +61,21 @@ describe 'values' do
|
|
61
61
|
Point.new(0,0).should_not == Y.new(0,0)
|
62
62
|
end
|
63
63
|
|
64
|
+
it 'is not equal to another value with different fields' do
|
65
|
+
Point.new(0,0).should_not == Point.new(0,1)
|
66
|
+
Point.new(0,0).should_not == Point.new(1,0)
|
67
|
+
end
|
68
|
+
|
64
69
|
it 'has an equal hash if the fields are equal' do
|
65
70
|
p = Point.new(0,0)
|
66
71
|
p.hash.should == Point.new(0,0).hash
|
67
72
|
end
|
68
73
|
|
74
|
+
it 'has a non-equal hash if the fields are different' do
|
75
|
+
p = Point.new(0,0)
|
76
|
+
p.hash.should_not == Point.new(1,0).hash
|
77
|
+
end
|
78
|
+
|
69
79
|
it 'does not have an equal hash if the class is different' do
|
70
80
|
Point.new(0,0).hash.should_not == Y.new(0,0).hash
|
71
81
|
end
|
data/values.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{values}
|
8
|
-
s.version = "1.2.
|
8
|
+
s.version = "1.2.1"
|
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"]
|
12
|
-
s.date = %q{2011-
|
12
|
+
s.date = %q{2011-04-06}
|
13
13
|
s.description = %q{Simple immutable value objects for ruby.
|
14
14
|
|
15
15
|
Make a new value class: Point = Value.new(:x, :y)
|
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 1
|
7
7
|
- 2
|
8
|
-
-
|
9
|
-
version: 1.2.
|
8
|
+
- 1
|
9
|
+
version: 1.2.1
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Tom Crayford
|
@@ -14,7 +14,7 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date: 2011-
|
17
|
+
date: 2011-04-06 00:00:00 +01:00
|
18
18
|
default_executable:
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
@@ -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: 3256064136083469828
|
115
115
|
segments:
|
116
116
|
- 0
|
117
117
|
version: "0"
|