typ 0.1.0 → 0.1.1

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 +55 -18
  3. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a3d4190e37bd4aadc5b0d529256c441835cdf9f3673cbdb23a1d0b2e583926a7
4
- data.tar.gz: edc5160b73a35c92c08d27ea5a13998f37e37f47838f4f986dc4c81d69045b72
3
+ metadata.gz: 3292265f2d79043b1324f8221a43926863cb039233c48d5d989109fd712a9141
4
+ data.tar.gz: d81287849ec606b02f4710dcc9d86c822ad5f162fda839c23c5a84225fa91718
5
5
  SHA512:
6
- metadata.gz: 2994b127d25415c59392d1561c7b067bb578a1fc62c4e3936e0c74184222c40621af22003e2fd901ef17671d93eb2db2015eafd34f7450ece569f36066ebf615
7
- data.tar.gz: 26a90c3294320ee43b78b2c1a4c8ad9939060910b3d5265b400a30889c7e8bff6dd67e1955fb888ad0e41fd0c48e6602ecca62c39b3e4fbbdbfa189e08014230
6
+ metadata.gz: 4f6fa03315316cf839a3560dfb10197879d673a88c42823ed55a8e4daecd3c66f09a3393fc33b9b240ef9fb9a142a617a46bf03d62ead3dc5b6de662d1b53495
7
+ data.tar.gz: eba82e7d4741e2f1b489f383b4ea0423b24348a218a1e858291e7855e472b3c07c9440bd84769bd63ff8e74f88a113ebbee19ce3ebee8eddbcbf65bddfede41f
data/lib/typ.rb CHANGED
@@ -1,4 +1,35 @@
1
1
  module Typ
2
+ module ArrayPredicate
3
+ refine Array do
4
+ def predicate?
5
+ (size == 2) &&
6
+ (self[0].is_a?(Symbol) || self[1].is_a?(Symbol))
7
+ end
8
+ end
9
+ end
10
+
11
+ module ArrayToTest
12
+ refine Object do
13
+ def bad_assertion it, name
14
+ fail Typ::Error::BadAssertion.new(it, name)
15
+ end
16
+ end
17
+
18
+ refine Array do
19
+ def to_test
20
+ type = self
21
+
22
+ if type[0].is_a? Symbol
23
+ method, argument = type
24
+ -> it { it.send method, argument or bad_assertion(it, type) }
25
+ else
26
+ receiver, method = type
27
+ -> it { receiver.send method, it or bad_assertion(it, type) }
28
+ end
29
+ end
30
+ end
31
+ end
32
+
2
33
  module Gate
3
34
  module DSLReaders
4
35
  def dsl_method
@@ -8,6 +39,10 @@ module Typ
8
39
  def dsl_literal
9
40
  self.class.dsl_literal
10
41
  end
42
+
43
+ def dsl_key
44
+ self.class.dsl_key
45
+ end
11
46
  end
12
47
 
13
48
  module New
@@ -21,7 +56,7 @@ module Typ
21
56
  end
22
57
 
23
58
  module Singleton
24
- attr_accessor :dsl_method, :dsl_literal
59
+ attr_accessor :dsl_method, :dsl_literal, :dsl_key
25
60
  end
26
61
 
27
62
  extend New
@@ -47,6 +82,9 @@ module Typ
47
82
  end
48
83
  end
49
84
 
85
+ using ArrayPredicate
86
+ using ArrayToTest
87
+
50
88
  def self.included mod
51
89
  mod.extend DSL
52
90
  mod.extend Singleton
@@ -82,18 +120,7 @@ module Typ
82
120
  when Symbol
83
121
  -> it { it.send type or bad_assertion(it, type) }
84
122
  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
123
+ type.predicate? ? type.to_test : (cannot_create_gate type)
97
124
  when Class
98
125
  type if type.include? Typ
99
126
  end
@@ -102,10 +129,6 @@ module Typ
102
129
  def cannot_create_gate type
103
130
  fail Typ::Error::CannotCreateGate.new(type)
104
131
  end
105
-
106
- def bad_assertion it, name
107
- fail Typ::Error::BadAssertion.new(it, name)
108
- end
109
132
  end
110
133
 
111
134
  def initialize method, literal, **kwargs
@@ -127,6 +150,7 @@ module Typ
127
150
  augment_gate_with_check @test
128
151
  gate.dsl_method = @method
129
152
  gate.dsl_literal = @literal
153
+ gate.dsl_key = @kwargs[:name]
130
154
  end
131
155
  end
132
156
 
@@ -156,6 +180,19 @@ module Typ
156
180
  @test = fetch_check >> type_check
157
181
  enrich_gate
158
182
  end
183
+
184
+ def test_for literal
185
+ case literal
186
+ when Symbol
187
+ literal.end_with?(??) ? super : (super [:eql?, literal])
188
+ when Array
189
+ literal.predicate? ? literal.to_test : (super [:eql?, literal])
190
+ when Class
191
+ literal.include?(Typ) ? super : (super [:eql?, literal])
192
+ else
193
+ super [:eql?, literal]
194
+ end
195
+ end
159
196
  end
160
197
 
161
198
  class Is < Gate
@@ -204,7 +241,7 @@ module Typ
204
241
 
205
242
  class BadAssertion < self
206
243
  def initialize it, method_name
207
- message = "#{it} is #{method_name}"
244
+ message = "#{it.inspect} is #{method_name}"
208
245
  super message
209
246
  end
210
247
  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.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Anatoly Chernov
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-08-01 00:00:00.000000000 Z
11
+ date: 2023-08-07 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description:
14
14
  email: