lab42_data_class 0.2.0 → 0.3.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 +4 -4
- data/README.md +48 -20
- data/lib/lab42/data_class/proxy.rb +3 -4
- data/lib/lab42/data_class/version.rb +1 -1
- data/lib/lab42/data_class.rb +0 -1
- metadata +2 -3
- data/lib/lab42/data_class/for_module.rb +0 -15
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3c062e2633e5881141e65113d2d60878733611bece697c933876ef1d9f62270e
|
4
|
+
data.tar.gz: bb5e524274338fda67063a8514cbe17a27c92ec8b62106061793456245729e30
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ddfaa83d7d62a4f3ccdb6df66cc1b3462ee626ac970b2654e138d338c3e0ba8e016882445552d8fff4ffb13636953f17757991625bc1c54b5b3883c1eed43f89
|
7
|
+
data.tar.gz: 4dae3c50dc6b01e5f311f61e4f8aaf2b712db66a4ace5172b91df242411246e71dc2b30f29cded81ab5b95c77c0858f81282e72a481aff0921d4dbeea990d2f6
|
data/README.md
CHANGED
@@ -105,25 +105,6 @@ Then I have defined a method on my dataclass
|
|
105
105
|
```ruby
|
106
106
|
expect(my_instance.show).to eq("<42>")
|
107
107
|
```
|
108
|
-
### Context: Making a dataclass from a class
|
109
|
-
|
110
|
-
Given
|
111
|
-
```ruby
|
112
|
-
class DC
|
113
|
-
dataclass x: 1, y: 41
|
114
|
-
def sum; x + y end
|
115
|
-
end
|
116
|
-
```
|
117
|
-
|
118
|
-
Then we can define methods on it
|
119
|
-
```ruby
|
120
|
-
expect(DC.new.sum).to eq(42)
|
121
|
-
```
|
122
|
-
|
123
|
-
And we have a nice name for our instances
|
124
|
-
```ruby
|
125
|
-
expect(DC.new.class.name).to eq("DC")
|
126
|
-
```
|
127
108
|
|
128
109
|
### Context: Equality
|
129
110
|
|
@@ -146,12 +127,59 @@ But not in the sense of `equal?`, of course
|
|
146
127
|
expect(instance2).not_to be_equal(instance1)
|
147
128
|
```
|
148
129
|
|
149
|
-
#### Immutability of `dataclass` modified classes
|
130
|
+
#### Context: Immutability of `dataclass` modified classes
|
150
131
|
|
151
132
|
Then we still get frozen instances
|
152
133
|
```ruby
|
153
134
|
expect(instance1).to be_frozen
|
154
135
|
```
|
136
|
+
|
137
|
+
### Context: Inheritance
|
138
|
+
|
139
|
+
... is a no, we do not want inheritance although we **like** code reuse, how to do it then?
|
140
|
+
|
141
|
+
Well there shall be many different possibilities, depending on your style, use case and
|
142
|
+
context, here is just one example:
|
143
|
+
|
144
|
+
Given a class factory
|
145
|
+
```ruby
|
146
|
+
let :token do
|
147
|
+
->(*a, **k) do
|
148
|
+
DataClass(*a, **(k.merge(text: "")))
|
149
|
+
end
|
150
|
+
end
|
151
|
+
```
|
152
|
+
|
153
|
+
Then we have reused the `token` successfully
|
154
|
+
```ruby
|
155
|
+
empty = token.()
|
156
|
+
integer = token.(:value)
|
157
|
+
boolean = token.(value: false)
|
158
|
+
|
159
|
+
expect(empty.new.to_h).to eq(text: "")
|
160
|
+
expect(integer.new(value: -1).to_h).to eq(text: "", value: -1)
|
161
|
+
expect(boolean.new.value).to eq(false)
|
162
|
+
```
|
163
|
+
|
164
|
+
#### Context: Mixing in a module can be used of course
|
165
|
+
|
166
|
+
Given a behavior like
|
167
|
+
```ruby
|
168
|
+
module Humanize
|
169
|
+
def humanize
|
170
|
+
"my value is #{value}"
|
171
|
+
end
|
172
|
+
end
|
173
|
+
|
174
|
+
let(:class_level) { DataClass(value: 1).include(Humanize) }
|
175
|
+
```
|
176
|
+
|
177
|
+
Then we can access the included method
|
178
|
+
```ruby
|
179
|
+
expect(class_level.new.humanize).to eq("my value is 1")
|
180
|
+
```
|
181
|
+
|
182
|
+
|
155
183
|
# LICENSE
|
156
184
|
|
157
185
|
Copyright 2022 Robert Dober robert.dober@gmail.com
|
@@ -4,7 +4,7 @@ require 'set'
|
|
4
4
|
module Lab42
|
5
5
|
module DataClass
|
6
6
|
class Proxy
|
7
|
-
attr_reader :actual_params, :block, :defaults, :klass, :
|
7
|
+
attr_reader :actual_params, :block, :defaults, :klass, :members, :positionals
|
8
8
|
|
9
9
|
def check!(**params)
|
10
10
|
@actual_params = params
|
@@ -31,8 +31,7 @@ module Lab42
|
|
31
31
|
|
32
32
|
private
|
33
33
|
def initialize(*args, **kwds, &blk)
|
34
|
-
@klass =
|
35
|
-
@has_parent = !!kwds.delete(:__klass__)
|
34
|
+
@klass = Class.new
|
36
35
|
|
37
36
|
@block = blk
|
38
37
|
@defaults = kwds
|
@@ -80,7 +79,7 @@ module Lab42
|
|
80
79
|
->(*) do
|
81
80
|
define_method :merge do |**params|
|
82
81
|
values = to_h.merge(params)
|
83
|
-
DataClass(*proxy.positionals,
|
82
|
+
DataClass(*proxy.positionals, **proxy.defaults, &proxy.block)
|
84
83
|
.new(**values)
|
85
84
|
end
|
86
85
|
end
|
data/lib/lab42/data_class.rb
CHANGED
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.
|
4
|
+
version: 0.3.0
|
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-22 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: |
|
14
14
|
An Immutable DataClass for Ruby
|
@@ -23,7 +23,6 @@ files:
|
|
23
23
|
- LICENSE
|
24
24
|
- README.md
|
25
25
|
- lib/lab42/data_class.rb
|
26
|
-
- lib/lab42/data_class/for_module.rb
|
27
26
|
- lib/lab42/data_class/proxy.rb
|
28
27
|
- lib/lab42/data_class/version.rb
|
29
28
|
homepage: https://github.com/robertdober/lab42_data_class
|
@@ -1,15 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require_relative 'proxy'
|
4
|
-
|
5
|
-
module Lab42
|
6
|
-
module DataClass
|
7
|
-
class ::Module
|
8
|
-
def dataclass(*args, **defaults)
|
9
|
-
proxy = Lab42::DataClass::Proxy.new(*args, __klass__: self, **defaults)
|
10
|
-
proxy.define_class!
|
11
|
-
end
|
12
|
-
end
|
13
|
-
end
|
14
|
-
end
|
15
|
-
# SPDX-License-Identifier: Apache-2.0
|