typ 0.0.5 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/typ.rb +189 -85
  3. metadata +7 -7
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 14188b8d650c32fecbae6ed1ae3c8f03eac9f31effad6a0695c8ec1e77f8bdbe
4
- data.tar.gz: 89524fac30d45f13f8a21f904c22e9890fb3a8d2f59bb61ad53ef5ca6abfb7d4
3
+ metadata.gz: a3d4190e37bd4aadc5b0d529256c441835cdf9f3673cbdb23a1d0b2e583926a7
4
+ data.tar.gz: edc5160b73a35c92c08d27ea5a13998f37e37f47838f4f986dc4c81d69045b72
5
5
  SHA512:
6
- metadata.gz: a08274c0bed76f596da8d0b30b45bfea0679c2ba8ba29b05e478f6ce2fc3366d3110e612a8f0c2b72ac9a0262aeb7391da8a30c4915c62464eeb25b8373b4bd1
7
- data.tar.gz: defa65890558d113926b445b3e5a6161d80f645bd98d0ba5e3d2ff2f602ca62d64ac885378d140dc1d12d3deaec55897a497b28c567226afd506684ef0d2b585
6
+ metadata.gz: 2994b127d25415c59392d1561c7b067bb578a1fc62c4e3936e0c74184222c40621af22003e2fd901ef17671d93eb2db2015eafd34f7450ece569f36066ebf615
7
+ data.tar.gz: 26a90c3294320ee43b78b2c1a4c8ad9939060910b3d5265b400a30889c7e8bff6dd67e1955fb888ad0e41fd0c48e6602ecca62c39b3e4fbbdbfa189e08014230
data/lib/typ.rb CHANGED
@@ -1,123 +1,227 @@
1
1
  module Typ
2
+ module Gate
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
31
+
32
+ def initialize it
33
+ @it = it
34
+ @ok = begin
35
+ check
36
+ rescue => e
37
+ @error = e
38
+ false
39
+ end
40
+ end
41
+
42
+ def check
43
+ end
44
+
45
+ def ok?
46
+ @ok
47
+ end
48
+ end
49
+
2
50
  def self.included mod
3
51
  mod.extend DSL
52
+ mod.extend Singleton
4
53
  end
5
54
 
6
- def initialize it
7
- @it = it
8
- @gates = self.class.gates.map { |gate| gate.new it }
9
- @fails = gates.reject &:ok?
10
- end
55
+ include Gate
11
56
 
12
- attr_reader :it, :gates, :fails
57
+ attr_reader :gates, :fails
13
58
 
14
- def ok?
59
+ def check
60
+ @gates = self.class.gates.map { |gate| gate.new it }
61
+ @fails = gates.reject &:ok?
15
62
  fails.empty?
16
63
  end
17
64
 
18
65
  module DSL
19
- def gates
20
- @gates ||= []
21
- end
22
-
23
- def is type
24
- case type
25
- when Array
26
- gates << Is::Array.new(type)
27
- when Class
28
- if type.include? Typ
29
- gates << type
30
- else
31
- 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
32
76
  end
33
- else
34
- fail "don't know how to create a Gate from #{type}"
35
77
  end
36
- end
37
78
 
38
- def is_a type
39
- case type
40
- when Module
41
- gates << IsA.new(type)
42
- else
43
- fail "don't know how to create a Gate from #{type}"
44
- end
45
- end
46
- end
47
-
48
- module Is
49
-
50
- module Array
51
- class << self
52
- def new array
53
- check = if array[0].is_a?(Symbol)
54
- method, *arguments = array
55
- -> receiver { receiver.send method, *arguments }
56
- elsif array[1].is_a?(Symbol)
57
- receiver, method = array
58
- -> argument { receiver.send method, argument }
59
- else
60
- 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
61
99
  end
62
-
63
- gate = Class.new
64
- gate.include self
65
- gate.check, gate.array = check, array
66
- gate
100
+ end
101
+
102
+ def cannot_create_gate type
103
+ fail Typ::Error::CannotCreateGate.new(type)
104
+ end
105
+
106
+ def bad_assertion it, name
107
+ fail Typ::Error::BadAssertion.new(it, name)
67
108
  end
68
109
  end
69
-
70
- def self.included gate
71
- gate.extend Singleton
110
+
111
+ def initialize method, literal, **kwargs
112
+ @method, @literal, @kwargs = method, literal, kwargs
113
+ make
72
114
  end
73
115
 
74
- module Singleton
75
- attr_accessor :check, :array
116
+ def gate
117
+ @gate ||= Typ::Gate.new
76
118
  end
77
119
 
78
- def to_a
79
- self.class.array
120
+ def make
80
121
  end
81
122
 
82
- def ok?
83
- @ok
84
- end
123
+ include TestFor
124
+ include CheckMethod
85
125
 
86
- def initialize it
87
- @ok = self.class.check[it]
126
+ def enrich_gate
127
+ augment_gate_with_check @test
128
+ gate.dsl_method = @method
129
+ gate.dsl_literal = @literal
88
130
  end
89
131
  end
90
- end
91
-
92
- module IsA
93
- class << self
94
- def new type
95
- check = -> it { it.is_a? type }
96
-
97
- gate = Class.new
98
- gate.include self
99
- gate.type, gate.check = type, check
100
- gate
101
- end
132
+
133
+ def gates
134
+ @gates ||= []
102
135
  end
103
136
 
104
- def self.included gate
105
- gate.extend Singleton
137
+ def is literal
138
+ gates << Is.new(__method__, literal).gate
106
139
  end
107
140
 
108
- module Singleton
109
- attr_accessor :type, :check
141
+ def is_a literal
142
+ gates << IsA.new(__method__, literal).gate
110
143
  end
111
144
 
112
- def ok?
113
- @ok
145
+ def its name, literal
146
+ gates << Its.new(__method__, literal, name: name).gate
114
147
  end
115
148
 
116
- def initialize it
117
- @it = it
118
- @ok = self.class.check[it]
149
+ def key name, literal
150
+ gates << Key.new(__method__, literal, name: name).gate
151
+ end
152
+
153
+ class FetchGate < Gate
154
+ def make
155
+ type_check = test_for @literal
156
+ @test = fetch_check >> type_check
157
+ enrich_gate
158
+ end
159
+ end
160
+
161
+ class Is < Gate
162
+ def make
163
+ @test = test_for @literal
164
+
165
+ if @test
166
+ if Class === @literal
167
+ @gate = @literal
168
+ else
169
+ enrich_gate
170
+ end
171
+ else
172
+ cannot_create_gate @literal
173
+ end
174
+ end
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
200
+ end
201
+ end
202
+
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
222
+ new(it).ok?
119
223
  end
120
224
 
121
- attr_reader :it
225
+ alias_method :===, :call
122
226
  end
123
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.5
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
- - Anatoly Chernow
7
+ - Anatoly Chernov
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-04-30 00:00:00.000000000 Z
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
- rubyforge_project:
39
- rubygems_version: 2.7.6
39
+ rubygems_version: 3.4.15
40
40
  signing_key:
41
41
  specification_version: 4
42
- summary: A tool for defining and enforcing types of Ruby objects.
42
+ summary: To type Ruby objects.
43
43
  test_files: []