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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: d3da287d80b08ce05b13b14cc3a273319a73dd20
4
- data.tar.gz: 8dbec245e7f84988b0c6686182b7039ac4fb5e72
3
+ metadata.gz: 552e91fdd3fcd10fffaadc09b4b4608d653a51c8
4
+ data.tar.gz: eab3104c51d2f8f047d2fc8e2b3197cf9d3c928b
5
5
  SHA512:
6
- metadata.gz: dece3132f430f9f0bf9a7d70fc903033bbacd78566bf7eb4465ea3fe4fdc8aeef89d46888102810e8a865810837c08ff71e58a91857e8e481edae9f33249f722
7
- data.tar.gz: 2d639254921bca95796de8d776b54bc713d637a5076b85643a5eb70c81442a8104c86894c35e29ac5470b3c9ae2894bd6429cf9812e7a1465ccf335890a13937
6
+ metadata.gz: c545dd265e1262c133f7d702c781a3c7d00f65100ff12dfd1d4336e411cc1b82d38c567bb12a157fde63c3017a66c21d7c81a2ce1d0712821bff58f17284e61b
7
+ data.tar.gz: 6815466bd20840e98b2db91c7ed2fb155b9ea0f7b825b0305f617b463d10308d6cf23d5fd956118ff402d6cf2e18a6ac86a474c36de49134b6e1eca46ada869f
@@ -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"
@@ -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.classify
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
@@ -11,6 +11,8 @@ module Trafaret
11
11
  Trafaret.get_validator from
12
12
  when Trafaret::Validator
13
13
  from
14
+ when ::Proc
15
+ from
14
16
  else
15
17
  raise 'Wrong parameter'
16
18
  end
@@ -45,6 +45,11 @@ class Trafaret::Validator
45
45
  @converters << blk
46
46
  end
47
47
 
48
+ def to (&blk)
49
+ @converters << blk
50
+ self
51
+ end
52
+
48
53
  # ADT
49
54
  def |(other)
50
55
  Trafaret::Or.new(self, other)
@@ -1,3 +1,3 @@
1
1
  module Trafaret
2
- VERSION = "1.5.1"
2
+ VERSION = "1.5.2"
3
3
  end
@@ -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
- argh: {
38
+ hash: {
39
39
  karma: :integer
40
40
  },
41
- arr: [{id: :integer}]
41
+ array: [{id: :integer}],
42
+ proc_: proc { |d| d }
42
43
  })
43
- res = t.call({kuku: 123, krkr: 'karma', argh: {karma: 234}, arr: [{id: 123}, {id: 234}]})
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[:argh][:karma].should == 234
46
+ res[:hash][:karma].should == 234
47
+ res[:proc_].should == 123
46
48
  end
47
49
  end
48
50
 
@@ -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.1
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.md
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