evt-schema 2.4.0.0 → 2.5.0.0.pre.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 35387bf895aaceee0a8b592ba686dd1cdc24448c8bca10d7e7714f0ff55387b4
4
- data.tar.gz: 8d60be6f2a80644f3fb6517d72c5d10842fc399386d509aeeb19cf051e99a61d
3
+ metadata.gz: 2da63ac381710f07d3eaf73c14935519846e1cf9f97ac4cbe27c472a8034af80
4
+ data.tar.gz: 7fa56b66b7524dda5089d1209040604f34247551d41dfc98f9497c576493114c
5
5
  SHA512:
6
- metadata.gz: 712f0cc9e56c0d053226a21375336c16664b02ff8a77df029c32c01ad75c38bc02ce57bb3bcfef91ed93701a1c04e117ecb885d2da3e2e4b2da1ced3d5786cef
7
- data.tar.gz: 5e403e19b077b933fc0d22783679ba3673e5afa8decc9e82dcb8d9f555222ab568241bfac3e5df6615e44d97ff1aedfb2975e0d5af6786536c76da04a4deb876
6
+ metadata.gz: bae6ed63b133103a875b85cbb83a89e4f5b3d051408eb7da056bf11778ee4ecc60c2cc4732bc04fa2ecf62a2dbf289be604b6f97d8e5778c777b9d8f5c8518b9
7
+ data.tar.gz: 0c5604d671d62e5104632d6733d481a633f6f7c79bcffcd07b59eeb976ed784a1fbacff9aa0840b989c5563aad0d16082795910616f5c5b6e8a001a907f1918f
@@ -136,13 +136,29 @@ 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 TypeCheck
142
+ def self.valid_some_attribute
143
+ "some-valid-value"
144
+ end
145
+
146
+ def self.invalid_some_attribute
147
+ "some-invalid-value"
148
+ end
149
+
150
+ class ExampleType
151
+ module TypeCheck
152
+ def self.call(type, value)
153
+ value == Schema::TypeCheck.valid_some_attribute
154
+ end
144
155
  end
145
156
  end
157
+
158
+ class Example
159
+ include ::Schema
160
+ attribute :some_attribute, ExampleType
161
+ end
146
162
  end
147
163
 
148
164
  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
38
-
39
- if type == Boolean
40
- strict ||= true
28
+ def attribute_macro(attribute_name, type=nil, default: nil)
29
+ type_check = TypeCheck.get(type)
41
30
 
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
+ check = lambda do |val|
32
+ if not type_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
 
@@ -72,10 +44,31 @@ module Schema
72
44
 
73
45
  ::Attribute::Define.(self, attribute_name, :accessor, check: 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 TypeCheck
53
+ def self.call(type, val)
54
+ return true if val.nil?
55
+ return true if type.nil?
56
+
57
+ val.is_a?(type)
58
+ end
59
+
60
+ def self.get(type)
61
+ return self if type.nil?
62
+
63
+ result = Reflect.(type, :TypeCheck, ancestors: true, strict: false)
64
+
65
+ if not result.nil?
66
+ return result.constant
67
+ else
68
+ return self
69
+ end
70
+ end
71
+ end
79
72
  end
80
73
 
81
74
  module Attributes
@@ -135,16 +128,15 @@ module Schema
135
128
  entries.each_with_object(obj, &action)
136
129
  end
137
130
 
138
- def add(name, type, strict=nil)
139
- strict ||= false
140
- attribute = Schema::Attribute.new(name, type, strict)
131
+ def add(name, type)
132
+ attribute = Schema::Attribute.new(name, type)
141
133
  entries << attribute
142
134
  attribute
143
135
  end
144
136
 
145
- def register(name, type, strict=nil)
137
+ def register(name, type)
146
138
  remove(name)
147
- add(name, type, strict)
139
+ add(name, type)
148
140
  end
149
141
 
150
142
  def remove(name)
@@ -161,8 +153,12 @@ module Schema
161
153
  end
162
154
 
163
155
  module Boolean
164
- def self.call(val)
165
- val == true || val == false
156
+ module TypeCheck
157
+ def self.call(type, val)
158
+ return true if val.nil?
159
+
160
+ val == true || val == false
161
+ end
166
162
  end
167
163
  end
168
164
 
@@ -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
data/lib/schema.rb CHANGED
@@ -3,6 +3,7 @@ require 'set_attributes'
3
3
  require 'initializer'
4
4
  require 'validate'
5
5
  require 'template_method'
6
+ require 'reflect'
6
7
 
7
8
  require 'schema/schema'
8
9
  require 'schema/schema/compare/comparison'
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.2
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-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: evt-attribute
@@ -80,6 +80,20 @@ dependencies:
80
80
  - - ">="
81
81
  - !ruby/object:Gem::Version
82
82
  version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: evt-reflect
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
83
97
  - !ruby/object:Gem::Dependency
84
98
  name: test_bench
85
99
  requirement: !ruby/object:Gem::Requirement
@@ -115,6 +129,7 @@ files:
115
129
  - lib/schema/schema/compare.rb
116
130
  - lib/schema/schema/compare/comparison.rb
117
131
  - lib/schema/schema/compare/comparison/entry.rb
132
+ - lib/schema/scratch.rb
118
133
  - lib/schema/validation/error.rb
119
134
  - lib/schema/validation/nil_attributes.rb
120
135
  homepage: https://github.com/eventide-project/schema
@@ -136,7 +151,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
136
151
  - !ruby/object:Gem::Version
137
152
  version: '0'
138
153
  requirements: []
139
- rubygems_version: 3.5.9
154
+ rubygems_version: 3.5.11
140
155
  signing_key:
141
156
  specification_version: 4
142
157
  summary: Primitives for schema and data structure