evt-schema 2.5.0.0.pre.1 → 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: 81b342195e97b5f56c9763504e915f97a56e3f22f8f789b2d8dfd9a55eb62afb
4
- data.tar.gz: bc242d36979dc1f82223cabced500fd62f289c915f4b925346eb97c9f6d2792e
3
+ metadata.gz: 2da63ac381710f07d3eaf73c14935519846e1cf9f97ac4cbe27c472a8034af80
4
+ data.tar.gz: 7fa56b66b7524dda5089d1209040604f34247551d41dfc98f9497c576493114c
5
5
  SHA512:
6
- metadata.gz: 4f396482f4fca9cfb7210123bfcda0ea426f4a36d7bc8ac5616029b3d99daab19a89d865e2595e8d565c87cea947850e79963910f81d07bac3f6b671b5e895a1
7
- data.tar.gz: 7d5736df27d4f8a607bb870a93d61f5ae37e8ab1e480234c92c580f3af072b1e3cd72c67e5e3de783012128deb0039950c79a2606ce85e63a339cc9c513b387d
6
+ metadata.gz: bae6ed63b133103a875b85cbb83a89e4f5b3d051408eb7da056bf11778ee4ecc60c2cc4732bc04fa2ecf62a2dbf289be604b6f97d8e5778c777b9d8f5c8518b9
7
+ data.tar.gz: 0c5604d671d62e5104632d6733d481a633f6f7c79bcffcd07b59eeb976ed784a1fbacff9aa0840b989c5563aad0d16082795910616f5c5b6e8a001a907f1918f
@@ -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,11 +25,11 @@ 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)
31
+ check = lambda do |val|
32
+ if not type_check.(type, val)
33
33
  raise Schema::Attribute::TypeError, "#{val.inspect} of type #{val.class.name} cannot be assigned to attribute #{attribute_name.inspect} of #{self.to_s}"
34
34
  end
35
35
  end
@@ -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.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-10-01 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