wholable 0.0.0 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 42f00bcfb8063a21a4fb8f6427424078be9ce45365d2b41f5a6c053133b059d7
4
- data.tar.gz: 267014e8011488d2b9e3db0fcdecebe6a70c0032e8f47ea60cb0d3b035174719
3
+ metadata.gz: bdd21036f1cae41b84f1db0c85aa8806770de6317ffff2e5928faa938871e9d5
4
+ data.tar.gz: 258dd2e251c8dd9d385d4331c20ab6395c8ccb93c5ee47cd079849df80bf2cd2
5
5
  SHA512:
6
- metadata.gz: 94334fe408aa2e6ac16589688f1c3e3a4d746f738ef9ec4c75d2031e10898463ce63954e0c8f147167396a0a18da79d312a75a5a3ce52bc663a773be824841d9
7
- data.tar.gz: b921837f4c80fb2651cf6a135c367c0fd2bbf18a5dfa47c0ad52838ad6f6482b90492e18d312093660f2f4ee693f56c49768647b50c8f7e0b5c93d0a9d99c9d7
6
+ metadata.gz: add335b9cdfc0ff14334b77aff7ee3295419fcf0fe4b78470925f6f395a85ded7cd47496213e9f36695e60833b4cf04692e9c030c02869d2fbff42352cabaa36
7
+ data.tar.gz: e2a9f041d020e166111905e76396887e9aae3f6f1f7475790a5dcb32678eb038c171a7e790ba1b196c0d9604463e6072e5975151980c3e67d61e930959a0a60f
checksums.yaml.gz.sig CHANGED
Binary file
data/README.adoc CHANGED
@@ -3,23 +3,33 @@
3
3
  :figure-caption!:
4
4
 
5
5
  :data_link: link:https://alchemists.io/articles/ruby_data[Data]
6
+ :pattern_matching_link: link:https://alchemists.io/articles/ruby_pattern_matching[pattern matching]
7
+ :ruby_link: link:https://www.ruby-lang.org[Ruby]
8
+ :data_link: link:https://alchemists.io/articles/ruby_data[Data]
9
+ :structs_link: link:https://alchemists.io/articles/ruby_structs[Structs]
6
10
 
7
11
  = Wholable
8
12
 
9
- Wholable is a mixin that turns a class into a _whole value object_ by ensuring object equality is determined by the values that make up the object instead of by identity within memory.
13
+ 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]:
14
+
15
+ * 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}.
16
+ * 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.
17
+ * 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
18
 
11
19
  toc::[]
12
20
 
13
21
  == Features
14
22
 
15
23
  * Ensures equality (i.e. `#==` and `#eql?`) is determined by attribute values and not object identity (i.e. `#equal?`).
24
+ * Allows you to compare two objects of same or different types and see their differences.
25
+ * Provides {pattern_matching_link}.
16
26
  * Automatically defines public attribute readers (i.e. `.attr_reader`) based on provided keys.
17
27
  * Ensures object inspection (i.e. `#inspect`) shows all registered attributes.
18
28
  * Ensures object is frozen upon initialization.
19
29
 
20
30
  == Requirements
21
31
 
22
- . link:https://www.ruby-lang.org[Ruby].
32
+ . {ruby_link}.
23
33
 
24
34
  == Setup
25
35
 
@@ -72,45 +82,64 @@ jill = Person.new name: "Jill Smith", email: "jill@example.com"
72
82
  jill_two = Person.new name: "Jill Smith", email: "jill@example.com"
73
83
  jack = Person.new name: "Jack Smith", email: "jack@example.com"
74
84
 
75
- jill.frozen? # true
76
- jill_two.frozen? # true
77
- jack.frozen? # true
85
+ jill.name # "Jill Smith"
86
+ jill.email # "jill@example.com"
87
+
88
+ jill.frozen? # true
89
+ jill_two.frozen? # true
90
+ jack.frozen? # true
91
+
92
+ jill.inspect # "#<Person @name=\"Jill Smith\", @email=\"jill@example.com\">"
93
+ jill_two.inspect # "#<Person @name=\"Jill Smith\", @email=\"jill@example.com\">"
94
+ jack.inspect # "#<Person @name=\"Jack Smith\", @email=\"jack@example.com\">"
95
+
96
+ jill == jill # true
97
+ jill == jill_two # true
98
+ jill == jack # false
99
+
100
+ jill.diff(jill) # {}
101
+ jill.diff(jack) # {
102
+ # name: ["Jill Smith", "Jack Smith"],
103
+ # email: ["jill@example.com", "jack@example.com"]
104
+ # }
105
+ jill.diff(Object.new) # {:name=>["Jill Smith", nil], :email=>["jill@example.com", nil]}
78
106
 
79
- jill.inspect # "#<Person @name=\"Jill Smith\", @email=\"jill@example.com\">"
80
- jill_two.inspect # "#<Person @name=\"Jill Smith\", @email=\"jill@example.com\">"
81
- jack.inspect # "#<Person @name=\"Jack Smith\", @email=\"jack@example.com\">"
107
+ jill.eql? jill # true
108
+ jill.eql? jill_two # true
109
+ jill.eql? jack # false
82
110
 
83
- jill.name # "Jill Smith"
84
- jill.email # "jill@example.com"
111
+ jill.equal? jill # true
112
+ jill.equal? jill_two # false
113
+ jill.equal? jack # false
85
114
 
86
- jill == jill # true
87
- jill == jill_two # true
88
- jill == jack # false
115
+ jill.hash # 3650965837788801745
116
+ jill_two.hash # 3650965837788801745
117
+ jack.hash # 4460658980509842640
89
118
 
90
- jill.eql? jill # true
91
- jill.eql? jill_two # true
92
- jill.eql? jack # false
119
+ jill.to_a # ["Jill Smith", "jill@example.com"]
120
+ jack.to_a # ["Jack Smith", "jack@example.com"]
93
121
 
94
- jill.equal? jill # true
95
- jill.equal? jill_two # false
96
- jill.equal? jack # false
122
+ jill.to_h # {:name=>"Jill Smith", :email=>"jill@example.com"}
123
+ jack.to_h # {:name=>"Jack Smith", :email=>"jack@example.com"}
97
124
 
98
- jill.hash # 3650965837788801745
99
- jill_two.hash # 3650965837788801745
100
- jack.hash # 4460658980509842640
125
+ jill.with name: "Sue" # #<Person @name="Sue", @email="jill@example.com">
126
+ jill.with bad: "!" # unknown keyword: :bad (ArgumentError)
101
127
  ----
102
128
 
103
- 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:
129
+ 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
130
 
105
- . The corresponding _public_ `attr_reader` for each key will be created which saves you time and reduces double entry.
106
- . The object will be immediately frozen after initialization to ensure your instance is _immutable_ by default.
131
+ . The corresponding _public_ `attr_reader` for each key is created which saves you time and reduces double entry when implementing your whole value object.
132
+ . The `#to_a` and `#to_h` methods are added for convenience in order to play nice with {data_link} and {structs_link}.
133
+ . The `#deconstruct` and `#deconstruct_keys` aliases are created for you so you can leverage {pattern_matching_link}.
134
+ . Custom `#==`, `#eql?`, `#hash`, `#inspect`, `#to_a`, `#to_h`, and `#with` methods are added to provide whole value behavior.
135
+ . The object is immediately frozen after initialization to ensure your instance is _immutable_ by default.
107
136
 
108
137
  == Caveats
109
138
 
110
- The whole value contract created for you when using this gem can be broken by doing the following:
139
+ Whole values can be broken via the following:
111
140
 
112
- * *Duplication*: Sending the `#dub` message will cause your whole value object to be unfrozen. This might be desired in certain situations but make sure to refreeze when able.
113
- * *Post Attributes*: Adding additional attributes after what is defined when including `Wholable` will break your whole value object contract so ensure you let Wholable manage this for you.
141
+ * *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.
142
+ * *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`, `#inspect`, `#to_a`, and `#to_h` behavior at which point you don't need Wholable anymore.
114
143
  * *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
144
 
116
145
  == Influences
@@ -118,8 +147,9 @@ The whole value contract created for you when using this gem can be broken by do
118
147
  This implementation is based upon these original designs:
119
148
 
120
149
  - 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. A version of this has moved into link:https://dry-rb.org/gems/dry-core[Dry Core] but doesn't appear to work properly or be well supported. Even the documentation is not fully linked on the main page.
122
- - 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 now.
150
+ - 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.
151
+ - 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.
152
+ - 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
153
 
124
154
  == Development
125
155
 
@@ -6,5 +6,14 @@ module Wholable
6
6
  def eql?(other) = instance_of?(other.class) && hash == other.hash
7
7
 
8
8
  def ==(other) = other.is_a?(self.class) && hash == other.hash
9
+
10
+ def diff other
11
+ if other.is_a? self.class
12
+ to_h.merge(other.to_h) { |_, one, two| [one, two].uniq }
13
+ .select { |_, diff| diff.size == 2 }
14
+ else
15
+ to_h.each.with_object({}) { |(key, value), diff| diff[key] = [value, nil] }
16
+ end
17
+ end
9
18
  end
10
19
  end
@@ -6,14 +6,13 @@ module Wholable
6
6
  def initialize *keys
7
7
  super()
8
8
  @keys = keys.uniq
9
- define_hash
10
- define_inspect
9
+ define_instance_methods
11
10
  freeze
12
11
  end
13
12
 
14
13
  def included descendant
15
14
  super
16
- define_readers descendant
15
+ define_class_methods descendant
17
16
  descendant.include Comparable
18
17
  descendant.prepend Freezable
19
18
  end
@@ -22,12 +21,42 @@ module Wholable
22
21
 
23
22
  attr_reader :keys
24
23
 
24
+ def define_instance_methods
25
+ define_hash
26
+ define_inspect
27
+ define_with
28
+ define_to_a
29
+ define_to_h
30
+ end
31
+
32
+ def define_class_methods descendant
33
+ define_readers descendant
34
+ define_deconstruct descendant
35
+ define_deconstruct_keys descendant
36
+ end
37
+
25
38
  def define_readers descendant
26
39
  descendant.class_eval <<-READERS, __FILE__, __LINE__ + 1
27
40
  attr_reader #{keys.map(&:inspect).join ", "}
28
41
  READERS
29
42
  end
30
43
 
44
+ def define_deconstruct descendant
45
+ descendant.class_eval <<-READERS, __FILE__, __LINE__ + 1
46
+ alias deconstruct to_a
47
+ READERS
48
+ end
49
+
50
+ def define_deconstruct_keys descendant
51
+ descendant.class_eval <<-READERS, __FILE__, __LINE__ + 1
52
+ alias deconstruct_keys to_h
53
+ READERS
54
+ end
55
+
56
+ def define_with
57
+ define_method(:with) { |**attributes| self.class.new(**to_h.merge!(attributes)) }
58
+ end
59
+
31
60
  def define_hash
32
61
  local_keys = keys
33
62
 
@@ -38,7 +67,6 @@ module Wholable
38
67
  end
39
68
  end
40
69
 
41
- # :reek:TooManyStatements
42
70
  def define_inspect
43
71
  local_keys = keys
44
72
 
@@ -46,10 +74,26 @@ module Wholable
46
74
  klass = self.class
47
75
  name = klass.name || klass.inspect
48
76
 
49
- local_keys.map { |key| %(@#{key}=#{public_send(key).inspect}) }
77
+ local_keys.map { |key| "@#{key}=#{public_send(key).inspect}" }
50
78
  .join(", ")
51
79
  .then { |pairs| "#<#{name} #{pairs}>" }
52
80
  end
53
81
  end
82
+
83
+ def define_to_a
84
+ local_keys = keys
85
+
86
+ define_method :to_a do
87
+ local_keys.reduce([]) { |array, key| array.append public_send(key) }
88
+ end
89
+ end
90
+
91
+ def define_to_h
92
+ local_keys = keys
93
+
94
+ define_method :to_h do
95
+ local_keys.each.with_object({}) { |key, dictionary| dictionary[key] = public_send key }
96
+ end
97
+ end
54
98
  end
55
99
  end
@@ -4,7 +4,7 @@ module Wholable
4
4
  # Ensures an object is frozen after being initialized.
5
5
  module Freezable
6
6
  def initialize(...)
7
- super(...)
7
+ super
8
8
  freeze
9
9
  end
10
10
  end
data/wholable.gemspec CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |spec|
4
4
  spec.name = "wholable"
5
- spec.version = "0.0.0"
5
+ spec.version = "0.1.0"
6
6
  spec.authors = ["Brooke Kuhlmann"]
7
7
  spec.email = ["brooke@alchemists.io"]
8
8
  spec.homepage = "https://alchemists.io/projects/wholable"
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.0
4
+ version: 0.1.0
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-07-27 00:00:00.000000000 Z
38
+ date: 2023-08-15 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.17
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