lab42_data_class 0.8.1 → 0.8.4
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/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/constraint.rb +5 -1
- data/lib/lab42/data_class/constraints/setter_constraint.rb +4 -2
- data/lib/lab42/data_class/proxy/constraints/maker.rb +1 -1
- data/lib/lab42/data_class/proxy/constraints.rb +8 -6
- data/lib/lab42/data_class/proxy.rb +23 -6
- data/lib/lab42/data_class/version.rb +1 -1
- data/lib/lab42/list.rb +26 -9
- data/lib/lab42/nil.rb +19 -4
- 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
|
|
@@ -7,8 +7,12 @@ module Lab42
|
|
|
7
7
|
class Constraint
|
|
8
8
|
attr_reader :name, :function
|
|
9
9
|
|
|
10
|
-
def call(value)
|
|
10
|
+
def call(value)
|
|
11
|
+
function.(value)
|
|
12
|
+
end
|
|
13
|
+
|
|
11
14
|
def setter_constraint? = false
|
|
15
|
+
def to_proc = function
|
|
12
16
|
def to_s = "Constraint<#{name}>"
|
|
13
17
|
|
|
14
18
|
private
|
|
@@ -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)
|
|
@@ -5,9 +5,9 @@ module Lab42
|
|
|
5
5
|
module DataClass
|
|
6
6
|
class Proxy
|
|
7
7
|
module Constraints
|
|
8
|
-
def check_constraints_against_defaults
|
|
8
|
+
def check_constraints_against_defaults
|
|
9
9
|
errors = constraints
|
|
10
|
-
.
|
|
10
|
+
.flat_map(&_check_constraint_against_default)
|
|
11
11
|
.compact
|
|
12
12
|
raise ConstraintError, errors.join("\n\n") unless errors.empty?
|
|
13
13
|
end
|
|
@@ -19,7 +19,7 @@ module Lab42
|
|
|
19
19
|
"constraints cannot be defined for undefined attributes #{errors.inspect}"
|
|
20
20
|
end
|
|
21
21
|
|
|
22
|
-
check_constraints_against_defaults
|
|
22
|
+
check_constraints_against_defaults
|
|
23
23
|
end
|
|
24
24
|
|
|
25
25
|
private
|
|
@@ -32,9 +32,11 @@ module Lab42
|
|
|
32
32
|
end
|
|
33
33
|
end
|
|
34
34
|
|
|
35
|
-
def _check_constraint_against_default_value(attr, value,
|
|
36
|
-
|
|
37
|
-
|
|
35
|
+
def _check_constraint_against_default_value(attr, value, constraints)
|
|
36
|
+
constraints.map do |constraint|
|
|
37
|
+
unless constraint.(value)
|
|
38
|
+
"default value #{value.inspect} is not allowed for attribute #{attr.inspect}"
|
|
39
|
+
end
|
|
38
40
|
end
|
|
39
41
|
rescue StandardError => e
|
|
40
42
|
"constraint error during validation of default value of attribute #{attr.inspect}\n #{e.message}"
|
|
@@ -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
|
data/lib/lab42/list.rb
CHANGED
|
@@ -7,27 +7,44 @@ module Lab42
|
|
|
7
7
|
include Enumerable
|
|
8
8
|
attr_reader :car, :cdr, :length
|
|
9
9
|
|
|
10
|
-
def ==(other)
|
|
10
|
+
def ==(other)
|
|
11
11
|
self.class.list?(other) &&
|
|
12
12
|
length == other.length &&
|
|
13
13
|
car == other.car &&
|
|
14
14
|
cdr == other.cdr
|
|
15
|
+
end
|
|
15
16
|
|
|
16
|
-
def cadr
|
|
17
|
+
def cadr
|
|
18
|
+
cdr.car
|
|
19
|
+
end
|
|
17
20
|
|
|
18
|
-
def caddr
|
|
21
|
+
def caddr
|
|
22
|
+
cdr.cdr.car
|
|
23
|
+
end
|
|
19
24
|
|
|
20
|
-
def cddr
|
|
25
|
+
def cddr
|
|
26
|
+
cdr.cdr
|
|
27
|
+
end
|
|
21
28
|
|
|
22
|
-
def cdddr
|
|
29
|
+
def cdddr
|
|
30
|
+
cdr.cdr.cdr
|
|
31
|
+
end
|
|
23
32
|
|
|
24
|
-
def cons(new_head)
|
|
33
|
+
def cons(new_head)
|
|
34
|
+
self.class.cons(new_head, self)
|
|
35
|
+
end
|
|
25
36
|
|
|
26
|
-
def deconstruct
|
|
37
|
+
def deconstruct
|
|
38
|
+
[car, cdr]
|
|
39
|
+
end
|
|
27
40
|
|
|
28
|
-
def each(&blk)
|
|
41
|
+
def each(&blk)
|
|
42
|
+
self.class.each(self, &blk)
|
|
43
|
+
end
|
|
29
44
|
|
|
30
|
-
def empty?
|
|
45
|
+
def empty?
|
|
46
|
+
false
|
|
47
|
+
end
|
|
31
48
|
end
|
|
32
49
|
end
|
|
33
50
|
# SPDX-License-Identifier: Apache-2.0
|
data/lib/lab42/nil.rb
CHANGED
|
@@ -4,10 +4,25 @@ module Lab42
|
|
|
4
4
|
module Nil
|
|
5
5
|
extend self, Enumerable
|
|
6
6
|
|
|
7
|
-
def deconstruct
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
7
|
+
def deconstruct
|
|
8
|
+
[]
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
def each
|
|
12
|
+
self
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def empty?
|
|
16
|
+
true
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def length
|
|
20
|
+
0
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def cons(new_head)
|
|
24
|
+
List.cons(new_head, self)
|
|
25
|
+
end
|
|
11
26
|
end
|
|
12
27
|
end
|
|
13
28
|
# SPDX-License-Identifier: Apache-2.0
|
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
|