min_max_validator 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +17 -0
- data/.rspec +2 -0
- data/Gemfile +8 -0
- data/LICENSE.txt +22 -0
- data/README.md +52 -0
- data/Rakefile +1 -0
- data/lib/min_max_validator/version.rb +3 -0
- data/lib/min_max_validator.rb +26 -0
- data/min_max_validator.gemspec +24 -0
- data/spec/min_max_validator_spec.rb +25 -0
- data/spec/salary.rb +14 -0
- data/spec/spec_helper.rb +18 -0
- metadata +101 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 38db3259d6533c22a29614ced4b5211ffd06ec3e
|
4
|
+
data.tar.gz: 0a4b3762015aca70966d7845fcb1dda36402964c
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 370bd59997189d7087d29bf5e666da720112eef7bde2dd70611ff05b8d292abcd4b56e20ceeeb41978ffa6e5966d7b5161c56d0a4d570350497bdb47e94a7277
|
7
|
+
data.tar.gz: bd362e55ec3fedd0355b9d087a4f0cdbf6b168aae54cf8893341605a73852ac1a4d20b0ba2bca85f78e064e38463b2fcda97664628738bfc57ea7f5b8177a04e
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Naveen Agarwal
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
# MinMaxValidator
|
2
|
+
|
3
|
+
In rails, when we have two attributes to store the minimum and maximum values then we have to write the custom validator or callbacks each time to check the valid maximum value. So this is a simple and small validator which does the same for the two attributes. It validates that if maximum is present then maximum should be >= minimum else it adds error to the maximum attribute name passed to the validator.
|
4
|
+
|
5
|
+
For Example:
|
6
|
+
|
7
|
+
1. Comparing maximum minimum salary (maximum should be >= minimum)
|
8
|
+
|
9
|
+
2. Comparing maximum minimum age
|
10
|
+
|
11
|
+
3. Comparing father son age, etc.
|
12
|
+
|
13
|
+
|
14
|
+
|
15
|
+
## Installation
|
16
|
+
|
17
|
+
Add this line to your application's Gemfile:
|
18
|
+
|
19
|
+
gem 'min_max_validator'
|
20
|
+
|
21
|
+
And then execute:
|
22
|
+
|
23
|
+
$ bundle
|
24
|
+
|
25
|
+
Or install it yourself as:
|
26
|
+
|
27
|
+
$ gem install min_max_validator
|
28
|
+
|
29
|
+
## Usage
|
30
|
+
|
31
|
+
class Salary < ActiveRecord::Base
|
32
|
+
|
33
|
+
validates_with MinMaxValidator,
|
34
|
+
fields: { min: :min_salary, max: :max_salary, msg: "Maximum salary can't be less than minimum salary" }
|
35
|
+
|
36
|
+
end
|
37
|
+
|
38
|
+
#=> Explanation of the fields argument
|
39
|
+
|
40
|
+
fields: {
|
41
|
+
min: <attribute name holding minimum value>,
|
42
|
+
max: <attribute name holding maximum value>,
|
43
|
+
msg: <error message to be set>
|
44
|
+
}
|
45
|
+
|
46
|
+
## Contributing
|
47
|
+
|
48
|
+
1. Fork it ( http://github.com/<my-github-username>/min_max_validator/fork )
|
49
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
50
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
51
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
52
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,26 @@
|
|
1
|
+
require "min_max_validator/version"
|
2
|
+
require 'active_model'
|
3
|
+
|
4
|
+
|
5
|
+
class MinMaxValidator < ActiveModel::Validator
|
6
|
+
|
7
|
+
def validate(record)
|
8
|
+
min = record.send options[:fields][:min]
|
9
|
+
max = record.send options[:fields][:max]
|
10
|
+
|
11
|
+
unless valid_max_value?(min: min, max: max)
|
12
|
+
record.errors.add(options[:fields][:max], options[:fields][:msg])
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
private
|
17
|
+
|
18
|
+
def valid_max_value?(min: 0, max: nil)
|
19
|
+
if max.present?
|
20
|
+
max >= min
|
21
|
+
else
|
22
|
+
true
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'min_max_validator/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "min_max_validator"
|
8
|
+
spec.version = MinMaxValidatorVersion::VERSION
|
9
|
+
spec.authors = ["Naveen Agarwal"]
|
10
|
+
spec.email = ["naveenagarwal287@gmail.com"]
|
11
|
+
spec.summary = %q{mininum and maximum value validator for the two attributes.}
|
12
|
+
spec.description = %q{mininum and maximum value validator for the two attributes.}
|
13
|
+
spec.homepage = ""
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.5"
|
22
|
+
spec.add_development_dependency "rake"
|
23
|
+
spec.add_development_dependency "activemodel", "~> 4.0.0"
|
24
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'salary'
|
3
|
+
|
4
|
+
describe Salary do
|
5
|
+
|
6
|
+
before do
|
7
|
+
@salary = Salary.new
|
8
|
+
end
|
9
|
+
|
10
|
+
it "salary should not be valid if maximum salary is less than minimum salary" do
|
11
|
+
@salary.minimum = 10000
|
12
|
+
@salary.maximum = 5000
|
13
|
+
@salary.should_not be_valid
|
14
|
+
@salary.errors.messages[:maximum].should_not be_blank
|
15
|
+
end
|
16
|
+
|
17
|
+
it "salary should be valid if maximum salary is >= than minimum salary" do
|
18
|
+
@salary.minimum = 10000
|
19
|
+
@salary.maximum = 10000
|
20
|
+
@salary.should be_valid
|
21
|
+
@salary.maximum = 10001
|
22
|
+
@salary.should be_valid
|
23
|
+
end
|
24
|
+
|
25
|
+
end
|
data/spec/salary.rb
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
require 'active_model'
|
2
|
+
|
3
|
+
require File.join(File.dirname(__FILE__), '..', 'lib/min_max_validator')
|
4
|
+
|
5
|
+
class Salary
|
6
|
+
|
7
|
+
include ActiveModel::Validations
|
8
|
+
|
9
|
+
attr_accessor :minimum, :maximum
|
10
|
+
|
11
|
+
validates_with MinMaxValidator,
|
12
|
+
fields: { min: :minimum, max: :maximum, msg: "Invalid maximum salary" }
|
13
|
+
|
14
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
# This file was generated by the `rspec --init` command. Conventionally, all
|
2
|
+
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
|
3
|
+
# Require this file using `require "spec_helper"` to ensure that it is only
|
4
|
+
# loaded once.
|
5
|
+
#
|
6
|
+
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
|
7
|
+
|
8
|
+
RSpec.configure do |config|
|
9
|
+
config.treat_symbols_as_metadata_keys_with_true_values = true
|
10
|
+
config.run_all_when_everything_filtered = true
|
11
|
+
config.filter_run :focus
|
12
|
+
|
13
|
+
# Run specs in random order to surface order dependencies. If you find an
|
14
|
+
# order dependency and want to debug it, you can fix the order by providing
|
15
|
+
# the seed, which is printed after each run.
|
16
|
+
# --seed 1234
|
17
|
+
config.order = 'random'
|
18
|
+
end
|
metadata
ADDED
@@ -0,0 +1,101 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: min_max_validator
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Naveen Agarwal
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-03-05 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.5'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.5'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: activemodel
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: 4.0.0
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: 4.0.0
|
55
|
+
description: mininum and maximum value validator for the two attributes.
|
56
|
+
email:
|
57
|
+
- naveenagarwal287@gmail.com
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- ".gitignore"
|
63
|
+
- ".rspec"
|
64
|
+
- Gemfile
|
65
|
+
- LICENSE.txt
|
66
|
+
- README.md
|
67
|
+
- Rakefile
|
68
|
+
- lib/min_max_validator.rb
|
69
|
+
- lib/min_max_validator/version.rb
|
70
|
+
- min_max_validator.gemspec
|
71
|
+
- spec/min_max_validator_spec.rb
|
72
|
+
- spec/salary.rb
|
73
|
+
- spec/spec_helper.rb
|
74
|
+
homepage: ''
|
75
|
+
licenses:
|
76
|
+
- MIT
|
77
|
+
metadata: {}
|
78
|
+
post_install_message:
|
79
|
+
rdoc_options: []
|
80
|
+
require_paths:
|
81
|
+
- lib
|
82
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
83
|
+
requirements:
|
84
|
+
- - ">="
|
85
|
+
- !ruby/object:Gem::Version
|
86
|
+
version: '0'
|
87
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
88
|
+
requirements:
|
89
|
+
- - ">="
|
90
|
+
- !ruby/object:Gem::Version
|
91
|
+
version: '0'
|
92
|
+
requirements: []
|
93
|
+
rubyforge_project:
|
94
|
+
rubygems_version: 2.2.0
|
95
|
+
signing_key:
|
96
|
+
specification_version: 4
|
97
|
+
summary: mininum and maximum value validator for the two attributes.
|
98
|
+
test_files:
|
99
|
+
- spec/min_max_validator_spec.rb
|
100
|
+
- spec/salary.rb
|
101
|
+
- spec/spec_helper.rb
|