trafaret 1.5.4 → 1.5.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.rst +34 -1
- data/lib/trafaret/constructor.rb +1 -1
- data/lib/trafaret/errors.rb +11 -0
- data/lib/trafaret/numeric.rb +4 -0
- data/lib/trafaret/validators.rb +3 -3
- data/lib/trafaret/version.rb +1 -1
- 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: 705d3489ea9f5859edca572044700d365e8a6db4
|
4
|
+
data.tar.gz: d0644a20d15ca0e535540f9368af10ee98d3b135
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 75c2f71ea8c22759ca1d7e54b6036891b0ef3515404a57f4e9a5bd0239ac3b44502e493c40c008379e6abba6bea740356263b2f9022d5ca7ee5d4c3287613938
|
7
|
+
data.tar.gz: 3980d87341dd76854286494eacf7795dc1ece42eda01168f3873128ed7dd9bf36d895532f6a51b151038a80bb411a06dcc25a892c4ff15f72bd13e52d6f90bb7
|
data/README.rst
CHANGED
@@ -26,10 +26,43 @@ You can attach converter to trafaret with ``to``::
|
|
26
26
|
|
27
27
|
T.string(regex: /\A\d+\Z/).to { |match| match.string.to_i }.call('123')
|
28
28
|
|
29
|
+
T.string(regex: /\A\d+\Z/).to(&:string).to(&:to_i).call('123')
|
30
|
+
|
29
31
|
Any callable can be used as ``Trafaret`` while it use simple convention. If data ok, you return something, if it wrong
|
30
32
|
you must return ``Trafaret::Error`` instance with message. Correct message can be a string or a Hash with simple keys and Errors values::
|
31
33
|
|
32
34
|
irb> (T.string & T.proc { |data| data == 'karramba' ? 'Bart' : T.failure('Not a Bart text!')}).call ('ku')
|
33
35
|
=> #<Trafaret::Error("Not a Bart text!")>
|
34
36
|
irb> (T.string & T.proc { |data| data == 'karramba' ? 'Bart' : T.failure('Not a Bart text!')}).call ('karramba')
|
35
|
-
=> "Bart"
|
37
|
+
=> "Bart"
|
38
|
+
|
39
|
+
Numeric
|
40
|
+
-------
|
41
|
+
|
42
|
+
Two trafarets Integer and Float supports common interface. In options this is parameters `lt`, `lte`, `gt`, `gte`.
|
43
|
+
|
44
|
+
Example::
|
45
|
+
|
46
|
+
T.integer(gt: 3, lt: 5).call(4) == 4
|
47
|
+
T.float(gt:3, lt: 5).call(4.3) == 4.3
|
48
|
+
|
49
|
+
String
|
50
|
+
------
|
51
|
+
|
52
|
+
Parameters `allow_blank`, `min_length`, `max_length`. And special option `regex`.
|
53
|
+
|
54
|
+
Example::
|
55
|
+
|
56
|
+
T.string.call('kuku') == 'kuku'
|
57
|
+
T.string(regex: /\Akuku\z/).call('kuku') == 'kuku'
|
58
|
+
|
59
|
+
If you use custom converter block, you will get `Match` instead of `String`, so you can use regex result::
|
60
|
+
|
61
|
+
T.string(regex: /\Ayear=(\d+),month=(\d+),day=(\d+)\z/).to {|m| Date.new(*m.to_a[1..3].map(&:to_i)) }.call('year=2012,month=5,day=4').to_s == '2012-05-04'
|
62
|
+
|
63
|
+
Array
|
64
|
+
-----
|
65
|
+
|
66
|
+
Get one important parameter `validator` that will be applied to every array element::
|
67
|
+
|
68
|
+
T.array(validator: :integer).call(['1','2','3']) == [1,2,3]
|
data/lib/trafaret/constructor.rb
CHANGED
data/lib/trafaret/errors.rb
CHANGED
@@ -9,5 +9,16 @@ module Trafaret
|
|
9
9
|
def inspect
|
10
10
|
"#<Trafaret::Error(#{message.inspect})>"
|
11
11
|
end
|
12
|
+
|
13
|
+
def dump
|
14
|
+
case @message
|
15
|
+
when ::Hash
|
16
|
+
Hash[@message.map { |k, v| [k, v.dump] }]
|
17
|
+
when ::Array
|
18
|
+
@message.map &:dump
|
19
|
+
else
|
20
|
+
@message
|
21
|
+
end
|
22
|
+
end
|
12
23
|
end
|
13
24
|
end
|
data/lib/trafaret/numeric.rb
CHANGED
data/lib/trafaret/validators.rb
CHANGED
@@ -5,9 +5,9 @@ module Trafaret
|
|
5
5
|
class ADT < Validator
|
6
6
|
attr_reader :validators
|
7
7
|
|
8
|
-
def
|
9
|
-
args = args.first if args.first.is_a? ::Array
|
10
|
-
@validators = args.map { |v| Trafaret.get_instantiated_validator(v) }
|
8
|
+
def prepare(*args)
|
9
|
+
@args = @args.first if @args.first.is_a? ::Array
|
10
|
+
@validators = @args.map { |v| Trafaret.get_instantiated_validator(v) }
|
11
11
|
end
|
12
12
|
end
|
13
13
|
|
data/lib/trafaret/version.rb
CHANGED
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.5
|
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-19 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|