typ 0.0.5 → 0.0.6
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/typ.rb +107 -37
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a726dc4e0d96b4f5bdad9858d10063bf818af3e5bd1293135acffa040bbde236
|
4
|
+
data.tar.gz: fbc8d499fb78954604a757593e2e46a9e2af7f5fba4102783d6011aa991a10f3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2d3679f431008960d32befdaea22720e3a3cf49692c7a81f19463476b8cae6d67c8069d3920c1325a3c68d743058a348f2087295219f75bde0261bb780b95cdd
|
7
|
+
data.tar.gz: 20af3243c88900e428928f2c770cb661ee69835187fb0834f55d7be701ab3cfbf0f848b2a854f8998e42cd85291c4551da5b6eaf7ceb7946d63f71aadc823bfb
|
data/lib/typ.rb
CHANGED
@@ -1,18 +1,35 @@
|
|
1
1
|
module Typ
|
2
|
+
module Gate
|
3
|
+
attr_reader :it
|
4
|
+
|
5
|
+
def initialize it
|
6
|
+
@it = it
|
7
|
+
check
|
8
|
+
end
|
9
|
+
|
10
|
+
# Should set @ok to be true or false
|
11
|
+
def check
|
12
|
+
@ok = self.class.check === it
|
13
|
+
end
|
14
|
+
|
15
|
+
def ok?
|
16
|
+
@ok
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
2
20
|
def self.included mod
|
3
21
|
mod.extend DSL
|
22
|
+
mod.extend PatternMatching
|
4
23
|
end
|
5
24
|
|
6
|
-
|
7
|
-
@it = it
|
8
|
-
@gates = self.class.gates.map { |gate| gate.new it }
|
9
|
-
@fails = gates.reject &:ok?
|
10
|
-
end
|
25
|
+
include Gate
|
11
26
|
|
12
|
-
attr_reader :
|
27
|
+
attr_reader :gates, :fails
|
13
28
|
|
14
|
-
def
|
15
|
-
|
29
|
+
def check
|
30
|
+
@gates = self.class.gates.map { |gate| gate.new it }
|
31
|
+
@fails = gates.reject &:ok?
|
32
|
+
@ok = fails.empty?
|
16
33
|
end
|
17
34
|
|
18
35
|
module DSL
|
@@ -35,10 +52,10 @@ module Typ
|
|
35
52
|
end
|
36
53
|
end
|
37
54
|
|
38
|
-
def is_a type
|
55
|
+
def is_a type, params = {}
|
39
56
|
case type
|
40
57
|
when Module
|
41
|
-
gates << IsA.new(type)
|
58
|
+
gates << IsA.new(type, params)
|
42
59
|
else
|
43
60
|
fail "don't know how to create a Gate from #{type}"
|
44
61
|
end
|
@@ -48,21 +65,36 @@ module Typ
|
|
48
65
|
module Is
|
49
66
|
|
50
67
|
module Array
|
51
|
-
class
|
52
|
-
def
|
53
|
-
|
68
|
+
class Check
|
69
|
+
def initialize array
|
70
|
+
@array = array
|
71
|
+
@check = if array[0].is_a?(Symbol)
|
54
72
|
method, *arguments = array
|
55
|
-
->
|
73
|
+
-> it { it.send method, *arguments }
|
56
74
|
elsif array[1].is_a?(Symbol)
|
57
75
|
receiver, method = array
|
58
|
-
->
|
76
|
+
-> it { receiver.send method, it }
|
59
77
|
else
|
60
78
|
fail "not sure how to handle #{array} yet"
|
61
79
|
end
|
80
|
+
end
|
81
|
+
|
82
|
+
def === it
|
83
|
+
@check === it
|
84
|
+
end
|
85
|
+
|
86
|
+
def to_a
|
87
|
+
@array
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
class << self
|
92
|
+
def new array
|
93
|
+
check = Check.new array
|
62
94
|
|
63
95
|
gate = Class.new
|
64
96
|
gate.include self
|
65
|
-
gate.check
|
97
|
+
gate.check = check
|
66
98
|
gate
|
67
99
|
end
|
68
100
|
end
|
@@ -72,31 +104,25 @@ module Typ
|
|
72
104
|
end
|
73
105
|
|
74
106
|
module Singleton
|
75
|
-
attr_accessor :check
|
107
|
+
attr_accessor :check
|
76
108
|
end
|
77
109
|
|
78
110
|
def to_a
|
79
|
-
self.class.
|
80
|
-
end
|
81
|
-
|
82
|
-
def ok?
|
83
|
-
@ok
|
111
|
+
self.class.check.to_a
|
84
112
|
end
|
85
113
|
|
86
|
-
|
87
|
-
@ok = self.class.check[it]
|
88
|
-
end
|
114
|
+
include Gate
|
89
115
|
end
|
90
116
|
end
|
91
117
|
|
92
118
|
module IsA
|
93
119
|
class << self
|
94
|
-
def new type
|
95
|
-
|
120
|
+
def new type, params = {}
|
121
|
+
type = Type.new type, params
|
96
122
|
|
97
123
|
gate = Class.new
|
98
124
|
gate.include self
|
99
|
-
gate.
|
125
|
+
gate.check = type
|
100
126
|
gate
|
101
127
|
end
|
102
128
|
end
|
@@ -106,18 +132,62 @@ module Typ
|
|
106
132
|
end
|
107
133
|
|
108
134
|
module Singleton
|
109
|
-
attr_accessor :
|
135
|
+
attr_accessor :check
|
110
136
|
end
|
111
137
|
|
112
|
-
|
113
|
-
|
138
|
+
include Gate
|
139
|
+
|
140
|
+
class Type
|
141
|
+
def initialize type, params = {}
|
142
|
+
@type, @params = type, params
|
143
|
+
|
144
|
+
@param = params[:of]
|
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
|
159
|
+
end
|
160
|
+
|
161
|
+
def to_s
|
162
|
+
if @params.empty?
|
163
|
+
@type.to_s
|
164
|
+
else
|
165
|
+
"#{@type}, #{@params}"
|
166
|
+
end
|
167
|
+
end
|
168
|
+
|
169
|
+
private
|
170
|
+
def check_Array
|
171
|
+
-> it {
|
172
|
+
it.is_a?(@type) &&
|
173
|
+
it.all? { |element| element.is_a? @param }
|
174
|
+
}
|
175
|
+
end
|
176
|
+
|
177
|
+
def check_Hash
|
178
|
+
key_type, value_type = @params.first
|
179
|
+
-> it {
|
180
|
+
it.is_a?(@type) &&
|
181
|
+
it.keys.all? {|k| k.is_a? key_type } &&
|
182
|
+
it.values.all? {|v| v.is_a? value_type }
|
183
|
+
}
|
184
|
+
end
|
114
185
|
end
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
186
|
+
end
|
187
|
+
|
188
|
+
module PatternMatching
|
189
|
+
def === it
|
190
|
+
new(it).ok?
|
119
191
|
end
|
120
|
-
|
121
|
-
attr_reader :it
|
122
192
|
end
|
123
193
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: typ
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.6
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Anatoly Chernow
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-
|
11
|
+
date: 2018-05-03 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description:
|
14
14
|
email:
|