value_object 0.0.1 → 0.0.3
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 +48 -16
- data/Rakefile +25 -0
- data/lib/value_object/version.rb +1 -1
- data/test/tc_value_object.rb +49 -0
- data/value_object.gemspec +1 -0
- metadata +22 -4
data/README.md
CHANGED
@@ -1,29 +1,61 @@
|
|
1
1
|
# ValueObject
|
2
2
|
|
3
|
-
|
3
|
+
A very small library to help you define and create immutable value objects.
|
4
4
|
|
5
|
-
##
|
5
|
+
## Why?
|
6
6
|
|
7
|
-
|
7
|
+
Mainly to reduce the amount of code I needed for ActiveRecord `composed_of` classes.
|
8
|
+
ValueObjects are designed with `composed_of` in mind; they will:
|
8
9
|
|
9
|
-
|
10
|
+
* happily accept `nil` for their fields, unlike [Values](https://github.com/tcrayford/Values).
|
11
|
+
* allow multiple levels of subclassing -- if you want a value object
|
12
|
+
which is like some other class of value object (ie, it has all of the other object's fields
|
13
|
+
and methods) but will some additional field or method, you can simply subclass it.
|
10
14
|
|
11
|
-
|
15
|
+
## How to use
|
12
16
|
|
13
|
-
|
17
|
+
Require it
|
14
18
|
|
15
|
-
|
19
|
+
require 'value_object'
|
16
20
|
|
17
|
-
|
21
|
+
Subclass it
|
18
22
|
|
19
|
-
## Usage
|
20
23
|
|
21
|
-
|
24
|
+
class Person < ValueObject::Base
|
25
|
+
has_fields :height, :weight
|
26
|
+
end
|
22
27
|
|
23
|
-
|
28
|
+
Create a value object with positional arguments
|
24
29
|
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
+
tom = Person.new(176, 75)
|
31
|
+
dick = Person.new(160, 60)
|
32
|
+
|
33
|
+
Create a value object with a hash
|
34
|
+
|
35
|
+
harry = Person.new(:height => 176, :weight => 75)
|
36
|
+
|
37
|
+
Read their attributes
|
38
|
+
|
39
|
+
tom.height # => 176
|
40
|
+
dick.weight # => 60
|
41
|
+
|
42
|
+
Test whether value objects are equal
|
43
|
+
|
44
|
+
tom == dick # => false
|
45
|
+
tom == harry # => true
|
46
|
+
|
47
|
+
Test for emptiness
|
48
|
+
|
49
|
+
Person.new(nil, nil).empty? # => true
|
50
|
+
|
51
|
+
You can even subclass them again!
|
52
|
+
|
53
|
+
class Superhero < Person
|
54
|
+
has_fields :power
|
55
|
+
end
|
56
|
+
|
57
|
+
superman = Superhero.new('6 foot 3', '235 lbs', 'flies')
|
58
|
+
|
59
|
+
superman.height # => '6 foot 3'
|
60
|
+
superman.weight # => '235 lbs'
|
61
|
+
superman.power # => 'flies'
|
data/Rakefile
CHANGED
@@ -1,2 +1,27 @@
|
|
1
1
|
#!/usr/bin/env rake
|
2
2
|
require "bundler/gem_tasks"
|
3
|
+
require 'rake/testtask'
|
4
|
+
|
5
|
+
def test_pattern
|
6
|
+
'test/tc_*.rb'
|
7
|
+
end
|
8
|
+
|
9
|
+
desc "Run tests"
|
10
|
+
Rake::TestTask.new do |t|
|
11
|
+
t.libs << 'test'
|
12
|
+
t.pattern = test_pattern
|
13
|
+
end
|
14
|
+
|
15
|
+
desc "Run tests with SimpleCov coverage"
|
16
|
+
task :coverage do
|
17
|
+
require 'simplecov'
|
18
|
+
SimpleCov.start do
|
19
|
+
add_filter '/test/'
|
20
|
+
end
|
21
|
+
require 'test/unit'
|
22
|
+
Dir.glob(test_pattern).each do |file|
|
23
|
+
require_relative file
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
task :default => [:test]
|
data/lib/value_object/version.rb
CHANGED
@@ -0,0 +1,49 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
require 'value_object'
|
3
|
+
|
4
|
+
class ValueObjectTest < Test::Unit::TestCase
|
5
|
+
def setup
|
6
|
+
@person_class = Class.new(ValueObject::Base)
|
7
|
+
@person_class.has_fields :height, :weight
|
8
|
+
end
|
9
|
+
|
10
|
+
def test_creates_people_properly_with_positional_arguments
|
11
|
+
p = @person_class.new(176, 75)
|
12
|
+
assert_equal(176, p.height)
|
13
|
+
assert_equal(75, p.weight)
|
14
|
+
end
|
15
|
+
|
16
|
+
def test_creates_people_properly_with_hashes
|
17
|
+
p = @person_class.new(:height => 176, :weight => 75)
|
18
|
+
assert_equal(176, p.height)
|
19
|
+
assert_equal(75, p.weight)
|
20
|
+
end
|
21
|
+
|
22
|
+
def test_equality_of_people
|
23
|
+
p1 = @person_class.new(176, 75)
|
24
|
+
p2 = @person_class.new(176, 75)
|
25
|
+
assert_equal(p1, p2)
|
26
|
+
end
|
27
|
+
|
28
|
+
def test_inequality_of_people
|
29
|
+
p1 = @person_class.new(180, 75)
|
30
|
+
p2 = @person_class.new(176, 89)
|
31
|
+
assert_not_equal(p1, p2)
|
32
|
+
end
|
33
|
+
|
34
|
+
def test_emptiness_of_people
|
35
|
+
assert_equal(true, @person_class.new(nil, nil).empty?)
|
36
|
+
end
|
37
|
+
|
38
|
+
def test_can_subclass_subclasses
|
39
|
+
superhero_class = Class.new(@person_class)
|
40
|
+
superhero_class.has_fields :powers
|
41
|
+
|
42
|
+
superman = superhero_class.new('6 foot 3',
|
43
|
+
'235 lbs',
|
44
|
+
['flies', 'laser eyes', 'really strong'])
|
45
|
+
assert_equal('6 foot 3', superman.height)
|
46
|
+
assert_equal('235 lbs', superman.weight)
|
47
|
+
assert_equal('flies', superman.powers[0])
|
48
|
+
end
|
49
|
+
end
|
data/value_object.gemspec
CHANGED
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.
|
4
|
+
version: 0.0.3
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-10-
|
12
|
+
date: 2012-10-02 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activesupport
|
@@ -27,6 +27,22 @@ dependencies:
|
|
27
27
|
- - ! '>='
|
28
28
|
- !ruby/object:Gem::Version
|
29
29
|
version: '0'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: simplecov
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
30
46
|
description: A tiny gem for value objects.
|
31
47
|
email:
|
32
48
|
- hdgarrood@gmail.com
|
@@ -41,6 +57,7 @@ files:
|
|
41
57
|
- Rakefile
|
42
58
|
- lib/value_object.rb
|
43
59
|
- lib/value_object/version.rb
|
60
|
+
- test/tc_value_object.rb
|
44
61
|
- value_object.gemspec
|
45
62
|
homepage: https://github.com/hdgarrood/value_object
|
46
63
|
licenses: []
|
@@ -62,10 +79,11 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
62
79
|
version: '0'
|
63
80
|
requirements: []
|
64
81
|
rubyforge_project:
|
65
|
-
rubygems_version: 1.8.
|
82
|
+
rubygems_version: 1.8.23
|
66
83
|
signing_key:
|
67
84
|
specification_version: 3
|
68
85
|
summary: A gem which gives you a class which you can subclass to easily create immutable
|
69
86
|
value objects.
|
70
|
-
test_files:
|
87
|
+
test_files:
|
88
|
+
- test/tc_value_object.rb
|
71
89
|
has_rdoc:
|