typ 0.0.6 → 0.1.0
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 +4 -4
- data/lib/typ.rb +162 -128
- metadata +7 -7
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a3d4190e37bd4aadc5b0d529256c441835cdf9f3673cbdb23a1d0b2e583926a7
|
4
|
+
data.tar.gz: edc5160b73a35c92c08d27ea5a13998f37e37f47838f4f986dc4c81d69045b72
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2994b127d25415c59392d1561c7b067bb578a1fc62c4e3936e0c74184222c40621af22003e2fd901ef17671d93eb2db2015eafd34f7450ece569f36066ebf615
|
7
|
+
data.tar.gz: 26a90c3294320ee43b78b2c1a4c8ad9939060910b3d5265b400a30889c7e8bff6dd67e1955fb888ad0e41fd0c48e6602ecca62c39b3e4fbbdbfa189e08014230
|
data/lib/typ.rb
CHANGED
@@ -1,15 +1,45 @@
|
|
1
1
|
module Typ
|
2
2
|
module Gate
|
3
|
-
|
3
|
+
module DSLReaders
|
4
|
+
def dsl_method
|
5
|
+
self.class.dsl_method
|
6
|
+
end
|
7
|
+
|
8
|
+
def dsl_literal
|
9
|
+
self.class.dsl_literal
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
module New
|
14
|
+
def new
|
15
|
+
c = Class.new
|
16
|
+
c.include Gate
|
17
|
+
c.extend Singleton
|
18
|
+
c
|
19
|
+
end
|
20
|
+
|
21
|
+
end
|
22
|
+
|
23
|
+
module Singleton
|
24
|
+
attr_accessor :dsl_method, :dsl_literal
|
25
|
+
end
|
26
|
+
|
27
|
+
extend New
|
28
|
+
include DSLReaders
|
29
|
+
|
30
|
+
attr_reader :it, :error
|
4
31
|
|
5
32
|
def initialize it
|
6
33
|
@it = it
|
7
|
-
|
34
|
+
@ok = begin
|
35
|
+
check
|
36
|
+
rescue => e
|
37
|
+
@error = e
|
38
|
+
false
|
39
|
+
end
|
8
40
|
end
|
9
41
|
|
10
|
-
# Should set @ok to be true or false
|
11
42
|
def check
|
12
|
-
@ok = self.class.check === it
|
13
43
|
end
|
14
44
|
|
15
45
|
def ok?
|
@@ -19,7 +49,7 @@ module Typ
|
|
19
49
|
|
20
50
|
def self.included mod
|
21
51
|
mod.extend DSL
|
22
|
-
mod.extend
|
52
|
+
mod.extend Singleton
|
23
53
|
end
|
24
54
|
|
25
55
|
include Gate
|
@@ -29,165 +59,169 @@ module Typ
|
|
29
59
|
def check
|
30
60
|
@gates = self.class.gates.map { |gate| gate.new it }
|
31
61
|
@fails = gates.reject &:ok?
|
32
|
-
|
62
|
+
fails.empty?
|
33
63
|
end
|
34
64
|
|
35
65
|
module DSL
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
gates << type
|
47
|
-
else
|
48
|
-
fail "don't know how to create a Gate from #{type}"
|
66
|
+
class Gate
|
67
|
+
module CheckMethod
|
68
|
+
def augment_gate_with_check test
|
69
|
+
check = Module.new do
|
70
|
+
define_method :check do
|
71
|
+
test[it]
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
gate.include check
|
49
76
|
end
|
50
|
-
else
|
51
|
-
fail "don't know how to create a Gate from #{type}"
|
52
77
|
end
|
53
|
-
end
|
54
78
|
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
receiver, method = array
|
76
|
-
-> it { receiver.send method, it }
|
77
|
-
else
|
78
|
-
fail "not sure how to handle #{array} yet"
|
79
|
+
module TestFor
|
80
|
+
def test_for type
|
81
|
+
case type
|
82
|
+
when Symbol
|
83
|
+
-> it { it.send type or bad_assertion(it, type) }
|
84
|
+
when Array
|
85
|
+
cannot_create_gate type unless type.size == 2
|
86
|
+
unless type[0].is_a?(Symbol) || type[1].is_a?(Symbol)
|
87
|
+
cannot_create_gate type
|
88
|
+
end
|
89
|
+
|
90
|
+
if type[0].is_a? Symbol
|
91
|
+
method, argument = type
|
92
|
+
-> it { it.send method, argument or bad_assertion(it, type) }
|
93
|
+
else
|
94
|
+
receiver, method = type
|
95
|
+
-> it { receiver.send method, it or bad_assertion(it, type) }
|
96
|
+
end
|
97
|
+
when Class
|
98
|
+
type if type.include? Typ
|
79
99
|
end
|
80
100
|
end
|
81
|
-
|
82
|
-
def
|
83
|
-
|
101
|
+
|
102
|
+
def cannot_create_gate type
|
103
|
+
fail Typ::Error::CannotCreateGate.new(type)
|
84
104
|
end
|
85
|
-
|
86
|
-
def
|
87
|
-
|
105
|
+
|
106
|
+
def bad_assertion it, name
|
107
|
+
fail Typ::Error::BadAssertion.new(it, name)
|
88
108
|
end
|
89
109
|
end
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
gate = Class.new
|
96
|
-
gate.include self
|
97
|
-
gate.check = check
|
98
|
-
gate
|
99
|
-
end
|
110
|
+
|
111
|
+
def initialize method, literal, **kwargs
|
112
|
+
@method, @literal, @kwargs = method, literal, kwargs
|
113
|
+
make
|
100
114
|
end
|
101
115
|
|
102
|
-
def
|
103
|
-
gate.
|
116
|
+
def gate
|
117
|
+
@gate ||= Typ::Gate.new
|
104
118
|
end
|
105
119
|
|
106
|
-
|
107
|
-
attr_accessor :check
|
120
|
+
def make
|
108
121
|
end
|
109
122
|
|
110
|
-
|
111
|
-
|
112
|
-
end
|
123
|
+
include TestFor
|
124
|
+
include CheckMethod
|
113
125
|
|
114
|
-
|
126
|
+
def enrich_gate
|
127
|
+
augment_gate_with_check @test
|
128
|
+
gate.dsl_method = @method
|
129
|
+
gate.dsl_literal = @literal
|
130
|
+
end
|
131
|
+
end
|
132
|
+
|
133
|
+
def gates
|
134
|
+
@gates ||= []
|
115
135
|
end
|
116
|
-
end
|
117
|
-
|
118
|
-
module IsA
|
119
|
-
class << self
|
120
|
-
def new type, params = {}
|
121
|
-
type = Type.new type, params
|
122
136
|
|
123
|
-
|
124
|
-
|
125
|
-
gate.check = type
|
126
|
-
gate
|
127
|
-
end
|
137
|
+
def is literal
|
138
|
+
gates << Is.new(__method__, literal).gate
|
128
139
|
end
|
129
140
|
|
130
|
-
def
|
131
|
-
|
141
|
+
def is_a literal
|
142
|
+
gates << IsA.new(__method__, literal).gate
|
132
143
|
end
|
133
144
|
|
134
|
-
|
135
|
-
|
145
|
+
def its name, literal
|
146
|
+
gates << Its.new(__method__, literal, name: name).gate
|
136
147
|
end
|
137
148
|
|
138
|
-
|
149
|
+
def key name, literal
|
150
|
+
gates << Key.new(__method__, literal, name: name).gate
|
151
|
+
end
|
139
152
|
|
140
|
-
class
|
141
|
-
def
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
@check = if @params.empty?
|
147
|
-
-> it { it.is_a? @type }
|
148
|
-
else
|
149
|
-
if @param
|
150
|
-
check_Array
|
151
|
-
else
|
152
|
-
check_Hash
|
153
|
-
end
|
154
|
-
end
|
155
|
-
end
|
156
|
-
|
157
|
-
def === it
|
158
|
-
@check === it
|
153
|
+
class FetchGate < Gate
|
154
|
+
def make
|
155
|
+
type_check = test_for @literal
|
156
|
+
@test = fetch_check >> type_check
|
157
|
+
enrich_gate
|
159
158
|
end
|
159
|
+
end
|
160
|
+
|
161
|
+
class Is < Gate
|
162
|
+
def make
|
163
|
+
@test = test_for @literal
|
160
164
|
|
161
|
-
|
162
|
-
|
163
|
-
|
165
|
+
if @test
|
166
|
+
if Class === @literal
|
167
|
+
@gate = @literal
|
168
|
+
else
|
169
|
+
enrich_gate
|
170
|
+
end
|
164
171
|
else
|
165
|
-
|
172
|
+
cannot_create_gate @literal
|
166
173
|
end
|
167
174
|
end
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
175
|
+
end
|
176
|
+
|
177
|
+
class IsA < Gate
|
178
|
+
def make
|
179
|
+
@test = test_for [:is_a?, @literal]
|
180
|
+
enrich_gate
|
181
|
+
end
|
182
|
+
end
|
183
|
+
|
184
|
+
class Its < FetchGate
|
185
|
+
def fetch_check
|
186
|
+
@fetch_check ||= begin
|
187
|
+
name = @kwargs[:name]
|
188
|
+
-> it { it.send name }
|
189
|
+
end
|
190
|
+
end
|
191
|
+
end
|
192
|
+
|
193
|
+
class Key < FetchGate
|
194
|
+
def fetch_check
|
195
|
+
@fetch_check ||= begin
|
196
|
+
name = @kwargs[:name]
|
197
|
+
-> it { it[name] }
|
198
|
+
end
|
199
|
+
end
|
185
200
|
end
|
186
201
|
end
|
187
202
|
|
188
|
-
|
189
|
-
|
203
|
+
class Error < StandardError
|
204
|
+
|
205
|
+
class BadAssertion < self
|
206
|
+
def initialize it, method_name
|
207
|
+
message = "#{it} is #{method_name}"
|
208
|
+
super message
|
209
|
+
end
|
210
|
+
end
|
211
|
+
|
212
|
+
class CannotCreateGate < self
|
213
|
+
def initialize object
|
214
|
+
message = "Don't know how to create a gate from #{object.inspect}:#{object.class}."
|
215
|
+
super message
|
216
|
+
end
|
217
|
+
end
|
218
|
+
end
|
219
|
+
|
220
|
+
module Singleton
|
221
|
+
def call it
|
190
222
|
new(it).ok?
|
191
223
|
end
|
224
|
+
|
225
|
+
alias_method :===, :call
|
192
226
|
end
|
193
227
|
end
|
metadata
CHANGED
@@ -1,17 +1,18 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: typ
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
|
-
- Anatoly
|
7
|
+
- Anatoly Chernov
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2023-08-01 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description:
|
14
|
-
email:
|
14
|
+
email:
|
15
|
+
- chertoly@gmail.com
|
15
16
|
executables: []
|
16
17
|
extensions: []
|
17
18
|
extra_rdoc_files: []
|
@@ -35,9 +36,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
35
36
|
- !ruby/object:Gem::Version
|
36
37
|
version: '0'
|
37
38
|
requirements: []
|
38
|
-
|
39
|
-
rubygems_version: 2.7.6
|
39
|
+
rubygems_version: 3.4.15
|
40
40
|
signing_key:
|
41
41
|
specification_version: 4
|
42
|
-
summary:
|
42
|
+
summary: To type Ruby objects.
|
43
43
|
test_files: []
|