trafaret 1.5.1 → 1.5.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.rst +35 -0
- data/lib/trafaret.rb +1 -1
- data/lib/trafaret/constructor.rb +2 -0
- data/lib/trafaret/validator.rb +5 -0
- data/lib/trafaret/version.rb +1 -1
- data/spec/trafaret_spec.rb +6 -4
- data/trafaret.gemspec +1 -0
- metadata +16 -2
- data/README.md +0 -29
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 552e91fdd3fcd10fffaadc09b4b4608d653a51c8
|
4
|
+
data.tar.gz: eab3104c51d2f8f047d2fc8e2b3197cf9d3c928b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c545dd265e1262c133f7d702c781a3c7d00f65100ff12dfd1d4336e411cc1b82d38c567bb12a157fde63c3017a66c21d7c81a2ce1d0712821bff58f17284e61b
|
7
|
+
data.tar.gz: 6815466bd20840e98b2db91c7ed2fb155b9ea0f7b825b0305f617b463d10308d6cf23d5fd956118ff402d6cf2e18a6ac86a474c36de49134b6e1eca46ada869f
|
data/README.rst
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
Trafaret
|
2
|
+
========
|
3
|
+
|
4
|
+
Trafaret is lib for data parsing.
|
5
|
+
|
6
|
+
You can want this first::
|
7
|
+
|
8
|
+
T = Trafaret
|
9
|
+
|
10
|
+
Small example for one of ways to construct Trafaret::
|
11
|
+
|
12
|
+
T.construct({
|
13
|
+
id: :integer,
|
14
|
+
post_ids: [:integer],
|
15
|
+
users: [{name: :string, id: :integer}],
|
16
|
+
proc_: proc { |a| a == 3 ? a : T.failure('Not a 3') }
|
17
|
+
})
|
18
|
+
|
19
|
+
Trafarets supports ``|`` and ``&`` operations::
|
20
|
+
|
21
|
+
(T.symbol(:t) | T.symbol(:a)).call(:t) == :t
|
22
|
+
|
23
|
+
(T.string.to(&:to_i) & T.integer).call('123') == 123
|
24
|
+
|
25
|
+
You can attach converter to trafaret with ``to``::
|
26
|
+
|
27
|
+
T.string(regex: /\A\d+\Z/).to { |match| match.string.to_i }.call('123')
|
28
|
+
|
29
|
+
Any callable can be used as ``Trafaret`` while it use simple convention. If data ok, you return something, if it wrong
|
30
|
+
you must return ``Trafaret::Error`` instance with message. Correct message can be a string or a Hash with simple keys and Errors values::
|
31
|
+
|
32
|
+
irb> (T.string & T.proc { |data| data == 'karramba' ? 'Bart' : T.failure('Not a Bart text!')}).call ('ku')
|
33
|
+
=> #<Trafaret::Error("Not a Bart text!")>
|
34
|
+
irb> (T.string & T.proc { |data| data == 'karramba' ? 'Bart' : T.failure('Not a Bart text!')}).call ('karramba')
|
35
|
+
=> "Bart"
|
data/lib/trafaret.rb
CHANGED
@@ -14,7 +14,7 @@ module Trafaret
|
|
14
14
|
class << self
|
15
15
|
def get_validator(validator)
|
16
16
|
if validator.is_a? ::Symbol
|
17
|
-
class_name = validator.to_s.
|
17
|
+
class_name = validator.to_s.split('_').collect!{ |w| w.capitalize }.join
|
18
18
|
validator = Trafaret.const_get(class_name) rescue nil
|
19
19
|
validator ||= Kernel.const_get(class_name)
|
20
20
|
else
|
data/lib/trafaret/constructor.rb
CHANGED
data/lib/trafaret/validator.rb
CHANGED
data/lib/trafaret/version.rb
CHANGED
data/spec/trafaret_spec.rb
CHANGED
@@ -35,14 +35,16 @@ describe Trafaret::Base do
|
|
35
35
|
t = T.construct({
|
36
36
|
kuku: :integer,
|
37
37
|
T.key(:krkr, nil, to_name: :id) => :string,
|
38
|
-
|
38
|
+
hash: {
|
39
39
|
karma: :integer
|
40
40
|
},
|
41
|
-
|
41
|
+
array: [{id: :integer}],
|
42
|
+
proc_: proc { |d| d }
|
42
43
|
})
|
43
|
-
res = t.call({kuku: 123, krkr: 'karma',
|
44
|
+
res = t.call({kuku: 123, krkr: 'karma', hash: {karma: 234}, array: [{id: 123}, {id: 234}], proc_: 123})
|
44
45
|
res[:id].should == 'karma'
|
45
|
-
res[:
|
46
|
+
res[:hash][:karma].should == 234
|
47
|
+
res[:proc_].should == 123
|
46
48
|
end
|
47
49
|
end
|
48
50
|
|
data/trafaret.gemspec
CHANGED
@@ -19,6 +19,7 @@ Gem::Specification.new do |spec|
|
|
19
19
|
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
20
20
|
spec.require_paths = ["lib"]
|
21
21
|
|
22
|
+
spec.add_dependency "activesupport"
|
22
23
|
spec.add_development_dependency "bundler", "~> 1.5"
|
23
24
|
spec.add_development_dependency "rake"
|
24
25
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
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.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Mikhail Krivushin
|
@@ -10,6 +10,20 @@ bindir: bin
|
|
10
10
|
cert_chain: []
|
11
11
|
date: 2014-04-16 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: activesupport
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
13
27
|
- !ruby/object:Gem::Dependency
|
14
28
|
name: bundler
|
15
29
|
requirement: !ruby/object:Gem::Requirement
|
@@ -50,7 +64,7 @@ files:
|
|
50
64
|
- ".gitignore"
|
51
65
|
- Gemfile
|
52
66
|
- LICENSE.txt
|
53
|
-
- README.
|
67
|
+
- README.rst
|
54
68
|
- Rakefile
|
55
69
|
- lib/trafaret.rb
|
56
70
|
- lib/trafaret/base.rb
|
data/README.md
DELETED
@@ -1,29 +0,0 @@
|
|
1
|
-
# Trafaret
|
2
|
-
|
3
|
-
TODO: Write a gem description
|
4
|
-
|
5
|
-
## Installation
|
6
|
-
|
7
|
-
Add this line to your application's Gemfile:
|
8
|
-
|
9
|
-
gem 'trafaret'
|
10
|
-
|
11
|
-
And then execute:
|
12
|
-
|
13
|
-
$ bundle
|
14
|
-
|
15
|
-
Or install it yourself as:
|
16
|
-
|
17
|
-
$ gem install trafaret
|
18
|
-
|
19
|
-
## Usage
|
20
|
-
|
21
|
-
TODO: Write usage instructions here
|
22
|
-
|
23
|
-
## Contributing
|
24
|
-
|
25
|
-
1. Fork it ( http://github.com/<my-github-username>/trafaret/fork )
|
26
|
-
2. Create your feature branch (`git checkout -b my-new-feature`)
|
27
|
-
3. Commit your changes (`git commit -am 'Add some feature'`)
|
28
|
-
4. Push to the branch (`git push origin my-new-feature`)
|
29
|
-
5. Create new Pull Request
|