value_object 0.1.1 → 0.1.2
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.
- checksums.yaml +7 -0
- data/LICENSE +2 -2
- data/README.md +50 -59
- data/Rakefile +1 -0
- data/lib/value_object.rb +4 -8
- data/lib/value_object/version.rb +1 -1
- data/value_object.gemspec +2 -1
- metadata +15 -22
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA1:
|
|
3
|
+
metadata.gz: e81fef4675c5209e747e4b0cabff9b7527247517
|
|
4
|
+
data.tar.gz: cd5bd4d1af224d1df659e5523791db0fdbd2b95f
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: aba48d07c45433e9051bb64fa05103db629f119607277cd35927f5da4a60497e82c428509090200e0f9d1c9a7a0f006ea73923ce0be29c489d2e177821c008e5
|
|
7
|
+
data.tar.gz: f4511e42bae716e17d0dae8d5107d659ca5e073d87e37b8e49510ddbe4d05b016d13ebd8d17bc1b02512687a35b7aa59025fa631d5222bd322b0ce619796c611
|
data/LICENSE
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
Copyright (c) 2012 Harry Garrood
|
|
1
|
+
Copyright (c) 2012-2014 Harry Garrood
|
|
2
2
|
|
|
3
3
|
MIT License
|
|
4
4
|
|
|
@@ -19,4 +19,4 @@ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
|
19
19
|
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
|
20
20
|
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
21
21
|
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|
22
|
-
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
CHANGED
|
@@ -1,84 +1,75 @@
|
|
|
1
1
|
# ValueObject
|
|
2
|
+
A very small library to help you define and create immutable value
|
|
3
|
+
objects. Mainly to reduce the amount of code I needed for ActiveRecord
|
|
4
|
+
`composed_of` classes.
|
|
2
5
|
|
|
3
|
-
|
|
6
|
+
## overview
|
|
4
7
|
|
|
5
|
-
|
|
8
|
+
Start off by creating a class which derives from `ValueObject::Base`.
|
|
9
|
+
Call `has_fields` to define a list of fields for the class.
|
|
6
10
|
|
|
7
|
-
|
|
8
|
-
|
|
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.
|
|
14
|
-
|
|
15
|
-
## How to use
|
|
16
|
-
|
|
17
|
-
Require it
|
|
18
|
-
|
|
19
|
-
require 'value_object'
|
|
20
|
-
|
|
21
|
-
Subclass it
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
class Person < ValueObject::Base
|
|
25
|
-
has_fields :height, :weight
|
|
11
|
+
class Route < ValueObject::Base
|
|
12
|
+
has_fields :from, :to
|
|
26
13
|
end
|
|
27
14
|
|
|
28
|
-
|
|
15
|
+
You can now create instances of this class with positional arguments:
|
|
16
|
+
|
|
17
|
+
m1_motorway = Route.new("London", "Leeds")
|
|
18
|
+
channel_tunnel = Route.new("Calais", "Dover")
|
|
29
19
|
|
|
30
|
-
|
|
31
|
-
dick = Person.new(160, 60)
|
|
20
|
+
Or with hashes:
|
|
32
21
|
|
|
33
|
-
|
|
22
|
+
route_66 = Route.new(:from => "Santa Monica", :to => "Chicago")
|
|
34
23
|
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
Read their attributes
|
|
24
|
+
You can read their attributes, but you can't write them.
|
|
38
25
|
|
|
39
|
-
|
|
40
|
-
|
|
26
|
+
m1_motorway.from # => "London"
|
|
27
|
+
channel_tunnel.to # => "Dover"
|
|
28
|
+
route_66.from = "IL" # => NoMethodError
|
|
41
29
|
|
|
42
|
-
|
|
30
|
+
You can test whether they are equal with `==` or `eql?`, which are
|
|
31
|
+
aliases.
|
|
43
32
|
|
|
44
|
-
|
|
45
|
-
|
|
33
|
+
m1_motorway == channel_tunnel # => false
|
|
34
|
+
m1_motorway.eql?(Route.new("London", "Leeds)) # => true
|
|
46
35
|
|
|
47
|
-
|
|
36
|
+
You can also create instances where all values are nil. Objects like this will
|
|
37
|
+
return `true` when sent the message `empty?` (and will)
|
|
48
38
|
|
|
49
|
-
|
|
39
|
+
m1_motorway.empty? # => false
|
|
40
|
+
Route.new(nil, nil).empty? # => true
|
|
50
41
|
|
|
51
|
-
You can even subclass them again!
|
|
42
|
+
You can even subclass them again to add more fields!
|
|
52
43
|
|
|
53
|
-
class
|
|
54
|
-
has_fields :
|
|
44
|
+
class Railway < Route
|
|
45
|
+
has_fields :serviced_by
|
|
55
46
|
end
|
|
56
47
|
|
|
57
|
-
|
|
48
|
+
a_railway = Railway.new("Aberdeen", "Inverness", "ScotRail")
|
|
58
49
|
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
50
|
+
a_railway.height # => '6 foot 3'
|
|
51
|
+
a_railway.weight # => '235 lbs'
|
|
52
|
+
a_railway.power # => 'flies'
|
|
62
53
|
|
|
63
|
-
|
|
54
|
+
Create a hash from a value object by sending `to_hash`.
|
|
64
55
|
|
|
65
|
-
|
|
66
|
-
# => {
|
|
67
|
-
|
|
68
|
-
|
|
56
|
+
a_railway.to_hash
|
|
57
|
+
# => {
|
|
58
|
+
:from => "Aberdeen",
|
|
59
|
+
:to => "Inverness",
|
|
60
|
+
:serviced_by => "ScotRail"
|
|
61
|
+
}
|
|
69
62
|
|
|
70
|
-
|
|
63
|
+
`copy_with` allows you to make a copy of a value object while updating
|
|
64
|
+
certain attributes.
|
|
71
65
|
|
|
72
|
-
|
|
73
|
-
# => #<
|
|
74
|
-
@
|
|
75
|
-
@
|
|
76
|
-
@
|
|
77
|
-
>
|
|
66
|
+
a_railway.copy_with(:serviced_by => "Virgin")
|
|
67
|
+
# => #<Railway:0x3914490
|
|
68
|
+
@from="Aberdeen",
|
|
69
|
+
@to="Inverness",
|
|
70
|
+
@serviced_by="Virgin">
|
|
78
71
|
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
objects, and o1.eql?(o2), then o1.hash == o2.hash
|
|
72
|
+
Value objects also define `hash`, which depends on the values of the
|
|
73
|
+
fields as well as the class of which they are an instance.
|
|
82
74
|
|
|
83
|
-
|
|
84
|
-
# => 1160641176
|
|
75
|
+
a_railway.hash # => 1160641176
|
data/Rakefile
CHANGED
data/lib/value_object.rb
CHANGED
|
@@ -8,15 +8,11 @@ module ValueObject
|
|
|
8
8
|
|
|
9
9
|
class << self
|
|
10
10
|
def has_fields(*args)
|
|
11
|
-
attr_reader
|
|
11
|
+
attr_reader(*args)
|
|
12
12
|
self.fields += args
|
|
13
13
|
end
|
|
14
14
|
end
|
|
15
15
|
|
|
16
|
-
def fields
|
|
17
|
-
self.class.fields
|
|
18
|
-
end
|
|
19
|
-
|
|
20
16
|
def initialize(*args)
|
|
21
17
|
if args.length == 1 && args[0].is_a?(Hash)
|
|
22
18
|
fields.each { |f| instance_variable_set "@#{f}", args[0][f] }
|
|
@@ -27,7 +23,7 @@ module ValueObject
|
|
|
27
23
|
|
|
28
24
|
def eql?(other)
|
|
29
25
|
return false if self.class != other.class
|
|
30
|
-
fields.all? { |f|
|
|
26
|
+
fields.all? { |f| public_send(f) == other.public_send(f) }
|
|
31
27
|
end
|
|
32
28
|
|
|
33
29
|
def ==(other)
|
|
@@ -35,12 +31,12 @@ module ValueObject
|
|
|
35
31
|
end
|
|
36
32
|
|
|
37
33
|
def empty?
|
|
38
|
-
fields.all? { |f|
|
|
34
|
+
fields.all? { |f| public_send(f).nil? }
|
|
39
35
|
end
|
|
40
36
|
|
|
41
37
|
def to_hash
|
|
42
38
|
hash = {}
|
|
43
|
-
fields.each { |f| hash[f] =
|
|
39
|
+
fields.each { |f| hash[f] = public_send(f) }
|
|
44
40
|
hash
|
|
45
41
|
end
|
|
46
42
|
|
data/lib/value_object/version.rb
CHANGED
data/value_object.gemspec
CHANGED
|
@@ -5,7 +5,7 @@ Gem::Specification.new do |gem|
|
|
|
5
5
|
gem.authors = ["Harry Garrood"]
|
|
6
6
|
gem.email = ["hdgarrood@gmail.com"]
|
|
7
7
|
gem.description = %q{A tiny gem for value objects.}
|
|
8
|
-
gem.summary = %q{A gem which
|
|
8
|
+
gem.summary = %q{A tiny gem which makes it dead simple to create immutable value objects.}
|
|
9
9
|
gem.homepage = "https://github.com/hdgarrood/value_object"
|
|
10
10
|
|
|
11
11
|
gem.files = `git ls-files`.split($\)
|
|
@@ -14,6 +14,7 @@ Gem::Specification.new do |gem|
|
|
|
14
14
|
gem.name = "value_object"
|
|
15
15
|
gem.require_paths = ["lib"]
|
|
16
16
|
gem.version = ValueObject::VERSION
|
|
17
|
+
gem.license = "MIT"
|
|
17
18
|
|
|
18
19
|
gem.add_dependency 'activesupport'
|
|
19
20
|
gem.add_development_dependency 'simplecov'
|
metadata
CHANGED
|
@@ -1,46 +1,41 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: value_object
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.1.
|
|
5
|
-
prerelease:
|
|
4
|
+
version: 0.1.2
|
|
6
5
|
platform: ruby
|
|
7
6
|
authors:
|
|
8
7
|
- Harry Garrood
|
|
9
8
|
autorequire:
|
|
10
9
|
bindir: bin
|
|
11
10
|
cert_chain: []
|
|
12
|
-
date:
|
|
11
|
+
date: 2014-02-02 00:00:00.000000000 Z
|
|
13
12
|
dependencies:
|
|
14
13
|
- !ruby/object:Gem::Dependency
|
|
15
14
|
name: activesupport
|
|
16
15
|
requirement: !ruby/object:Gem::Requirement
|
|
17
|
-
none: false
|
|
18
16
|
requirements:
|
|
19
|
-
- -
|
|
17
|
+
- - ">="
|
|
20
18
|
- !ruby/object:Gem::Version
|
|
21
19
|
version: '0'
|
|
22
20
|
type: :runtime
|
|
23
21
|
prerelease: false
|
|
24
22
|
version_requirements: !ruby/object:Gem::Requirement
|
|
25
|
-
none: false
|
|
26
23
|
requirements:
|
|
27
|
-
- -
|
|
24
|
+
- - ">="
|
|
28
25
|
- !ruby/object:Gem::Version
|
|
29
26
|
version: '0'
|
|
30
27
|
- !ruby/object:Gem::Dependency
|
|
31
28
|
name: simplecov
|
|
32
29
|
requirement: !ruby/object:Gem::Requirement
|
|
33
|
-
none: false
|
|
34
30
|
requirements:
|
|
35
|
-
- -
|
|
31
|
+
- - ">="
|
|
36
32
|
- !ruby/object:Gem::Version
|
|
37
33
|
version: '0'
|
|
38
34
|
type: :development
|
|
39
35
|
prerelease: false
|
|
40
36
|
version_requirements: !ruby/object:Gem::Requirement
|
|
41
|
-
none: false
|
|
42
37
|
requirements:
|
|
43
|
-
- -
|
|
38
|
+
- - ">="
|
|
44
39
|
- !ruby/object:Gem::Version
|
|
45
40
|
version: '0'
|
|
46
41
|
description: A tiny gem for value objects.
|
|
@@ -50,7 +45,7 @@ executables: []
|
|
|
50
45
|
extensions: []
|
|
51
46
|
extra_rdoc_files: []
|
|
52
47
|
files:
|
|
53
|
-
- .gitignore
|
|
48
|
+
- ".gitignore"
|
|
54
49
|
- Gemfile
|
|
55
50
|
- LICENSE
|
|
56
51
|
- README.md
|
|
@@ -60,30 +55,28 @@ files:
|
|
|
60
55
|
- test/tc_value_object.rb
|
|
61
56
|
- value_object.gemspec
|
|
62
57
|
homepage: https://github.com/hdgarrood/value_object
|
|
63
|
-
licenses:
|
|
58
|
+
licenses:
|
|
59
|
+
- MIT
|
|
60
|
+
metadata: {}
|
|
64
61
|
post_install_message:
|
|
65
62
|
rdoc_options: []
|
|
66
63
|
require_paths:
|
|
67
64
|
- lib
|
|
68
65
|
required_ruby_version: !ruby/object:Gem::Requirement
|
|
69
|
-
none: false
|
|
70
66
|
requirements:
|
|
71
|
-
- -
|
|
67
|
+
- - ">="
|
|
72
68
|
- !ruby/object:Gem::Version
|
|
73
69
|
version: '0'
|
|
74
70
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
75
|
-
none: false
|
|
76
71
|
requirements:
|
|
77
|
-
- -
|
|
72
|
+
- - ">="
|
|
78
73
|
- !ruby/object:Gem::Version
|
|
79
74
|
version: '0'
|
|
80
75
|
requirements: []
|
|
81
76
|
rubyforge_project:
|
|
82
|
-
rubygems_version:
|
|
77
|
+
rubygems_version: 2.2.1
|
|
83
78
|
signing_key:
|
|
84
|
-
specification_version:
|
|
85
|
-
summary: A gem which
|
|
86
|
-
value objects.
|
|
79
|
+
specification_version: 4
|
|
80
|
+
summary: A tiny gem which makes it dead simple to create immutable value objects.
|
|
87
81
|
test_files:
|
|
88
82
|
- test/tc_value_object.rb
|
|
89
|
-
has_rdoc:
|