lab42_data_class 0.3.1 → 0.3.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 +4 -4
- data/README.md +45 -1
- data/lib/lab42/data_class/proxy.rb +9 -0
- data/lib/lab42/data_class/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ca6b2bccf941edcb400b3e9230d394ef867a4a37b6865f5dd76e521fdb33070f
|
4
|
+
data.tar.gz: 431c61adef3873183ecba35f86c1f5397c4b8c3b9df140d09438e8e3a5d0b1da
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3f63d89d02aa4a4e2ac551ce79c6f09787ab7d7f173ef459e10a689b0c24dd1682b6d27e756534108187a113784a632adfa384858d2e553e8ca37a72d4bb1b09
|
7
|
+
data.tar.gz: 8be0680e0cd43d98a9fe8bfdc8c1844ad076e65fdba2b7360f2471d2f74498dbe937fd74c456231b80066541739551e76cf58356b9fa45bdbf02a5fdab673573
|
data/README.md
CHANGED
@@ -181,7 +181,7 @@ Then we can access the included method
|
|
181
181
|
|
182
182
|
### Context: Pattern Matching
|
183
183
|
|
184
|
-
|
184
|
+
A `DataClass` object behaves like the result of it's `to_h` in pattern matching
|
185
185
|
|
186
186
|
Given
|
187
187
|
```ruby
|
@@ -211,6 +211,50 @@ And in `in` expressions
|
|
211
211
|
expect(second).to eq(4)
|
212
212
|
```
|
213
213
|
|
214
|
+
#### Context: In Case Statements
|
215
|
+
|
216
|
+
Given a nice little dataclass `Box`
|
217
|
+
```ruby
|
218
|
+
let(:box) { DataClass content: nil }
|
219
|
+
```
|
220
|
+
|
221
|
+
Then we can also use it in a case statement
|
222
|
+
```ruby
|
223
|
+
value = case box.new
|
224
|
+
when box
|
225
|
+
42
|
226
|
+
else
|
227
|
+
0
|
228
|
+
end
|
229
|
+
expect(value).to eq(42)
|
230
|
+
```
|
231
|
+
|
232
|
+
And all the associated methods
|
233
|
+
```ruby
|
234
|
+
expect(box.new).to be_a(box)
|
235
|
+
expect(box === box.new).to be_truthy
|
236
|
+
```
|
237
|
+
|
238
|
+
### Context: Behaving like a proc
|
239
|
+
|
240
|
+
It is useful to be able to filter heterogeneous lists of `DataClass` instances by means of `&to_proc`, therefore
|
241
|
+
|
242
|
+
Given two different `DataClass` objects
|
243
|
+
```ruby
|
244
|
+
let(:class1) { DataClass :value }
|
245
|
+
let(:class2) { DataClass :value }
|
246
|
+
```
|
247
|
+
|
248
|
+
And a list of instances
|
249
|
+
```ruby
|
250
|
+
let(:list) {[class1.new(value: 1), class2.new(value: 2), class1.new(value: 3)]}
|
251
|
+
```
|
252
|
+
|
253
|
+
Then we can filter
|
254
|
+
```ruby
|
255
|
+
expect(list.filter(&class2)).to eq([class2.new(value: 2)])
|
256
|
+
```
|
257
|
+
|
214
258
|
# LICENSE
|
215
259
|
|
216
260
|
Copyright 2022 Robert Dober robert.dober@gmail.com
|
@@ -81,6 +81,7 @@ module Lab42
|
|
81
81
|
|
82
82
|
def _define_methods
|
83
83
|
(class << klass; self end).module_eval(&_define_freezing_constructor)
|
84
|
+
(class << klass; self end).module_eval(&_define_to_proc)
|
84
85
|
klass.module_eval(&_define_to_h)
|
85
86
|
klass.module_eval(&_define_merge)
|
86
87
|
end
|
@@ -94,6 +95,14 @@ module Lab42
|
|
94
95
|
end
|
95
96
|
end
|
96
97
|
|
98
|
+
def _define_to_proc
|
99
|
+
->(*) do
|
100
|
+
define_method :to_proc do
|
101
|
+
->(other) { self === other }
|
102
|
+
end
|
103
|
+
end
|
104
|
+
end
|
105
|
+
|
97
106
|
def _init(data_class_instance, params)
|
98
107
|
params.each do |key, value|
|
99
108
|
data_class_instance.instance_variable_set("@#{key}", value)
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: lab42_data_class
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.3.
|
4
|
+
version: 0.3.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Robert Dober
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2022-01-
|
11
|
+
date: 2022-01-30 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: |
|
14
14
|
An Immutable DataClass for Ruby
|