lab42_data_class 0.8.3 → 0.8.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/lab42/data_class/constraints/attribute_setters/attribute_setter.rb +9 -4
- data/lib/lab42/data_class/constraints/attribute_setters/list_of_attribute_setter.rb +6 -0
- data/lib/lab42/data_class/constraints/setter_constraint.rb +4 -2
- data/lib/lab42/data_class/proxy.rb +23 -6
- 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: 447747114cf7e2577b5a36da76f39dbc7ffe7e2935bb573d697878a1e4fc77bb
|
4
|
+
data.tar.gz: 5413ab67f94a9a1fb231043c9f6cc3229e8a57c89dab94572d546f1b97476550
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c96c9b135d4be1c05ef413e8623982e6f7896b73c85cb538945fc5af4b4f1686deea02800d6c8a6f9325d212b71dbe8c07f35c1b3e6613308ae71aa9d38df373
|
7
|
+
data.tar.gz: 5d17992111e0925a44b691aa71bf4cb4cc8a35a2a2f73c4f01e3034a0ca3d91a850bde0ee15dc340342d618cbef8c3f26872ae46ddd6b9551013c591c6a782de
|
@@ -5,22 +5,27 @@ module Lab42
|
|
5
5
|
module Constraints
|
6
6
|
module AttributeSetters
|
7
7
|
module AttributeSetter
|
8
|
-
attr_reader :attribute, :constraint, :instance
|
8
|
+
attr_reader :attribute, :constraint, :instance, :return_setter
|
9
9
|
|
10
10
|
private
|
11
11
|
|
12
|
-
def initialize(attribute:, constraint:, instance:)
|
12
|
+
def initialize(attribute:, constraint:, instance:, return_setter: false)
|
13
13
|
@attribute = attribute
|
14
14
|
@constraint = constraint
|
15
15
|
@instance = instance
|
16
|
+
@return_setter = return_setter
|
16
17
|
end
|
17
18
|
|
18
19
|
def _set_attr!(value)
|
19
20
|
new_values = instance.to_h.merge(attribute => value)
|
20
|
-
instance.class.send(:_new_from_merge, {}, new_values)
|
21
|
+
instance.class.send(:_new_from_merge, {}, new_values).tap do |new_instance|
|
22
|
+
return return_setter ? new_instance.set(attribute, return_setter: true) : new_instance
|
23
|
+
end
|
21
24
|
end
|
22
25
|
|
23
|
-
def _value
|
26
|
+
def _value
|
27
|
+
@___value__ ||= instance[attribute]
|
28
|
+
end
|
24
29
|
end
|
25
30
|
end
|
26
31
|
end
|
@@ -18,6 +18,12 @@ module Lab42
|
|
18
18
|
def cdr
|
19
19
|
_set_attr!(_value.cdr)
|
20
20
|
end
|
21
|
+
|
22
|
+
def set_car(value)
|
23
|
+
constraint.constraint.(value) or raise ConstraintError,
|
24
|
+
"cannot set value #{value} in set(#{attribute}).cons"
|
25
|
+
_set_attr!(_value.cdr.cons(value))
|
26
|
+
end
|
21
27
|
end
|
22
28
|
end
|
23
29
|
end
|
@@ -8,13 +8,15 @@ module Lab42
|
|
8
8
|
|
9
9
|
def setter_constraint? = true
|
10
10
|
|
11
|
-
def setter_for(attribute:, instance:)
|
11
|
+
def setter_for(attribute:, instance:, return_setter: false)
|
12
12
|
attribute_setter.new(
|
13
13
|
attribute:,
|
14
14
|
constraint: self,
|
15
|
-
instance
|
15
|
+
instance:,
|
16
|
+
return_setter:
|
16
17
|
)
|
17
18
|
end
|
19
|
+
|
18
20
|
private
|
19
21
|
|
20
22
|
def initialize(constraint:, **other)
|
@@ -110,6 +110,14 @@ module Lab42
|
|
110
110
|
end
|
111
111
|
end
|
112
112
|
|
113
|
+
def _define_instance_methods
|
114
|
+
klass.module_eval(&_define_access)
|
115
|
+
klass.module_eval(&_define_to_h)
|
116
|
+
klass.module_eval(&_define_merge)
|
117
|
+
klass.module_eval(&_define_set)
|
118
|
+
klass.module_eval(&_define_with_attribute)
|
119
|
+
end
|
120
|
+
|
113
121
|
def _define_freezing_constructor
|
114
122
|
proxy = self
|
115
123
|
->(*) do
|
@@ -157,10 +165,7 @@ module Lab42
|
|
157
165
|
|
158
166
|
def _define_methods
|
159
167
|
_define_singleton_methods(klass.singleton_class)
|
160
|
-
|
161
|
-
klass.module_eval(&_define_to_h)
|
162
|
-
klass.module_eval(&_define_merge)
|
163
|
-
klass.module_eval(&_define_set)
|
168
|
+
_define_instance_methods
|
164
169
|
end
|
165
170
|
|
166
171
|
def _define_singleton_methods(singleton)
|
@@ -202,11 +207,23 @@ module Lab42
|
|
202
207
|
def _define_set
|
203
208
|
proxy = self
|
204
209
|
->(*) do
|
205
|
-
define_method :set do |attribute|
|
210
|
+
define_method :set do |attribute, return_setter: false|
|
211
|
+
setter_constraint = proxy.setter_attributes.fetch(attribute) do
|
212
|
+
raise UndefinedSetterError, "There is no constraint implementing a setter for attribute #{attribute}"
|
213
|
+
end
|
214
|
+
setter_constraint.setter_for(attribute:, instance: self, return_setter:)
|
215
|
+
end
|
216
|
+
end
|
217
|
+
end
|
218
|
+
|
219
|
+
def _define_with_attribute
|
220
|
+
proxy = self
|
221
|
+
->(*) do
|
222
|
+
define_method :with_attribute do |attribute, &blk|
|
206
223
|
setter_constraint = proxy.setter_attributes.fetch(attribute) do
|
207
224
|
raise UndefinedSetterError, "There is no constraint implementing a setter for attribute #{attribute}"
|
208
225
|
end
|
209
|
-
setter_constraint.setter_for(attribute:, instance: self)
|
226
|
+
blk.(setter_constraint.setter_for(attribute:, instance: self, return_setter: true)).instance
|
210
227
|
end
|
211
228
|
end
|
212
229
|
end
|
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.8.
|
4
|
+
version: 0.8.4
|
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-03-
|
11
|
+
date: 2022-03-09 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: |
|
14
14
|
An Immutable DataClass for Ruby
|