evt-schema 2.5.0.0.pre.1 → 2.5.0.0

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: 81b342195e97b5f56c9763504e915f97a56e3f22f8f789b2d8dfd9a55eb62afb
4
- data.tar.gz: bc242d36979dc1f82223cabced500fd62f289c915f4b925346eb97c9f6d2792e
3
+ metadata.gz: b7f39ba650c62ee4bf0f2de29183ab17eb1425d770f56d098584d744d6d6cb02
4
+ data.tar.gz: d0474a2e4d66b03ef4424a06fab9ecb0683570ff55bdd928895717cd238fd59f
5
5
  SHA512:
6
- metadata.gz: 4f396482f4fca9cfb7210123bfcda0ea426f4a36d7bc8ac5616029b3d99daab19a89d865e2595e8d565c87cea947850e79963910f81d07bac3f6b671b5e895a1
7
- data.tar.gz: 7d5736df27d4f8a607bb870a93d61f5ae37e8ab1e480234c92c580f3af072b1e3cd72c67e5e3de783012128deb0039950c79a2606ce85e63a339cc9c513b387d
6
+ metadata.gz: 3fb7c6d8ca133bd24cb212430e488a8dfbdf2e794cfc35addb7a186e1b1e9febbeaf70bfed375153fd8dda5cc216d6fc6f53f4135e2a1504da29c73a835e2383
7
+ data.tar.gz: dfd3e8e2e47d6dfbe0f064926f9b6fac9b26cfa0ece8956f1ecb5dda7fe6cf4dc1a1b45dadacea386c95adbef2b29edb63f392afd916eb4eee9c1d1adb84859e
@@ -138,13 +138,7 @@ module Schema
138
138
  end
139
139
  end
140
140
 
141
- module Check
142
- def self.check
143
- lambda do |type, value|
144
- value == Check.valid_some_attribute
145
- end
146
- end
147
-
141
+ module TypeCheck
148
142
  def self.valid_some_attribute
149
143
  "some-valid-value"
150
144
  end
@@ -153,9 +147,17 @@ module Schema
153
147
  "some-invalid-value"
154
148
  end
155
149
 
150
+ class ExampleType
151
+ module TypeCheck
152
+ def self.call(type, value)
153
+ value == Schema::TypeCheck.valid_some_attribute
154
+ end
155
+ end
156
+ end
157
+
156
158
  class Example
157
159
  include ::Schema
158
- attribute :some_attribute, check: Check.check
160
+ attribute :some_attribute, ExampleType
159
161
  end
160
162
  end
161
163
 
data/lib/schema/schema.rb CHANGED
@@ -25,12 +25,12 @@ module Schema
25
25
  end
26
26
 
27
27
  module AttributeMacro
28
- def attribute_macro(attribute_name, type=nil, default: nil, check: nil)
29
- check ||= Defaults.check
28
+ def attribute_macro(attribute_name, type=nil, default: nil)
29
+ type_check = TypeCheck.get(type)
30
30
 
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}"
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 #{self.to_s} attribute #{attribute_name.inspect} of type #{type.name}"
34
34
  end
35
35
  end
36
36
 
@@ -42,25 +42,30 @@ module Schema
42
42
  raise Schema::Attribute::Error, "Default values must be callable, like procs, lambdas, or objects that respond to the call method (Attribute: #{attribute_name})"
43
43
  end
44
44
 
45
- ::Attribute::Define.(self, attribute_name, :accessor, check: attribute_check, &initialize_value)
45
+ ::Attribute::Define.(self, attribute_name, :accessor, check: check, &initialize_value)
46
46
 
47
47
  attribute = attributes.register(attribute_name, type)
48
48
  attribute
49
49
  end
50
50
  alias :attribute :attribute_macro
51
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
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
64
69
  end
65
70
  end
66
71
  end
@@ -148,8 +153,12 @@ module Schema
148
153
  end
149
154
 
150
155
  module Boolean
151
- def self.call(val)
152
- 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
153
162
  end
154
163
  end
155
164
 
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.5.0.0.pre.1
4
+ version: 2.5.0.0
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-10-01 00:00:00.000000000 Z
11
+ date: 2024-12-21 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
@@ -137,7 +151,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
137
151
  - !ruby/object:Gem::Version
138
152
  version: '0'
139
153
  requirements: []
140
- rubygems_version: 3.5.11
154
+ rubygems_version: 3.5.22
141
155
  signing_key:
142
156
  specification_version: 4
143
157
  summary: Primitives for schema and data structure