value_object 0.0.3 → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README.md CHANGED
@@ -39,10 +39,10 @@ Read their attributes
39
39
  tom.height # => 176
40
40
  dick.weight # => 60
41
41
 
42
- Test whether value objects are equal
42
+ Test whether value objects are equal (note: #== and #eql? are aliases)
43
43
 
44
44
  tom == dick # => false
45
- tom == harry # => true
45
+ tom.eql?(harry) # => true
46
46
 
47
47
  Test for emptiness
48
48
 
@@ -59,3 +59,26 @@ You can even subclass them again!
59
59
  superman.height # => '6 foot 3'
60
60
  superman.weight # => '235 lbs'
61
61
  superman.power # => 'flies'
62
+
63
+ Turn them into hashes
64
+
65
+ superman.to_hash
66
+ # => {:height => '6 foot 3',
67
+ :weight => '325 lbs',
68
+ :power => 'flies'}
69
+
70
+ Make copies with only one changed value
71
+
72
+ superman.copy_with(:power => 'laser eyes')
73
+ # => #<Superhero:0x3914490
74
+ @height="6 foot 3",
75
+ @weight="235 lbs",
76
+ @power="laser eyes"
77
+ >
78
+
79
+ Hash them. A hash depends on the both the object's class and the
80
+ attribute values for that object, so that if o1 and o2 are both value
81
+ objects, and o1.eql?(o2), then o1.hash == o2.hash
82
+
83
+ superman.hash
84
+ # => 1160641176
@@ -1,3 +1,3 @@
1
1
  module ValueObject
2
- VERSION = "0.0.3"
2
+ VERSION = "0.1.1"
3
3
  end
data/lib/value_object.rb CHANGED
@@ -25,12 +25,34 @@ module ValueObject
25
25
  end
26
26
  end
27
27
 
28
- def ==(other)
28
+ def eql?(other)
29
+ return false if self.class != other.class
29
30
  fields.all? { |f| send(f) == other.send(f) }
30
31
  end
31
32
 
33
+ def ==(other)
34
+ eql?(other)
35
+ end
36
+
32
37
  def empty?
33
38
  fields.all? { |f| send(f).nil? }
34
39
  end
40
+
41
+ def to_hash
42
+ hash = {}
43
+ fields.each { |f| hash[f] = send(f) }
44
+ hash
45
+ end
46
+
47
+ def copy_with(attrs)
48
+ new_attrs = self.to_hash.update(attrs)
49
+ self.class.new(new_attrs)
50
+ end
51
+
52
+ # if the class is the same, and the fields' values are the same,
53
+ # the hash should be the same
54
+ def hash
55
+ to_hash.hash + self.class.hash
56
+ end
35
57
  end
36
58
  end
@@ -23,6 +23,7 @@ class ValueObjectTest < Test::Unit::TestCase
23
23
  p1 = @person_class.new(176, 75)
24
24
  p2 = @person_class.new(176, 75)
25
25
  assert_equal(p1, p2)
26
+ assert p1.eql?(p2), "p1 should #eql? p2"
26
27
  end
27
28
 
28
29
  def test_inequality_of_people
@@ -46,4 +47,28 @@ class ValueObjectTest < Test::Unit::TestCase
46
47
  assert_equal('235 lbs', superman.weight)
47
48
  assert_equal('flies', superman.powers[0])
48
49
  end
50
+
51
+ def test_hashes_are_equal_if_people_are_equal
52
+ p1 = @person_class.new(150, 50)
53
+ p2 = @person_class.new(150, 50)
54
+ assert_equal(p1.hash, p2.hash)
55
+ end
56
+
57
+ def test_hashes_are_not_equal_if_people_are_not_equal
58
+ p1 = @person_class.new(150, 50)
59
+ p2 = @person_class.new(200, 50)
60
+ assert_not_equal(p1.hash, p2.hash)
61
+ end
62
+
63
+ def test_can_copy_value_objects_with_new_attributes
64
+ p1 = @person_class.new(175, 75)
65
+ p2 = @person_class.new(175, 80)
66
+ assert_equal(p2, p1.copy_with(:weight => 80))
67
+ end
68
+
69
+ def test_turning_value_objects_into_hashes
70
+ p = @person_class.new(175, 75)
71
+ p_hash = {:height => 175, :weight => 75}
72
+ assert_equal(p_hash, p.to_hash)
73
+ end
49
74
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: value_object
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.1.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: