trafaret 1.5.5 → 1.5.6
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/README.rst +34 -1
- data/lib/trafaret/numeric.rb +5 -5
- data/lib/trafaret/validator.rb +4 -0
- data/lib/trafaret/validators.rb +45 -0
- data/lib/trafaret/version.rb +1 -1
- data/spec/trafaret_spec.rb +40 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a287cdfb38aa84321f68a1989564a6491277ea01
|
4
|
+
data.tar.gz: 66fe38066bd25cc4ebfd96136b93d7199a4200ec
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ff3884c959180136768c903dd9a15051054596af68324e4086862a9527ee90116115898c08559f1bb1642a8fc9a7f0924477dc2c6c99553d548af7b1169fb338
|
7
|
+
data.tar.gz: 777a037a908f5da2d94c5663be10acd750440016a4554ef43b2bc5f1e8a3a0c56180229911aa1172a613aa56b4a48a397c0583ff437881a60320804526eb1464
|
data/README.rst
CHANGED
@@ -65,4 +65,37 @@ Array
|
|
65
65
|
|
66
66
|
Get one important parameter `validator` that will be applied to every array element::
|
67
67
|
|
68
|
-
T.array(validator: :integer).call(['1','2','3']) == [1,2,3]
|
68
|
+
T.array(validator: :integer).call(['1','2','3']) == [1,2,3]
|
69
|
+
|
70
|
+
Case
|
71
|
+
----
|
72
|
+
|
73
|
+
You can use Ruby case with trafarets, but this have not much sense::
|
74
|
+
|
75
|
+
case 123
|
76
|
+
when T.integer
|
77
|
+
:integer
|
78
|
+
else
|
79
|
+
:any
|
80
|
+
end
|
81
|
+
|
82
|
+
And you can use `Trafaret::Case` that puts result of trafaret to when clause::
|
83
|
+
|
84
|
+
cs = T.case do |c|
|
85
|
+
c.when(T.integer) { |r| :int }
|
86
|
+
c.when(T.string) { |r| :string }
|
87
|
+
c.when(T.nil) { |r| :nil }
|
88
|
+
end
|
89
|
+
|
90
|
+
Tuple
|
91
|
+
-----
|
92
|
+
|
93
|
+
Tuple is Array that consists not from any number of similar elements, but from exact number of different ones.
|
94
|
+
`[1,2,3]` - Array of ints.
|
95
|
+
`[1, 'a', nil]` - Tuple.
|
96
|
+
|
97
|
+
Example::
|
98
|
+
|
99
|
+
t = T.tuple(:integer, :string, :nil)
|
100
|
+
t.call([1, 'a', nil]) == [1, 'a', nil]
|
101
|
+
t.call([1, 'a', 3]).dump == {2 => 'Value must be nil'} # Error dumped to pure structures
|
data/lib/trafaret/numeric.rb
CHANGED
@@ -19,11 +19,11 @@ module Trafaret
|
|
19
19
|
return failure('Too big') if @options[:lte] && val > @options[:lte]
|
20
20
|
return failure('Too small') if @options[:gt] && val <= @options[:gt]
|
21
21
|
return failure('Too small') if @options[:gte] && val < @options[:gte]
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
22
|
+
if @converters.empty?
|
23
|
+
val
|
24
|
+
else
|
25
|
+
data
|
26
|
+
end
|
27
27
|
end
|
28
28
|
end
|
29
29
|
|
data/lib/trafaret/validator.rb
CHANGED
data/lib/trafaret/validators.rb
CHANGED
@@ -153,4 +153,49 @@ module Trafaret
|
|
153
153
|
@blk.call(data)
|
154
154
|
end
|
155
155
|
end
|
156
|
+
|
157
|
+
class Case < Validator
|
158
|
+
def prepare
|
159
|
+
@whens = []
|
160
|
+
@converters.shift.call(self)
|
161
|
+
end
|
162
|
+
|
163
|
+
def when(trafaret, &blk)
|
164
|
+
@whens << [Trafaret::Constructor.construct_from(trafaret), blk]
|
165
|
+
end
|
166
|
+
|
167
|
+
def call(data)
|
168
|
+
@whens.each do |trafaret, blk|
|
169
|
+
val = trafaret.call(data)
|
170
|
+
unless val.is_a?(Trafaret::Error)
|
171
|
+
return blk.call(val)
|
172
|
+
end
|
173
|
+
end
|
174
|
+
failure('Case does not match')
|
175
|
+
end
|
176
|
+
end
|
177
|
+
|
178
|
+
class Tuple < Validator
|
179
|
+
def prepare
|
180
|
+
@validators = @args.map { |arg| Trafaret.get_instantiated_validator(arg) }
|
181
|
+
@size = @validators.size
|
182
|
+
end
|
183
|
+
|
184
|
+
def call(data)
|
185
|
+
return failure('Too many elements') if data.size > @size# && !@extra
|
186
|
+
return failure('Not enough elements') if data.size < @size
|
187
|
+
failures = {}
|
188
|
+
result = []
|
189
|
+
data[0..@size].each.with_index do |el, index|
|
190
|
+
val = @validators[index].call(el)
|
191
|
+
result << val
|
192
|
+
failures[index] = val if val.is_a? Trafaret::Error
|
193
|
+
end
|
194
|
+
if failures.empty?
|
195
|
+
result
|
196
|
+
else
|
197
|
+
failure(failures)
|
198
|
+
end
|
199
|
+
end
|
200
|
+
end
|
156
201
|
end
|
data/lib/trafaret/version.rb
CHANGED
data/spec/trafaret_spec.rb
CHANGED
@@ -142,4 +142,44 @@ describe Trafaret::Nil do
|
|
142
142
|
n.call(123).message.should == 'Value must be nil'
|
143
143
|
n.call(nil).should == nil
|
144
144
|
end
|
145
|
+
end
|
146
|
+
|
147
|
+
describe Trafaret::Validator do
|
148
|
+
it 'must work with case' do
|
149
|
+
caser = proc do |a|
|
150
|
+
case a
|
151
|
+
when T.integer
|
152
|
+
:int
|
153
|
+
when T.symbol(:k)
|
154
|
+
:symbol
|
155
|
+
else
|
156
|
+
:any
|
157
|
+
end
|
158
|
+
end
|
159
|
+
caser.call(123).should == :int
|
160
|
+
caser.call(:k).should == :symbol
|
161
|
+
caser.call('asd').should == :any
|
162
|
+
end
|
163
|
+
end
|
164
|
+
|
165
|
+
describe Trafaret::Case do
|
166
|
+
it 'must work' do
|
167
|
+
cs = T.case do |c|
|
168
|
+
c.when(T.integer) { |r| :int }
|
169
|
+
c.when(T.string) { |r| :string }
|
170
|
+
c.when(T.nil) { |r| :nil }
|
171
|
+
end
|
172
|
+
cs.call(123).should == :int
|
173
|
+
cs.call('123').should == :int
|
174
|
+
cs.call('abs').should == :string
|
175
|
+
cs.call(nil).should == :nil
|
176
|
+
end
|
177
|
+
end
|
178
|
+
|
179
|
+
describe Trafaret::Tuple do
|
180
|
+
it 'must work' do
|
181
|
+
t = T.tuple(:integer, :string, :nil)
|
182
|
+
t.call([1, 'a', nil]).should == [1, 'a', nil]
|
183
|
+
t.call([1, 'a', 3]).dump.should == {2 => 'Value must be nil'}
|
184
|
+
end
|
145
185
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: trafaret
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.5.
|
4
|
+
version: 1.5.6
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Mikhail Krivushin
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-04-
|
11
|
+
date: 2014-04-20 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|