lab42_data_class 0.1.2 → 0.2.0

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 04c191005baa65d6d21f1bea2bfbf8361651ab06e9c81e45cc2ab0ac7d8531e8
4
- data.tar.gz: d66a6a791c4102e5d9347441a713e893a74e1d49efd39ee145db890ea5d9db07
3
+ metadata.gz: e882ef6623d30a0203797a53f31e4d3607e3d3e883adbb20ac63ee517db6c71e
4
+ data.tar.gz: 3ce45a762f3b3628ea6ee19392b1a2202f3da5f55b8f4b9d341b25f4236581aa
5
5
  SHA512:
6
- metadata.gz: 8ee5d308afdb75b72840846a7c968d95693204f9db441e8c20825c239c3a9895f0ebfb0cf49e8c2fef5c5e31b79c858cee837c91281a7b48b888451dc6f49e50
7
- data.tar.gz: 3f170816817a20cd00827d87bf4257cfa7a8e8c748f0a46be64ca30ea3d4533c1d9af9a3192676abf3ea9119b8f6c5a7ef69144c89ee1a1dd71bd165bc0d8af1
6
+ metadata.gz: a50fabe74deb22f46e0bc33c062547514fba922e138a3af2bbd6faeabe69f137a98ef5925166e04367366474bcfddf59d4c4067e81f0ebfc2ef61f86312702b0
7
+ data.tar.gz: 61a6d7e6d7b4fca517622d49516cda9406daab3265b076eff18a0e65fa9ad60a188b295d302b64918600deef5c68d3a2684a6344e4bc2e96ff61dff9aa89d998
data/README.md CHANGED
@@ -6,7 +6,7 @@
6
6
 
7
7
  # Lab42::DataClass
8
8
 
9
- A dataclass with an immutable API (you can still change the state of the object with metaprogramming and `lab42_immutable` is not ready yet!)
9
+ An immutable dataclass
10
10
 
11
11
  ## Usage
12
12
 
@@ -60,7 +60,18 @@ And we can extract the values
60
60
  expect(my_instance.to_h).to eq(name: "robert", email: nil)
61
61
  ```
62
62
 
63
- #### Context: Immutable
63
+ #### Context: Immutable → self
64
+
65
+ Then `my_instance` is frozen:
66
+ ```ruby
67
+ expect(my_instance).to be_frozen
68
+ ```
69
+ And we cannot even mute `my_instance` by means of metaprogramming
70
+ ```ruby
71
+ expect{ my_instance.instance_variable_set("@x", nil) }.to raise_error(FrozenError)
72
+ ```
73
+
74
+ #### Context: Immutable → Cloning
64
75
 
65
76
  Given
66
77
  ```ruby
@@ -71,6 +82,10 @@ Then we have a new instance with the old instance unchanged
71
82
  expect(other_instance.to_h).to eq(name: "robert", email: "robert@mail.provider")
72
83
  expect(my_instance.to_h).to eq(name: "robert", email: nil)
73
84
  ```
85
+ And the new instance is frozen again
86
+ ```ruby
87
+ expect(other_instance).to be_frozen
88
+ ```
74
89
 
75
90
  ### Context: Defining behavior with blocks
76
91
 
@@ -131,6 +146,12 @@ But not in the sense of `equal?`, of course
131
146
  expect(instance2).not_to be_equal(instance1)
132
147
  ```
133
148
 
149
+ #### Immutability of `dataclass` modified classes
150
+
151
+ Then we still get frozen instances
152
+ ```ruby
153
+ expect(instance1).to be_frozen
154
+ ```
134
155
  # LICENSE
135
156
 
136
157
  Copyright 2022 Robert Dober robert.dober@gmail.com
@@ -57,6 +57,14 @@ module Lab42
57
57
  end
58
58
  end
59
59
 
60
+ def _define_freezing_constructor
61
+ ->(*) do
62
+ define_method :new do |*a, **p, &b|
63
+ super(*a, **p, &b).freeze
64
+ end
65
+ end
66
+ end
67
+
60
68
  def _define_initializer
61
69
  proxy = self
62
70
  ->(*) do
@@ -79,6 +87,7 @@ module Lab42
79
87
  end
80
88
 
81
89
  def _define_methods
90
+ (class << klass; self end).module_eval(&_define_freezing_constructor)
82
91
  klass.module_eval(&_define_to_h)
83
92
  klass.module_eval(&_define_merge)
84
93
  klass.module_eval(&_define_eql?)
@@ -2,7 +2,7 @@
2
2
 
3
3
  module Lab42
4
4
  module DataClass
5
- VERSION = "0.1.2"
5
+ VERSION = "0.2.0"
6
6
  end
7
7
  end
8
8
  # SPDX-License-Identifier: Apache-2.0
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lab42_data_class
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Robert Dober
@@ -10,7 +10,11 @@ bindir: bin
10
10
  cert_chain: []
11
11
  date: 2022-01-21 00:00:00.000000000 Z
12
12
  dependencies: []
13
- description: introduces a new Kernel function DataClass
13
+ description: |
14
+ An Immutable DataClass for Ruby
15
+
16
+ Exposes a class factory function `Kernel::DataClass` and a class
17
+ modifer `Module#dataclass'
14
18
  email: robert.dober@gmail.com
15
19
  executables: []
16
20
  extensions: []