trafaret 1.5.7 → 1.5.9
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.rst +22 -8
- data/lib/trafaret.rb +1 -1
- data/lib/trafaret/constructor.rb +2 -2
- data/lib/trafaret/errors.rb +1 -1
- data/lib/trafaret/{base.rb → hash.rb} +8 -7
- data/lib/trafaret/validator.rb +1 -1
- data/lib/trafaret/validators.rb +15 -2
- data/lib/trafaret/version.rb +1 -1
- data/spec/trafaret_spec.rb +27 -9
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1b74d49954330af927d82d0761fa42341afeb8e7
|
4
|
+
data.tar.gz: 05b550a4a635b52256fd7f346b2abab7035bef77
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3da2249c3b0e1963fd11badd4c7164151f83c5d2a1f950d6fb831b637ffedae9cac81b8fb032ae66d3a482cd806f1ae36cbe9c71fdd9b634d95ae72860d2610b
|
7
|
+
data.tar.gz: bbdd76be5e061c0d002844849ccbb8a30ca2f322c36a511ff12c75015a518685a1287671f4c02e0b2241dd0a48b48926695da169d7c32bba169f7ceee444b44a
|
data/README.rst
CHANGED
@@ -39,7 +39,7 @@ you must return ``Trafaret::Error`` instance with message. Correct message can b
|
|
39
39
|
Numeric
|
40
40
|
-------
|
41
41
|
|
42
|
-
Two trafarets Integer and Float supports common interface. In options this is parameters
|
42
|
+
Two trafarets ``Integer`` and ``Float`` supports common interface. In options this is parameters ``lt``, ``lte``, ``gt``, ``gte``.
|
43
43
|
|
44
44
|
Example::
|
45
45
|
|
@@ -49,21 +49,21 @@ Example::
|
|
49
49
|
String
|
50
50
|
------
|
51
51
|
|
52
|
-
Parameters
|
52
|
+
Parameters ``allow_blank``, ``min_length``, ``max_length``. And special option ``regex``.
|
53
53
|
|
54
54
|
Example::
|
55
55
|
|
56
56
|
T.string.call('kuku') == 'kuku'
|
57
57
|
T.string(regex: /\Akuku\z/).call('kuku') == 'kuku'
|
58
58
|
|
59
|
-
If you use custom converter block, you will get
|
59
|
+
If you use custom converter block, you will get ``Match`` instead of ``String``, so you can use regex result::
|
60
60
|
|
61
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
62
|
|
63
63
|
URI
|
64
64
|
---
|
65
65
|
|
66
|
-
URI parses URI. Parameter
|
66
|
+
URI parses URI. Parameter ``schemes``, by default == ['http', 'https']::
|
67
67
|
|
68
68
|
t = T.uri(schemes: ['ftp'])
|
69
69
|
t.call('ftp://ftp.ueaysuc.co.uk.edu') == 'ftp://ftp.ueaysuc.co.uk.edu'
|
@@ -80,7 +80,7 @@ Now just checks simple regexp::
|
|
80
80
|
Array
|
81
81
|
-----
|
82
82
|
|
83
|
-
Get one important parameter
|
83
|
+
Get one important parameter ``validator`` that will be applied to every array element::
|
84
84
|
|
85
85
|
T.array(validator: :integer).call(['1','2','3']) == [1,2,3]
|
86
86
|
|
@@ -96,7 +96,7 @@ You can use Ruby case with trafarets, but this have not much sense::
|
|
96
96
|
:any
|
97
97
|
end
|
98
98
|
|
99
|
-
And you can use
|
99
|
+
And you can use ``Trafaret::Case`` that puts result of trafaret to when clause::
|
100
100
|
|
101
101
|
cs = T.case do |c|
|
102
102
|
c.when(T.integer) { |r| :int }
|
@@ -108,11 +108,25 @@ Tuple
|
|
108
108
|
-----
|
109
109
|
|
110
110
|
Tuple is Array that consists not from any number of similar elements, but from exact number of different ones.
|
111
|
-
|
112
|
-
|
111
|
+
``[1,2,3]`` - Array of ints.
|
112
|
+
``[1, 'a', nil]`` - Tuple.
|
113
113
|
|
114
114
|
Example::
|
115
115
|
|
116
116
|
t = T.tuple(:integer, :string, :nil)
|
117
117
|
t.call([1, 'a', nil]) == [1, 'a', nil]
|
118
118
|
t.call([1, 'a', 3]).dump == {2 => 'Value must be nil'} # Error dumped to pure structures
|
119
|
+
|
120
|
+
Hash
|
121
|
+
----
|
122
|
+
|
123
|
+
Hashes work in pair with ``Key``'s::
|
124
|
+
|
125
|
+
T::Hash.new(keys: [T.key(:field_name, validator: T.string)])
|
126
|
+
|
127
|
+
Is not too appeal, but Keys are powerful and we have sugar::
|
128
|
+
|
129
|
+
T.construct(
|
130
|
+
kuku: :integer,
|
131
|
+
T.key(:opt_field, optional: true) => T.integer
|
132
|
+
)
|
data/lib/trafaret.rb
CHANGED
data/lib/trafaret/constructor.rb
CHANGED
@@ -23,13 +23,13 @@ module Trafaret
|
|
23
23
|
params.each do |k, v|
|
24
24
|
v = Trafaret::Constructor.construct_from v
|
25
25
|
if k.is_a? ::Symbol
|
26
|
-
keys << Key.new(k, v)
|
26
|
+
keys << Key.new(k, validator: v)
|
27
27
|
elsif k.is_a? Trafaret::Key
|
28
28
|
k.set_validator(v)
|
29
29
|
keys << k
|
30
30
|
end
|
31
31
|
end
|
32
|
-
Trafaret::
|
32
|
+
Trafaret::Hash.new keys: keys
|
33
33
|
end
|
34
34
|
|
35
35
|
def from_array(params)
|
data/lib/trafaret/errors.rb
CHANGED
@@ -1,15 +1,16 @@
|
|
1
1
|
module Trafaret
|
2
2
|
class Key
|
3
|
-
def initialize(name,
|
3
|
+
def initialize(name, options = {}, &blk)
|
4
4
|
@name = name
|
5
5
|
@sname = name.to_s
|
6
6
|
|
7
7
|
@optional = options.delete(:optional)
|
8
8
|
@default = options.delete(:default)
|
9
9
|
@to_name = options.delete(:to_name) || name
|
10
|
+
validator = options.delete(:validator)
|
10
11
|
@options = options
|
11
12
|
|
12
|
-
set_validator(validator, options, &blk)
|
13
|
+
set_validator(validator, options, &blk) if validator
|
13
14
|
end
|
14
15
|
|
15
16
|
def set_validator(validator, options = {}, &blk)
|
@@ -49,7 +50,7 @@ module Trafaret
|
|
49
50
|
end
|
50
51
|
end
|
51
52
|
|
52
|
-
class
|
53
|
+
class Hash < Validator
|
53
54
|
module ClassMethods
|
54
55
|
attr_accessor :keys
|
55
56
|
def inherited(base)
|
@@ -57,7 +58,7 @@ module Trafaret
|
|
57
58
|
end
|
58
59
|
|
59
60
|
def key(name, validator, options = {}, &blk)
|
60
|
-
@keys << Key.new(name, validator
|
61
|
+
@keys << Key.new(name, options.merge(validator: validator), &blk)
|
61
62
|
end
|
62
63
|
end
|
63
64
|
extend ClassMethods
|
@@ -70,7 +71,7 @@ module Trafaret
|
|
70
71
|
end
|
71
72
|
|
72
73
|
def key(name, validator, options = {}, &blk)
|
73
|
-
@keys << Key.new(name, validator, options, &blk)
|
74
|
+
@keys << Key.new(name, validator, options.merge(validator: validator), &blk)
|
74
75
|
end
|
75
76
|
|
76
77
|
def validate(data)
|
@@ -87,9 +88,9 @@ module Trafaret
|
|
87
88
|
end
|
88
89
|
end
|
89
90
|
if fails.blank?
|
90
|
-
Hash[res]
|
91
|
+
::Hash[res]
|
91
92
|
else
|
92
|
-
failure(Hash[fails])
|
93
|
+
failure(::Hash[fails])
|
93
94
|
end
|
94
95
|
end
|
95
96
|
end
|
data/lib/trafaret/validator.rb
CHANGED
data/lib/trafaret/validators.rb
CHANGED
@@ -44,6 +44,19 @@ module Trafaret
|
|
44
44
|
end
|
45
45
|
end
|
46
46
|
|
47
|
+
class Forward < Validator
|
48
|
+
attr_accessor :validator
|
49
|
+
|
50
|
+
def provide(validator)
|
51
|
+
@validator = validator
|
52
|
+
end
|
53
|
+
|
54
|
+
def validate(data)
|
55
|
+
raise 'Validator is not provided' unless validator
|
56
|
+
validator.validate(data)
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
47
60
|
class Nil < Validator
|
48
61
|
def validate(data)
|
49
62
|
failure('Value must be nil') unless data.nil?
|
@@ -140,9 +153,9 @@ module Trafaret
|
|
140
153
|
end
|
141
154
|
end
|
142
155
|
if fails.blank?
|
143
|
-
Hash[pairs]
|
156
|
+
::Hash[pairs]
|
144
157
|
else
|
145
|
-
failure(Hash[fails])
|
158
|
+
failure(::Hash[fails])
|
146
159
|
end
|
147
160
|
end
|
148
161
|
end
|
data/lib/trafaret/version.rb
CHANGED
data/spec/trafaret_spec.rb
CHANGED
@@ -3,11 +3,11 @@ require 'trafaret'
|
|
3
3
|
|
4
4
|
T = Trafaret
|
5
5
|
|
6
|
-
class FacebookResponseTrafaret < Trafaret::
|
6
|
+
class FacebookResponseTrafaret < Trafaret::Hash
|
7
7
|
key :name, T.string(min_length: 2), optional: true
|
8
8
|
key :oa, T.mapping(T.string,
|
9
9
|
T.mapping(T.string,
|
10
|
-
T
|
10
|
+
T[:hash, keys: [T.key(:url, validator: T.string)]]
|
11
11
|
)
|
12
12
|
), to_name: :providers do |data| # so we have response that matches our assumptions, then we can convert it
|
13
13
|
data.flat_map { |p_n, accs| accs.map { |uuid, data| data } }
|
@@ -16,7 +16,7 @@ class FacebookResponseTrafaret < Trafaret::Base
|
|
16
16
|
key :optional_key, :string, optional: true
|
17
17
|
end
|
18
18
|
|
19
|
-
describe Trafaret::
|
19
|
+
describe Trafaret::Hash do
|
20
20
|
let :raw do
|
21
21
|
{'name' => 'kuku',
|
22
22
|
'oa' => {
|
@@ -34,7 +34,7 @@ describe Trafaret::Base do
|
|
34
34
|
it 'should work with hash' do
|
35
35
|
t = T.construct({
|
36
36
|
kuku: :integer,
|
37
|
-
T.key(:krkr,
|
37
|
+
T.key(:krkr, to_name: :id) => :string,
|
38
38
|
hash: {
|
39
39
|
karma: :integer
|
40
40
|
},
|
@@ -48,6 +48,14 @@ describe Trafaret::Base do
|
|
48
48
|
res[:hash][:karma].should == 234
|
49
49
|
res[:proc_].should == 123
|
50
50
|
end
|
51
|
+
|
52
|
+
it 'should work with optional keys' do
|
53
|
+
t = T.construct({
|
54
|
+
T.key(:abd, optional: true) => :string
|
55
|
+
})
|
56
|
+
t.call({}).should == {}
|
57
|
+
t.call({'abd' => 'abc'}).should == {abd: 'abc'}
|
58
|
+
end
|
51
59
|
end
|
52
60
|
|
53
61
|
describe Trafaret::String do
|
@@ -116,12 +124,12 @@ end
|
|
116
124
|
|
117
125
|
describe Trafaret::Key do
|
118
126
|
it 'should extract and check value' do
|
119
|
-
T.key(:name, :string).call({name: 'cow'}).should == [:name, 'cow']
|
120
|
-
T.key(:name, :string, default: 'Elephant').call({}).should == [:name, 'Elephant']
|
121
|
-
T.key(:name, :string, optional: true).call({}).should == nil
|
127
|
+
T.key(:name, validator: :string).call({name: 'cow'}).should == [:name, 'cow']
|
128
|
+
T.key(:name, validator: :string, default: 'Elephant').call({}).should == [:name, 'Elephant']
|
129
|
+
T.key(:name, validator: :string, optional: true).call({}).should == nil
|
122
130
|
# to name test
|
123
|
-
T.key(:name, :string, to_name: :id).call({name: '123'}).should == [:id, '123']
|
124
|
-
T.key(:name, :string, to_name: :id).call({name: 123})[0].should == :name
|
131
|
+
T.key(:name, validator: :string, to_name: :id).call({name: '123'}).should == [:id, '123']
|
132
|
+
T.key(:name, validator: :string, to_name: :id).call({name: 123})[0].should == :name
|
125
133
|
end
|
126
134
|
end
|
127
135
|
|
@@ -197,4 +205,14 @@ describe Trafaret::Email do
|
|
197
205
|
e = T.email.to { |m| m[:name] }
|
198
206
|
e.call('kuku@gmail.com').should == 'kuku'
|
199
207
|
end
|
208
|
+
end
|
209
|
+
|
210
|
+
describe Trafaret::Forward do
|
211
|
+
it 'should provide recursive' do
|
212
|
+
fwd = Trafaret.forward
|
213
|
+
v = T.construct(T.key(:child, optional: true) => fwd, payload: :string)
|
214
|
+
fwd.provide v
|
215
|
+
res = v.call({child: {child: {payload: 'kuku'}, payload: '123'}, payload: '321'})
|
216
|
+
res[:child][:child][:payload].should == 'kuku'
|
217
|
+
end
|
200
218
|
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.9
|
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-
|
11
|
+
date: 2014-08-06 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -67,9 +67,9 @@ files:
|
|
67
67
|
- README.rst
|
68
68
|
- Rakefile
|
69
69
|
- lib/trafaret.rb
|
70
|
-
- lib/trafaret/base.rb
|
71
70
|
- lib/trafaret/constructor.rb
|
72
71
|
- lib/trafaret/errors.rb
|
72
|
+
- lib/trafaret/hash.rb
|
73
73
|
- lib/trafaret/numeric.rb
|
74
74
|
- lib/trafaret/uri_email.rb
|
75
75
|
- lib/trafaret/validator.rb
|