rasti-form 0.1.0

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.
Files changed (45) hide show
  1. checksums.yaml +7 -0
  2. data/.coveralls.yml +2 -0
  3. data/.gitignore +9 -0
  4. data/.ruby-gemset +1 -0
  5. data/.ruby-version +1 -0
  6. data/.travis.yml +11 -0
  7. data/Gemfile +4 -0
  8. data/LICENSE.txt +21 -0
  9. data/README.md +131 -0
  10. data/Rakefile +24 -0
  11. data/lib/rasti/form/castable.rb +25 -0
  12. data/lib/rasti/form/errors.rb +41 -0
  13. data/lib/rasti/form/formatable.rb +19 -0
  14. data/lib/rasti/form/types/array.rb +36 -0
  15. data/lib/rasti/form/types/boolean.rb +42 -0
  16. data/lib/rasti/form/types/enum.rb +38 -0
  17. data/lib/rasti/form/types/float.rb +33 -0
  18. data/lib/rasti/form/types/form.rb +40 -0
  19. data/lib/rasti/form/types/hash.rb +39 -0
  20. data/lib/rasti/form/types/integer.rb +21 -0
  21. data/lib/rasti/form/types/regexp.rb +28 -0
  22. data/lib/rasti/form/types/string.rb +23 -0
  23. data/lib/rasti/form/types/symbol.rb +23 -0
  24. data/lib/rasti/form/types/time.rb +40 -0
  25. data/lib/rasti/form/types/uuid.rb +19 -0
  26. data/lib/rasti/form/version.rb +5 -0
  27. data/lib/rasti/form.rb +173 -0
  28. data/lib/rasti-form.rb +1 -0
  29. data/rasti-form.gemspec +37 -0
  30. data/spec/coverage_helper.rb +7 -0
  31. data/spec/form_spec.rb +310 -0
  32. data/spec/minitest_helper.rb +13 -0
  33. data/spec/types/array_spec.rb +22 -0
  34. data/spec/types/boolean_spec.rb +24 -0
  35. data/spec/types/enum_spec.rb +20 -0
  36. data/spec/types/float_spec.rb +18 -0
  37. data/spec/types/form_spec.rb +41 -0
  38. data/spec/types/hash_spec.rb +20 -0
  39. data/spec/types/integer_spec.rb +18 -0
  40. data/spec/types/regexp_spec.rb +20 -0
  41. data/spec/types/string_spec.rb +16 -0
  42. data/spec/types/symbol_spec.rb +16 -0
  43. data/spec/types/time_spec.rb +25 -0
  44. data/spec/types/uuid_spec.rb +18 -0
  45. metadata +228 -0
@@ -0,0 +1,41 @@
1
+ require 'minitest_helper'
2
+
3
+ describe Rasti::Form::Types::Form do
4
+
5
+ form = Rasti::Form[x: Rasti::Form::Types::Integer, y: Rasti::Form::Types::Integer]
6
+
7
+ hash = {x: '1', y: '2', z: '3'}
8
+
9
+ it 'Class' do
10
+ result = Rasti::Form::Types::Form[form].cast hash
11
+ result.must_be_instance_of form
12
+ result.x.must_equal 1
13
+ result.y.must_equal 2
14
+ end
15
+
16
+ it 'Inline' do
17
+ type = Rasti::Form::Types::Form[x: Rasti::Form::Types::Integer, y: Rasti::Form::Types::Integer]
18
+ result = type.cast hash
19
+ result.must_be_instance_of type.form_class
20
+ result.x.must_equal 1
21
+ result.y.must_equal 2
22
+ end
23
+
24
+ it 'Invalid form class' do
25
+ error = proc { Rasti::Form::Types::Form[1] }.must_raise ArgumentError
26
+ error.message.must_equal 'Invalid form specification: 1'
27
+ end
28
+
29
+ [nil, 'text', :symbol, 1, [1,2], Object.new].each do |value|
30
+ it "#{value.inspect} -> CastError" do
31
+ error = proc { Rasti::Form::Types::Form[form].cast(value) }.must_raise Rasti::Form::CastError
32
+ error.message.must_equal "Invalid cast: #{as_string(value)} -> #{Rasti::Form::Types::Form[form]}"
33
+ end
34
+ end
35
+
36
+ it '{x: "text"} -> ValidationError' do
37
+ error = proc { Rasti::Form::Types::Form[form].cast x: 'test' }.must_raise Rasti::Form::ValidationError
38
+ error.message.must_equal "Validation error: {\"x\":[\"Invalid cast: 'test' -> Rasti::Form::Types::Integer\"]}"
39
+ end
40
+
41
+ end
@@ -0,0 +1,20 @@
1
+ require 'minitest_helper'
2
+
3
+ describe Rasti::Form::Types::Hash do
4
+
5
+ it "{'a' => '123'} -> {a: 123}" do
6
+ Rasti::Form::Types::Hash[Rasti::Form::Types::Symbol, Rasti::Form::Types::Integer].cast('a' => '123').must_equal a: 123
7
+ end
8
+
9
+ it "{'1' => :abc} -> {1 => 'abc'}" do
10
+ Rasti::Form::Types::Hash[Rasti::Form::Types::Integer, Rasti::Form::Types::String].cast('1' => :abc).must_equal 1 => 'abc'
11
+ end
12
+
13
+ [nil, 1, 'text', :symbol, {a: true}, Object.new].each do |value|
14
+ it "#{value.inspect} -> CastError" do
15
+ error = proc { Rasti::Form::Types::Hash[Rasti::Form::Types::Symbol, Rasti::Form::Types::Integer].cast(value) }.must_raise Rasti::Form::CastError
16
+ error.message.must_equal "Invalid cast: #{as_string(value)} -> Rasti::Form::Types::Hash[Rasti::Form::Types::Symbol, Rasti::Form::Types::Integer]"
17
+ end
18
+ end
19
+
20
+ end
@@ -0,0 +1,18 @@
1
+ require 'minitest_helper'
2
+
3
+ describe Rasti::Form::Types::Integer do
4
+
5
+ [100, '200', Time.now, 2.1, "12.5"].each do |value|
6
+ it "#{value.inspect} -> #{value.to_i}" do
7
+ Rasti::Form::Types::Integer.cast(value).must_equal value.to_i
8
+ end
9
+ end
10
+
11
+ [nil, 'text', :symbol, '999'.to_sym, [1,2], {a: 1, b: 2}, Object.new].each do |value|
12
+ it "#{value.inspect} -> CastError" do
13
+ error = proc { Rasti::Form::Types::Integer.cast(value) }.must_raise Rasti::Form::CastError
14
+ error.message.must_equal "Invalid cast: #{as_string(value)} -> Rasti::Form::Types::Integer"
15
+ end
16
+ end
17
+
18
+ end
@@ -0,0 +1,20 @@
1
+ require 'minitest_helper'
2
+
3
+ describe Rasti::Form::Types::Regexp do
4
+
5
+ email_regexp = /\A[\w+\-.]+@[a-z\d\-]+(\.[a-z\d\-]+)*\.[a-z]+\z/i
6
+
7
+ ['user@mail.com'.to_sym, 'user.name-123@mail-test.com.ar'].each do |value|
8
+ it "#{value.inspect} -> #{value.to_s}" do
9
+ Rasti::Form::Types::Regexp[email_regexp].cast(value).must_equal value
10
+ end
11
+ end
12
+
13
+ [nil, 'text', :symbol, '999'.to_sym, [1,2], {a: 1, b: 2}, Object.new, 5].each do |value|
14
+ it "#{value.inspect} -> CastError" do
15
+ error = proc { Rasti::Form::Types::Regexp[email_regexp].cast(value) }.must_raise Rasti::Form::CastError
16
+ error.message.must_equal "Invalid cast: #{as_string(value)} -> Rasti::Form::Types::Regexp[#{as_string(email_regexp)}]"
17
+ end
18
+ end
19
+
20
+ end
@@ -0,0 +1,16 @@
1
+ require 'minitest_helper'
2
+
3
+ describe Rasti::Form::Types::String do
4
+
5
+ ['text', :symbol, true, false, 100, Time.now, [1,2], {a: 1, b: 2}, Object.new].each do |value|
6
+ it "#{value.inspect} -> \"#{value.to_s}\"" do
7
+ Rasti::Form::Types::String.cast(value).must_equal value.to_s
8
+ end
9
+ end
10
+
11
+ it 'nil -> CastError' do
12
+ error = proc { Rasti::Form::Types::String.cast(nil) }.must_raise Rasti::Form::CastError
13
+ error.message.must_equal 'Invalid cast: nil -> Rasti::Form::Types::String'
14
+ end
15
+
16
+ end
@@ -0,0 +1,16 @@
1
+ require 'minitest_helper'
2
+
3
+ describe Rasti::Form::Types::Symbol do
4
+
5
+ ['text', :symbol, true, false, 100, Time.now, [1,2], {a: 1, b: 2}, Object.new].each do |value|
6
+ it "#{value.inspect} -> \"#{value.to_s.to_sym}\"" do
7
+ Rasti::Form::Types::Symbol.cast(value).must_equal value.to_s.to_sym
8
+ end
9
+ end
10
+
11
+ it 'nil -> CastError' do
12
+ error = proc { Rasti::Form::Types::Symbol.cast(nil) }.must_raise Rasti::Form::CastError
13
+ error.message.must_equal 'Invalid cast: nil -> Rasti::Form::Types::Symbol'
14
+ end
15
+
16
+ end
@@ -0,0 +1,25 @@
1
+ require 'minitest_helper'
2
+
3
+ describe Rasti::Form::Types::Time do
4
+
5
+ time = Time.new 2016, 8, 18
6
+
7
+ it "#{time.inspect} -> #{time.inspect}" do
8
+ Rasti::Form::Types::Time['%F %T %z'].cast(time).must_equal time
9
+ end
10
+
11
+ ['%d/%m/%y', '%Y-%m-%d'].each do |format|
12
+ time_string = time.strftime(format)
13
+ it "#{time_string.inspect} -> #{time.inspect}" do
14
+ Rasti::Form::Types::Time[format].cast(time_string).must_equal time
15
+ end
16
+ end
17
+
18
+ [time.strftime('%d/%m/%y'), 'text', nil, 1, :symbol, [1,2], {a: 1, b: 2}, Object.new].each do |value|
19
+ it "#{value.inspect} -> CastError" do
20
+ error = proc { Rasti::Form::Types::Time['%F'].cast(value) }.must_raise Rasti::Form::CastError
21
+ error.message.must_equal "Invalid cast: #{as_string(value)} -> Rasti::Form::Types::Time['%F']"
22
+ end
23
+ end
24
+
25
+ end
@@ -0,0 +1,18 @@
1
+ require 'minitest_helper'
2
+
3
+ describe Rasti::Form::Types::UUID do
4
+
5
+ ['f09b7716-81a9-11e6-a549-bb8f165bcf02', '12345678-1234-1234-1234-123456789123'].each do |value|
6
+ it "#{value.inspect} -> #{value.to_s}" do
7
+ Rasti::Form::Types::UUID.cast(value).must_equal value.to_s
8
+ end
9
+ end
10
+
11
+ [nil, 'text', :symbol, '999'.to_sym, [1,2], {a: 1, b: 2}, Object.new, 5, 'f09b7716-81a9-11e6-a549-bb16502', 'f09b7716-11e6-a549-bb8f16502', '-84a9-11e6-a549-bb8f16502', 'f09b7716-81a9-11e6-a549-bh16502'].each do |value|
12
+ it "#{value.inspect} -> CastError" do
13
+ error = proc { Rasti::Form::Types::UUID.cast(value) }.must_raise Rasti::Form::CastError
14
+ error.message.must_equal "Invalid cast: #{as_string(value)} -> Rasti::Form::Types::UUID"
15
+ end
16
+ end
17
+
18
+ end
metadata ADDED
@@ -0,0 +1,228 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rasti-form
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Gabriel Naiman
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-03-01 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: multi_require
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.12'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.12'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '11.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '11.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: minitest
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '5.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '5.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: minitest-colorin
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '0.1'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '0.1'
83
+ - !ruby/object:Gem::Dependency
84
+ name: minitest-line
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '0.6'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '0.6'
97
+ - !ruby/object:Gem::Dependency
98
+ name: simplecov
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '0.12'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '0.12'
111
+ - !ruby/object:Gem::Dependency
112
+ name: coveralls
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: '0.8'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - "~>"
123
+ - !ruby/object:Gem::Version
124
+ version: '0.8'
125
+ - !ruby/object:Gem::Dependency
126
+ name: pry-nav
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - "~>"
130
+ - !ruby/object:Gem::Version
131
+ version: '0.2'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - "~>"
137
+ - !ruby/object:Gem::Version
138
+ version: '0.2'
139
+ description: Forms validations and type casting
140
+ email:
141
+ - gabynaiman@gmail.com
142
+ executables: []
143
+ extensions: []
144
+ extra_rdoc_files: []
145
+ files:
146
+ - ".coveralls.yml"
147
+ - ".gitignore"
148
+ - ".ruby-gemset"
149
+ - ".ruby-version"
150
+ - ".travis.yml"
151
+ - Gemfile
152
+ - LICENSE.txt
153
+ - README.md
154
+ - Rakefile
155
+ - lib/rasti-form.rb
156
+ - lib/rasti/form.rb
157
+ - lib/rasti/form/castable.rb
158
+ - lib/rasti/form/errors.rb
159
+ - lib/rasti/form/formatable.rb
160
+ - lib/rasti/form/types/array.rb
161
+ - lib/rasti/form/types/boolean.rb
162
+ - lib/rasti/form/types/enum.rb
163
+ - lib/rasti/form/types/float.rb
164
+ - lib/rasti/form/types/form.rb
165
+ - lib/rasti/form/types/hash.rb
166
+ - lib/rasti/form/types/integer.rb
167
+ - lib/rasti/form/types/regexp.rb
168
+ - lib/rasti/form/types/string.rb
169
+ - lib/rasti/form/types/symbol.rb
170
+ - lib/rasti/form/types/time.rb
171
+ - lib/rasti/form/types/uuid.rb
172
+ - lib/rasti/form/version.rb
173
+ - rasti-form.gemspec
174
+ - spec/coverage_helper.rb
175
+ - spec/form_spec.rb
176
+ - spec/minitest_helper.rb
177
+ - spec/types/array_spec.rb
178
+ - spec/types/boolean_spec.rb
179
+ - spec/types/enum_spec.rb
180
+ - spec/types/float_spec.rb
181
+ - spec/types/form_spec.rb
182
+ - spec/types/hash_spec.rb
183
+ - spec/types/integer_spec.rb
184
+ - spec/types/regexp_spec.rb
185
+ - spec/types/string_spec.rb
186
+ - spec/types/symbol_spec.rb
187
+ - spec/types/time_spec.rb
188
+ - spec/types/uuid_spec.rb
189
+ homepage: https://github.com/gabynaiman/rasti-form
190
+ licenses:
191
+ - MIT
192
+ metadata: {}
193
+ post_install_message:
194
+ rdoc_options: []
195
+ require_paths:
196
+ - lib
197
+ required_ruby_version: !ruby/object:Gem::Requirement
198
+ requirements:
199
+ - - ">="
200
+ - !ruby/object:Gem::Version
201
+ version: '0'
202
+ required_rubygems_version: !ruby/object:Gem::Requirement
203
+ requirements:
204
+ - - ">="
205
+ - !ruby/object:Gem::Version
206
+ version: '0'
207
+ requirements: []
208
+ rubyforge_project:
209
+ rubygems_version: 2.5.1
210
+ signing_key:
211
+ specification_version: 4
212
+ summary: Forms validations and type casting
213
+ test_files:
214
+ - spec/coverage_helper.rb
215
+ - spec/form_spec.rb
216
+ - spec/minitest_helper.rb
217
+ - spec/types/array_spec.rb
218
+ - spec/types/boolean_spec.rb
219
+ - spec/types/enum_spec.rb
220
+ - spec/types/float_spec.rb
221
+ - spec/types/form_spec.rb
222
+ - spec/types/hash_spec.rb
223
+ - spec/types/integer_spec.rb
224
+ - spec/types/regexp_spec.rb
225
+ - spec/types/string_spec.rb
226
+ - spec/types/symbol_spec.rb
227
+ - spec/types/time_spec.rb
228
+ - spec/types/uuid_spec.rb