evt-schema 2.4.0.0 → 2.5.0.0.pre.1

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: 35387bf895aaceee0a8b592ba686dd1cdc24448c8bca10d7e7714f0ff55387b4
4
- data.tar.gz: 8d60be6f2a80644f3fb6517d72c5d10842fc399386d509aeeb19cf051e99a61d
3
+ metadata.gz: 81b342195e97b5f56c9763504e915f97a56e3f22f8f789b2d8dfd9a55eb62afb
4
+ data.tar.gz: bc242d36979dc1f82223cabced500fd62f289c915f4b925346eb97c9f6d2792e
5
5
  SHA512:
6
- metadata.gz: 712f0cc9e56c0d053226a21375336c16664b02ff8a77df029c32c01ad75c38bc02ce57bb3bcfef91ed93701a1c04e117ecb885d2da3e2e4b2da1ced3d5786cef
7
- data.tar.gz: 5e403e19b077b933fc0d22783679ba3673e5afa8decc9e82dcb8d9f555222ab568241bfac3e5df6615e44d97ff1aedfb2975e0d5af6786536c76da04a4deb876
6
+ metadata.gz: 4f396482f4fca9cfb7210123bfcda0ea426f4a36d7bc8ac5616029b3d99daab19a89d865e2595e8d565c87cea947850e79963910f81d07bac3f6b671b5e895a1
7
+ data.tar.gz: 7d5736df27d4f8a607bb870a93d61f5ae37e8ab1e480234c92c580f3af072b1e3cd72c67e5e3de783012128deb0039950c79a2606ce85e63a339cc9c513b387d
@@ -136,13 +136,27 @@ module Schema
136
136
  include ::Schema
137
137
  attribute :some_attribute, SomeType
138
138
  end
139
+ end
139
140
 
140
- module Strict
141
- class Example
142
- include ::Schema
143
- attribute :some_attribute, SomeType, strict: true
141
+ module Check
142
+ def self.check
143
+ lambda do |type, value|
144
+ value == Check.valid_some_attribute
144
145
  end
145
146
  end
147
+
148
+ def self.valid_some_attribute
149
+ "some-valid-value"
150
+ end
151
+
152
+ def self.invalid_some_attribute
153
+ "some-invalid-value"
154
+ end
155
+
156
+ class Example
157
+ include ::Schema
158
+ attribute :some_attribute, check: Check.check
159
+ end
146
160
  end
147
161
 
148
162
  module Boolean
@@ -5,12 +5,10 @@ module Schema
5
5
 
6
6
  attr_reader :name
7
7
  attr_reader :type
8
- attr_reader :strict
9
8
 
10
- def initialize(name, type, strict)
9
+ def initialize(name, type)
11
10
  @name = name
12
11
  @type = type
13
- @strict = strict
14
12
  end
15
13
  end
16
14
  end
data/lib/schema/schema.rb CHANGED
@@ -25,40 +25,12 @@ module Schema
25
25
  end
26
26
 
27
27
  module AttributeMacro
28
- def attribute_macro(attribute_name, type=nil, strict: nil, default: nil)
29
- if type.nil? && !strict.nil?
30
- raise Schema::Attribute::Error, "The \"#{attribute_name}\" attribute is declared with the \"strict\" option but a type is not specified"
31
- end
32
-
33
- if type == Boolean && strict == false
34
- raise Schema::Attribute::Error, "The \"#{attribute_name}\" attribute is declared with the \"strict\" option disabled but boolean type is specified"
35
- end
36
-
37
- check = nil
28
+ def attribute_macro(attribute_name, type=nil, default: nil, check: nil)
29
+ check ||= Defaults.check
38
30
 
39
- if type == Boolean
40
- strict ||= true
41
-
42
- check = proc do |val|
43
- unless val.nil? || Boolean.(val)
44
- raise Schema::Attribute::TypeError, "#{val.inspect} of type #{val.class.name} cannot be assigned to attribute #{attribute_name.inspect} of type Boolean (Strict: #{strict.inspect})"
45
- end
46
- end
47
- elsif !type.nil?
48
- strict ||= false
49
-
50
- check = proc do |val|
51
- unless val.nil?
52
- if strict
53
- if not val.instance_of?(type)
54
- raise Schema::Attribute::TypeError, "#{val.inspect} of type #{val.class.name} cannot be assigned to attribute #{attribute_name.inspect} of type #{type.name} (Strict: #{strict.inspect})"
55
- end
56
- else
57
- if not val.is_a?(type)
58
- raise Schema::Attribute::TypeError, "#{val.inspect} of type #{val.class.name} cannot be assigned to attribute #{attribute_name.inspect} of type #{type.name} (Strict: #{strict.inspect})"
59
- end
60
- end
61
- end
31
+ attribute_check = lambda do |val|
32
+ if not check.(type, val)
33
+ raise Schema::Attribute::TypeError, "#{val.inspect} of type #{val.class.name} cannot be assigned to attribute #{attribute_name.inspect} of #{self.to_s}"
62
34
  end
63
35
  end
64
36
 
@@ -70,12 +42,28 @@ module Schema
70
42
  raise Schema::Attribute::Error, "Default values must be callable, like procs, lambdas, or objects that respond to the call method (Attribute: #{attribute_name})"
71
43
  end
72
44
 
73
- ::Attribute::Define.(self, attribute_name, :accessor, check: check, &initialize_value)
45
+ ::Attribute::Define.(self, attribute_name, :accessor, check: attribute_check, &initialize_value)
74
46
 
75
- attribute = attributes.register(attribute_name, type, strict)
47
+ attribute = attributes.register(attribute_name, type)
76
48
  attribute
77
49
  end
78
50
  alias :attribute :attribute_macro
51
+
52
+ module Defaults
53
+ def self.check
54
+ lambda do |type, val|
55
+ return true if val.nil?
56
+
57
+ if type == Boolean
58
+ Boolean.(val)
59
+ elsif !type.nil?
60
+ val.is_a?(type)
61
+ else
62
+ true
63
+ end
64
+ end
65
+ end
66
+ end
79
67
  end
80
68
 
81
69
  module Attributes
@@ -135,16 +123,15 @@ module Schema
135
123
  entries.each_with_object(obj, &action)
136
124
  end
137
125
 
138
- def add(name, type, strict=nil)
139
- strict ||= false
140
- attribute = Schema::Attribute.new(name, type, strict)
126
+ def add(name, type)
127
+ attribute = Schema::Attribute.new(name, type)
141
128
  entries << attribute
142
129
  attribute
143
130
  end
144
131
 
145
- def register(name, type, strict=nil)
132
+ def register(name, type)
146
133
  remove(name)
147
- add(name, type, strict)
134
+ add(name, type)
148
135
  end
149
136
 
150
137
  def remove(name)
@@ -0,0 +1,50 @@
1
+ Foo.build
2
+
3
+ -> transform_read({})
4
+
5
+
6
+
7
+
8
+
9
+
10
+
11
+
12
+
13
+ class Thing
14
+ attribute :some_attribute, String
15
+ attribute :some_object, SomeClass
16
+
17
+ def self.transform_read(data)
18
+ some_object_data = data[:some_object] # => nil or some hash
19
+
20
+ if not some_object_data.nil?
21
+ foo_data = some_object_data[:foo] # => Error
22
+ end
23
+
24
+ some_object = SomeClass.build(some_object_data)
25
+
26
+ data[:some_object] = some_object
27
+ end
28
+ end
29
+
30
+ class SomeClass
31
+ attribute :foo
32
+ end
33
+
34
+
35
+ data = {}
36
+ thing = Thing.build(data)
37
+
38
+ build(data) -> transform_read(data) # Rem: data is empty
39
+
40
+
41
+
42
+
43
+
44
+
45
+
46
+ thing = data[:some_atttribute]
47
+ # => nil
48
+
49
+ thing.other_thing
50
+ # => nil reference error
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: evt-schema
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.4.0.0
4
+ version: 2.5.0.0.pre.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - The Eventide Project
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-06-29 00:00:00.000000000 Z
11
+ date: 2024-10-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: evt-attribute
@@ -115,6 +115,7 @@ files:
115
115
  - lib/schema/schema/compare.rb
116
116
  - lib/schema/schema/compare/comparison.rb
117
117
  - lib/schema/schema/compare/comparison/entry.rb
118
+ - lib/schema/scratch.rb
118
119
  - lib/schema/validation/error.rb
119
120
  - lib/schema/validation/nil_attributes.rb
120
121
  homepage: https://github.com/eventide-project/schema
@@ -136,7 +137,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
136
137
  - !ruby/object:Gem::Version
137
138
  version: '0'
138
139
  requirements: []
139
- rubygems_version: 3.5.9
140
+ rubygems_version: 3.5.11
140
141
  signing_key:
141
142
  specification_version: 4
142
143
  summary: Primitives for schema and data structure