evt-schema 2.3.1.6 → 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: 3d2604faa3a43356561c9c0ccaf6ef63550d3351d79a6f4f032acf62b4ebb6d0
4
- data.tar.gz: 4e93fb8a36bb8fb3de0220c08450b56254a14ef3447ea242a9d09774e4bb3ba0
3
+ metadata.gz: 81b342195e97b5f56c9763504e915f97a56e3f22f8f789b2d8dfd9a55eb62afb
4
+ data.tar.gz: bc242d36979dc1f82223cabced500fd62f289c915f4b925346eb97c9f6d2792e
5
5
  SHA512:
6
- metadata.gz: 4d6b5342a2df666e0fcbe3e1d3c25afd67a2854b1f1e46c02fc836c92f94568e3df6a9da492dd0d8f50ef4b4b0c6393175056aba8bb8ed2faa07d1115cd926df
7
- data.tar.gz: 6bd8e4673db5859e8b29a2b587afe94a1faf55966c52683f5d5e3a64ee18f0ebb036eff3516a48a385988e1e18ca8645955023ebdf1d42a236e54aceeaba564c
6
+ metadata.gz: 4f396482f4fca9cfb7210123bfcda0ea426f4a36d7bc8ac5616029b3d99daab19a89d865e2595e8d565c87cea947850e79963910f81d07bac3f6b671b5e895a1
7
+ data.tar.gz: 7d5736df27d4f8a607bb870a93d61f5ae37e8ab1e480234c92c580f3af072b1e3cd72c67e5e3de783012128deb0039950c79a2606ce85e63a339cc9c513b387d
@@ -119,25 +119,9 @@ module Schema
119
119
  end
120
120
 
121
121
  class Example
122
- include ::Schema
123
- attribute :some_attribute, default: 'some default value'
124
- attribute :some_other_attribute
125
- end
126
-
127
- class Primitive
128
- include ::Schema
129
- attribute :some_attribute, default: 11
130
- attribute :some_other_attribute
131
- end
132
-
133
- class Proc
134
122
  include ::Schema
135
123
  attribute :some_attribute, default: proc { 'some default value' }
136
- end
137
-
138
- class ObjectReference
139
- include ::Schema
140
- attribute :some_attribute, default: []
124
+ attribute :some_other_attribute
141
125
  end
142
126
  end
143
127
 
@@ -152,13 +136,27 @@ module Schema
152
136
  include ::Schema
153
137
  attribute :some_attribute, SomeType
154
138
  end
139
+ end
155
140
 
156
- module Strict
157
- class Example
158
- include ::Schema
159
- attribute :some_attribute, SomeType, strict: true
141
+ module Check
142
+ def self.check
143
+ lambda do |type, value|
144
+ value == Check.valid_some_attribute
160
145
  end
161
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
162
160
  end
163
161
 
164
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,56 +25,45 @@ 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
 
65
- initialize_value = nil
66
- if default.is_a? Proc
37
+ if default.nil?
38
+ initialize_value = nil
39
+ elsif default.respond_to?(:call)
67
40
  initialize_value = default
68
- elsif !default.nil?
69
- initialize_value = proc { default.clone }
41
+ else
42
+ raise Schema::Attribute::Error, "Default values must be callable, like procs, lambdas, or objects that respond to the call method (Attribute: #{attribute_name})"
70
43
  end
71
44
 
72
- ::Attribute::Define.(self, attribute_name, :accessor, check: check, &initialize_value)
45
+ ::Attribute::Define.(self, attribute_name, :accessor, check: attribute_check, &initialize_value)
73
46
 
74
- attribute = attributes.register(attribute_name, type, strict)
47
+ attribute = attributes.register(attribute_name, type)
75
48
  attribute
76
49
  end
77
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
78
67
  end
79
68
 
80
69
  module Attributes
@@ -134,16 +123,15 @@ module Schema
134
123
  entries.each_with_object(obj, &action)
135
124
  end
136
125
 
137
- def add(name, type, strict=nil)
138
- strict ||= false
139
- attribute = Schema::Attribute.new(name, type, strict)
126
+ def add(name, type)
127
+ attribute = Schema::Attribute.new(name, type)
140
128
  entries << attribute
141
129
  attribute
142
130
  end
143
131
 
144
- def register(name, type, strict=nil)
132
+ def register(name, type)
145
133
  remove(name)
146
- add(name, type, strict)
134
+ add(name, type)
147
135
  end
148
136
 
149
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.3.1.6
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-04-21 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.4.10
140
+ rubygems_version: 3.5.11
140
141
  signing_key:
141
142
  specification_version: 4
142
143
  summary: Primitives for schema and data structure