veto 0.0.2 → 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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 5400da755950ab1051ba40feedecd92a41dd0647
4
- data.tar.gz: 5fb0aa35d6552f56c1c11c179527a4ef48e09f4c
3
+ metadata.gz: 4206c75190ac20d67c36162f76b7ea2b9e35e674
4
+ data.tar.gz: f12554b84b0c9969a26948daf5df0f980c622e96
5
5
  SHA512:
6
- metadata.gz: b856fcd337a2c8eab00d6f52e1ae450763cf84ef69b14b2e6cce43268d7ed11d8272beeb1426ea48964000833976d5e02f4970ecbc222d4aaa9941fa594e51ef
7
- data.tar.gz: 59c1a36ce169a31978e7280e8f2bb86ff171e4acbb83f5b1e1217cdb1529680adfa32d2afbf2840d729747e8f92406df9d52c9552e25b012defd9767b28cfcdc
6
+ metadata.gz: 838c7c98791d279f9a9031ae2273ce1cae41f92d8f619adc02386e47ce1244b4f20c5dfa94034452fbc01af9bdf7b5b5ad0e11d94129c46702a56cdbe34f43c5
7
+ data.tar.gz: 9dc1a3f7b53683c1c00ae27f6e24113cd989066631cadddd1ad765f1213641c4b6260d112c7ec539587fbc4b550e9b51a47e05fa93f6ae9c330bbcb9fe83e1d0
data/.travis.yml ADDED
@@ -0,0 +1,3 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.0.0
data/README.md CHANGED
@@ -4,6 +4,8 @@ Veto provides lightweight validation for plain old ruby objects, using a familia
4
4
 
5
5
  Tested on the following Rubies: MRI 2.0.0.
6
6
 
7
+ [![Build Status](https://travis-ci.org/kodio/veto.png)](https://travis-ci.org/kodio/veto)
8
+
7
9
  ## Installation
8
10
 
9
11
  Add this line to your application's Gemfile:
@@ -220,6 +222,50 @@ class PersonValidator
220
222
  end
221
223
  ```
222
224
 
225
+ ### Greater Than
226
+ This helper checks that the specified attribute can be a valid float which is greater than a specified value.
227
+
228
+ ```ruby
229
+ class PersonValidator
230
+ include Veto.validator
231
+
232
+ validates :age, :greater_than => 12
233
+ end
234
+ ```
235
+
236
+ ### Greater Than Or Equal To
237
+ This helper checks that the specified attribute can be a valid float which is greater than or equal to a specified value.
238
+
239
+ ```ruby
240
+ class PersonValidator
241
+ include Veto.validator
242
+
243
+ validates :age, :greater_than_or_equal_to => 12
244
+ end
245
+ ```
246
+
247
+ ### Less Than
248
+ This helper checks that the specified attribute can be a valid float which is less than a specified value.
249
+
250
+ ```ruby
251
+ class PersonValidator
252
+ include Veto.validator
253
+
254
+ validates :age, :less_than => 11
255
+ end
256
+ ```
257
+
258
+ ### Less Than Or Equal To
259
+ This helper checks that the specified attribute can be a valid float which is less than or equal to a specified value.
260
+
261
+ ```ruby
262
+ class PersonValidator
263
+ include Veto.validator
264
+
265
+ validates :age, :less_than_or_equal_to => 11
266
+ end
267
+ ```
268
+
223
269
  ### Exact Length
224
270
  This helper checks that an attribute is an exact length in characters.
225
271
 
data/Rakefile CHANGED
@@ -1 +1,10 @@
1
1
  require "bundler/gem_tasks"
2
+ require 'rake/testtask'
3
+
4
+ Rake::TestTask.new do |t|
5
+ t.libs << "spec"
6
+ t.test_files = FileList['spec/**/*_spec.rb']
7
+ t.verbose = true
8
+ end
9
+
10
+ task :default => :test
@@ -1,8 +1,12 @@
1
1
  require 'veto/validators/exact_length_validator'
2
2
  require 'veto/validators/format_validator'
3
+ require 'veto/validators/greater_than_validator'
4
+ require 'veto/validators/greater_than_or_equal_to_validator'
3
5
  require 'veto/validators/inclusion_validator'
4
6
  require 'veto/validators/integer_validator'
5
7
  require 'veto/validators/length_range_validator'
8
+ require 'veto/validators/less_than_validator'
9
+ require 'veto/validators/less_than_or_equal_to_validator'
6
10
  require 'veto/validators/max_length_validator'
7
11
  require 'veto/validators/min_length_validator'
8
12
  require 'veto/validators/not_null_validator'
@@ -2,16 +2,20 @@ module Veto
2
2
  class Configuration
3
3
  class Message
4
4
  DEFAULT_MESSAGES = {
5
- :exact_length => lambda{|exact| "is not #{exact} characters"},
6
- :format => lambda{"is not valid"},
7
- :inclusion => lambda{|set| "is not in set: #{set.inspect}"},
8
- :integer => lambda{"is not a number"},
9
- :length_range => lambda{"is too short or too long"},
10
- :max_length => lambda{|max| "is longer than #{max} characters"},
11
- :min_length => lambda{|min| "is shorter than #{min} characters"},
12
- :not_null => lambda{"is not present"},
13
- :numeric => lambda{"is not a number"},
14
- :presence => lambda{"is not present"}
5
+ :exact_length => lambda{|exact| "is not #{exact} characters"},
6
+ :format => lambda{"is not valid"},
7
+ :greater_than => lambda{|boundary| "must be greater than #{boundary}"},
8
+ :greater_than_or_equal_to => lambda{|boundary| "must be greater than or equal to #{boundary}"},
9
+ :inclusion => lambda{|set| "is not in set: #{set.inspect}"},
10
+ :integer => lambda{"is not a number"},
11
+ :length_range => lambda{"is too short or too long"},
12
+ :less_than => lambda{|boundary| "must be less than #{boundary}"},
13
+ :less_than_or_equal_to => lambda{|boundary| "must be less than or equal to #{boundary}"},
14
+ :max_length => lambda{|max| "is longer than #{max} characters"},
15
+ :min_length => lambda{|min| "is shorter than #{min} characters"},
16
+ :not_null => lambda{"is not present"},
17
+ :numeric => lambda{"is not a number"},
18
+ :presence => lambda{"is not present"}
15
19
  }
16
20
 
17
21
  def get type, *args
@@ -0,0 +1,22 @@
1
+ require 'veto/validators/attribute_validator'
2
+
3
+ module Veto
4
+ class GreaterThanOrEqualToValidator < AttributeValidator
5
+ def validate entity, attribute, value, errors, options={}
6
+ boundary = options.fetch(:with)
7
+ message = options.fetch(:message, :greater_than_or_equal_to)
8
+ on = options.fetch(:on, attribute)
9
+
10
+ begin
11
+ v = Kernel.Float(value.to_s)
12
+ nil
13
+ rescue
14
+ return errors.add(on, message, boundary)
15
+ end
16
+
17
+ unless v >= boundary
18
+ errors.add(on, message, boundary)
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,22 @@
1
+ require 'veto/validators/attribute_validator'
2
+
3
+ module Veto
4
+ class GreaterThanValidator < AttributeValidator
5
+ def validate entity, attribute, value, errors, options={}
6
+ boundary = options.fetch(:with)
7
+ message = options.fetch(:message, :greater_than)
8
+ on = options.fetch(:on, attribute)
9
+
10
+ begin
11
+ v = Kernel.Float(value.to_s)
12
+ nil
13
+ rescue
14
+ return errors.add(on, message, boundary)
15
+ end
16
+
17
+ unless v > boundary
18
+ errors.add(on, message, boundary)
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,22 @@
1
+ require 'veto/validators/attribute_validator'
2
+
3
+ module Veto
4
+ class LessThanOrEqualToValidator < AttributeValidator
5
+ def validate entity, attribute, value, errors, options={}
6
+ boundary = options.fetch(:with)
7
+ message = options.fetch(:message, :less_than_or_equal_to)
8
+ on = options.fetch(:on, attribute)
9
+
10
+ begin
11
+ v = Kernel.Float(value.to_s)
12
+ nil
13
+ rescue
14
+ return errors.add(on, message, boundary)
15
+ end
16
+
17
+ unless v <= boundary
18
+ errors.add(on, message, boundary)
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,22 @@
1
+ require 'veto/validators/attribute_validator'
2
+
3
+ module Veto
4
+ class LessThanValidator < AttributeValidator
5
+ def validate entity, attribute, value, errors, options={}
6
+ boundary = options.fetch(:with)
7
+ message = options.fetch(:message, :less_than)
8
+ on = options.fetch(:on, attribute)
9
+
10
+ begin
11
+ v = Kernel.Float(value.to_s)
12
+ nil
13
+ rescue
14
+ return errors.add(on, message, boundary)
15
+ end
16
+
17
+ unless v < boundary
18
+ errors.add(on, message, boundary)
19
+ end
20
+ end
21
+ end
22
+ end
data/lib/veto/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Veto
2
- VERSION = "0.0.2"
2
+ VERSION = "0.1.0"
3
3
  end
@@ -61,6 +61,122 @@ describe Veto do
61
61
  end
62
62
  end
63
63
 
64
+ describe 'greater_than' do
65
+ let(:options) {{:greater_than => 10}}
66
+
67
+ context 'when value is greater than option' do
68
+ let(:value) { 11 }
69
+ it { errors.must_be_nil }
70
+ end
71
+
72
+ context 'when float is greater than option' do
73
+ let(:value) { 11.123 }
74
+ it { errors.must_be_nil }
75
+ end
76
+
77
+ context 'when value is equal to option' do
78
+ let(:value) { 10 }
79
+ it { errors.must_equal ["must be greater than 10"] }
80
+ end
81
+
82
+ context 'when value is less than option' do
83
+ let(:value) { 9 }
84
+ it { errors.must_equal ["must be greater than 10"] }
85
+ end
86
+
87
+ context 'when value is string' do
88
+ let(:value) { 'abc' }
89
+ it { errors.must_equal ["must be greater than 10"] }
90
+ end
91
+ end
92
+
93
+ describe 'greater_than_or_equal_to' do
94
+ let(:options) {{:greater_than_or_equal_to => 10}}
95
+
96
+ context 'when value is greater than option' do
97
+ let(:value) { 11 }
98
+ it { errors.must_be_nil }
99
+ end
100
+
101
+ context 'when float value is greater than option' do
102
+ let(:value) { 11.123 }
103
+ it { errors.must_be_nil }
104
+ end
105
+
106
+ context 'when value is equal to option' do
107
+ let(:value) { 10 }
108
+ it { errors.must_be_nil }
109
+ end
110
+
111
+ context 'when value is less than option' do
112
+ let(:value) { 9 }
113
+ it { errors.must_equal ["must be greater than or equal to 10"] }
114
+ end
115
+
116
+ context 'when value is a string' do
117
+ let(:value) { 'abc' }
118
+ it { errors.must_equal ["must be greater than or equal to 10"] }
119
+ end
120
+ end
121
+
122
+ describe 'less_than' do
123
+ let(:options) {{:less_than => 10}}
124
+
125
+ context 'when value is less than option' do
126
+ let(:value) { 9 }
127
+ it { errors.must_be_nil }
128
+ end
129
+
130
+ context 'when float value is less than option' do
131
+ let(:value) { 8.123 }
132
+ it { errors.must_be_nil }
133
+ end
134
+
135
+ context 'when value is equal to option' do
136
+ let(:value) { 10 }
137
+ it { errors.must_equal ["must be less than 10"] }
138
+ end
139
+
140
+ context 'when value is greater than option' do
141
+ let(:value) { 11 }
142
+ it { errors.must_equal ["must be less than 10"] }
143
+ end
144
+
145
+ context 'when value is a string' do
146
+ let(:value) { 'abc' }
147
+ it { errors.must_equal ["must be less than 10"] }
148
+ end
149
+ end
150
+
151
+ describe 'less_than_or_equal_to' do
152
+ let(:options) {{:less_than_or_equal_to => 10}}
153
+
154
+ context 'when value is less than option' do
155
+ let(:value) { 9 }
156
+ it { errors.must_be_nil }
157
+ end
158
+
159
+ context 'when float value is less than option' do
160
+ let(:value) { 8.123 }
161
+ it { errors.must_be_nil }
162
+ end
163
+
164
+ context 'when value is equal to option' do
165
+ let(:value) { 10 }
166
+ it { errors.must_be_nil }
167
+ end
168
+
169
+ context 'when value is greater than option' do
170
+ let(:value) { 11 }
171
+ it { errors.must_equal ["must be less than or equal to 10"] }
172
+ end
173
+
174
+ context 'when value is a string' do
175
+ let(:value) { 'abc' }
176
+ it { errors.must_equal ["must be less than or equal to 10"] }
177
+ end
178
+ end
179
+
64
180
  describe 'inclusion' do
65
181
  context 'when set is array' do
66
182
  let(:options) {{:inclusion => %w(cat dog bird rabbit)}}
data/veto.gemspec CHANGED
@@ -21,4 +21,5 @@ Gem::Specification.new do |spec|
21
21
  spec.add_development_dependency "bundler", "~> 1.3"
22
22
  spec.add_development_dependency "rake"
23
23
  spec.add_development_dependency "mocha", "~> 0.14.0"
24
+ spec.add_development_dependency "minitest", "~> 5.0.8"
24
25
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: veto
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Erik Lott
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-11-06 00:00:00.000000000 Z
11
+ date: 2013-11-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -52,6 +52,20 @@ dependencies:
52
52
  - - ~>
53
53
  - !ruby/object:Gem::Version
54
54
  version: 0.14.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.8
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.8
55
69
  description: Validations for ruby objects
56
70
  email:
57
71
  - erik.lott@kodio.com
@@ -60,6 +74,7 @@ extensions: []
60
74
  extra_rdoc_files: []
61
75
  files:
62
76
  - .gitignore
77
+ - .travis.yml
63
78
  - Gemfile
64
79
  - LICENSE.txt
65
80
  - README.md
@@ -79,9 +94,13 @@ files:
79
94
  - lib/veto/validators/custom_method_validator.rb
80
95
  - lib/veto/validators/exact_length_validator.rb
81
96
  - lib/veto/validators/format_validator.rb
97
+ - lib/veto/validators/greater_than_or_equal_to_validator.rb
98
+ - lib/veto/validators/greater_than_validator.rb
82
99
  - lib/veto/validators/inclusion_validator.rb
83
100
  - lib/veto/validators/integer_validator.rb
84
101
  - lib/veto/validators/length_range_validator.rb
102
+ - lib/veto/validators/less_than_or_equal_to_validator.rb
103
+ - lib/veto/validators/less_than_validator.rb
85
104
  - lib/veto/validators/max_length_validator.rb
86
105
  - lib/veto/validators/min_length_validator.rb
87
106
  - lib/veto/validators/not_null_validator.rb