wholable 0.0.0 → 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- checksums.yaml.gz.sig +0 -0
- data/README.adoc +21 -13
- data/lib/wholable/equatable.rb +1 -2
- data/lib/wholable/freezable.rb +1 -1
- data/wholable.gemspec +1 -1
- data.tar.gz.sig +0 -0
- metadata +3 -3
- metadata.gz.sig +0 -0
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 926e2edcbc46f6bc80713ffcabfbcfcc7e0863ecfca039bb43aae8d79aca7727
|
4
|
+
data.tar.gz: 1b3bee9c9eeaaf4f3e3addcde40f1e49912533cb50ca0f7348bc988050da75f3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 46e3d952fed95ffc62afc158c93c802e38445b1bea62c8ff80c0edc28c4b699435bd41081d48a047d93e84d746e3a094596f58055e0207bdfdf8cfaf9292452f
|
7
|
+
data.tar.gz: 9baf38439f8ad831639f29ce8722bcda20b941a14b78330917c3efd6b1a33c713001562cbf5cc3643025fb4a5305d4b3742fe9444b57fa34a779d65b9d546dca
|
checksums.yaml.gz.sig
CHANGED
Binary file
|
data/README.adoc
CHANGED
@@ -3,10 +3,16 @@
|
|
3
3
|
:figure-caption!:
|
4
4
|
|
5
5
|
:data_link: link:https://alchemists.io/articles/ruby_data[Data]
|
6
|
+
:ruby_link: link:https://www.ruby-lang.org[Ruby]
|
7
|
+
:structs_link: link:https://alchemists.io/articles/ruby_structs[Structs]
|
6
8
|
|
7
9
|
= Wholable
|
8
10
|
|
9
|
-
Wholable is a mixin that
|
11
|
+
Wholable is a mixin that allows you to turn your object into a _whole value object_ by ensuring object equality is determined by the values of the object instead of by identity. Whole value objects -- or value objects in general -- have the following traits as also noted via link:https://en.wikipedia.org/wiki/Value_object[Wikipedia]:
|
12
|
+
|
13
|
+
* Equality is determined by the values that make up an object and not by link:https://en.wikipedia.org/wiki/Identity_(object-oriented_programming)[identity] (i.e. memory address) which is the default behavior for all {ruby_link} objects except for {data_link} and {structs_link}.
|
14
|
+
* Identity remains unique since two objects can have the same values but different identity. This means `BasicObject#equal?` is never overwritten -- which is strongly discouraged -- as per link:https://rubyapi.org/o/basicobject#method-i-3D-3D[BasicObject] documentation.
|
15
|
+
* Value objects should be immutable (i.e. frozen) by default. This implementation enforces a strict adherence to immutability in order to ensure value objects remain equal and discourage mutation.
|
10
16
|
|
11
17
|
toc::[]
|
12
18
|
|
@@ -19,7 +25,7 @@ toc::[]
|
|
19
25
|
|
20
26
|
== Requirements
|
21
27
|
|
22
|
-
.
|
28
|
+
. {ruby_link}.
|
23
29
|
|
24
30
|
== Setup
|
25
31
|
|
@@ -72,6 +78,9 @@ jill = Person.new name: "Jill Smith", email: "jill@example.com"
|
|
72
78
|
jill_two = Person.new name: "Jill Smith", email: "jill@example.com"
|
73
79
|
jack = Person.new name: "Jack Smith", email: "jack@example.com"
|
74
80
|
|
81
|
+
jill.name # "Jill Smith"
|
82
|
+
jill.email # "jill@example.com"
|
83
|
+
|
75
84
|
jill.frozen? # true
|
76
85
|
jill_two.frozen? # true
|
77
86
|
jack.frozen? # true
|
@@ -80,9 +89,6 @@ jill.inspect # "#<Person @name=\"Jill Smith\", @email=\"jill@example.co
|
|
80
89
|
jill_two.inspect # "#<Person @name=\"Jill Smith\", @email=\"jill@example.com\">"
|
81
90
|
jack.inspect # "#<Person @name=\"Jack Smith\", @email=\"jack@example.com\">"
|
82
91
|
|
83
|
-
jill.name # "Jill Smith"
|
84
|
-
jill.email # "jill@example.com"
|
85
|
-
|
86
92
|
jill == jill # true
|
87
93
|
jill == jill_two # true
|
88
94
|
jill == jack # false
|
@@ -100,17 +106,18 @@ jill_two.hash # 3650965837788801745
|
|
100
106
|
jack.hash # 4460658980509842640
|
101
107
|
----
|
102
108
|
|
103
|
-
As you can see, object equality is determined by the object's values and
|
109
|
+
As you can see, object equality is determined by the object's values and _not_ by the object's identity. When you include `Wholable` along with a list of keys, the following happens:
|
104
110
|
|
105
|
-
. The corresponding _public_ `attr_reader` for each key
|
106
|
-
.
|
111
|
+
. The corresponding _public_ `attr_reader` for each key is created which saves you time and reduces double entry.
|
112
|
+
. Custom `#==`, `#eql?`, `#hash`, and `#inspect` methods are added to provide whole value behavior.
|
113
|
+
. The object is immediately frozen after initialization to ensure your instance is _immutable_ by default.
|
107
114
|
|
108
115
|
== Caveats
|
109
116
|
|
110
|
-
|
117
|
+
Whole values can be broken via the following:
|
111
118
|
|
112
|
-
* *Duplication*: Sending the `#
|
113
|
-
* *Post Attributes*: Adding additional attributes after what is defined when including `Wholable` will break your whole value object
|
119
|
+
* *Duplication*: Sending the `#dup` message will cause your whole value object to be unfrozen. This might be desired in certain situations but make sure to refreeze when able.
|
120
|
+
* *Post Attributes*: Adding additional attributes after what is defined when including `Wholable` will break your whole value object. To prevent this, let Wholable manage this for you (easiest). Otherwise (harder), you can manually override `#==`, `#eql?`, `#hash`, and `#inspect` behavior at which point you probably don't need Wholable anymore.
|
114
121
|
* *Deep Freezing*: The automatic freezing of your instances is shallow and will not deeply freeze nested attributes. This behavior mimics the behavior of {data_link} objects.
|
115
122
|
|
116
123
|
== Influences
|
@@ -118,8 +125,9 @@ The whole value contract created for you when using this gem can be broken by do
|
|
118
125
|
This implementation is based upon these original designs:
|
119
126
|
|
120
127
|
- link:https://github.com/dkubb/equalizer[Equalizer]: One of the first implementations that is over a decade old and no longer maintained.
|
121
|
-
- link:https://github.com/dry-rb/dry-equalizer[Dry Equalizer]: Deprecated and no longer maintained but was based upon the above implementation
|
122
|
-
- link:https://
|
128
|
+
- link:https://github.com/dry-rb/dry-equalizer[Dry Equalizer]: Deprecated and no longer maintained but was based upon the above implementation and has now moved into Dry Core.
|
129
|
+
- link:https://dry-rb.org/gems/dry-core[Dry Core]: Includes the link:https://dry-rb.org/gems/dry-core/equalizer[Dry::Core::Equalizer] module which is officially supported and actively maintained by the Dry RB team. A good alternative to this gem.
|
130
|
+
- link:https://github.com/piotrmurach/equatable/tree/master[Equatable]: A similar implementation to the above but is based off what you define via your `.attr_reader`. The project hasn't been maintained or updated in several years.
|
123
131
|
|
124
132
|
== Development
|
125
133
|
|
data/lib/wholable/equatable.rb
CHANGED
@@ -38,7 +38,6 @@ module Wholable
|
|
38
38
|
end
|
39
39
|
end
|
40
40
|
|
41
|
-
# :reek:TooManyStatements
|
42
41
|
def define_inspect
|
43
42
|
local_keys = keys
|
44
43
|
|
@@ -46,7 +45,7 @@ module Wholable
|
|
46
45
|
klass = self.class
|
47
46
|
name = klass.name || klass.inspect
|
48
47
|
|
49
|
-
local_keys.map { |key|
|
48
|
+
local_keys.map { |key| "@#{key}=#{public_send(key).inspect}" }
|
50
49
|
.join(", ")
|
51
50
|
.then { |pairs| "#<#{name} #{pairs}>" }
|
52
51
|
end
|
data/lib/wholable/freezable.rb
CHANGED
data/wholable.gemspec
CHANGED
data.tar.gz.sig
CHANGED
Binary file
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: wholable
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Brooke Kuhlmann
|
@@ -35,7 +35,7 @@ cert_chain:
|
|
35
35
|
3n5C8/6Zh9DYTkpcwPSuIfAga6wf4nXc9m6JAw8AuMLaiWN/r/2s4zJsUHYERJEu
|
36
36
|
gZGm4JqtuSg8pYjPeIJxS960owq+SfuC+jxqmRA54BisFCv/0VOJi7tiJVY=
|
37
37
|
-----END CERTIFICATE-----
|
38
|
-
date: 2023-
|
38
|
+
date: 2023-08-06 00:00:00.000000000 Z
|
39
39
|
dependencies: []
|
40
40
|
description:
|
41
41
|
email:
|
@@ -79,7 +79,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
79
79
|
- !ruby/object:Gem::Version
|
80
80
|
version: '0'
|
81
81
|
requirements: []
|
82
|
-
rubygems_version: 3.4.
|
82
|
+
rubygems_version: 3.4.18
|
83
83
|
signing_key:
|
84
84
|
specification_version: 4
|
85
85
|
summary: A whole value object mixin.
|
metadata.gz.sig
CHANGED
Binary file
|