extra_validations 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/.rspec +2 -0
- data/.travis.yml +4 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +39 -0
- data/Rakefile +1 -0
- data/bin/console +14 -0
- data/bin/setup +7 -0
- data/extra_validations.gemspec +25 -0
- data/lib/extra_validations/collection_length_validator.rb +22 -0
- data/lib/extra_validations/collection_validator.rb +15 -0
- data/lib/extra_validations/existence_validator.rb +16 -0
- data/lib/extra_validations/locale/en.yml +10 -0
- data/lib/extra_validations/nested_validator.rb +11 -0
- data/lib/extra_validations/version.rb +3 -0
- data/lib/extra_validations.rb +13 -0
- metadata +122 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: a352c493f0a5a9e7079bf9d25fc69d6f866a9589
|
4
|
+
data.tar.gz: c4b3e96aca1b7773ebe74d4bc95206c8a6c95706
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: fd6f237ae867ba6da83839a14576c24172d52c81d416d7e5c19fe924103e496bff10e135878f9ebe92d7dc0340658d4dd6917568bffd44f4795bc1a6580185a8
|
7
|
+
data.tar.gz: a1f33cbedc6e08dd42f28bab7ae2d8de21cb8d0a81277abc08356f19a9dd74ce4129a9ed5f38f5618996090422247956f4c4117678b396104bf7b4d300908a2f
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2015 Lenon Marcel
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
# ExtraValidations
|
2
|
+
|
3
|
+
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/extra_validations`. To experiment with that code, run `bin/console` for an interactive prompt.
|
4
|
+
|
5
|
+
TODO: Delete this and the text above, and describe your gem
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'extra_validations'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install extra_validations
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
TODO: Write usage instructions here
|
26
|
+
|
27
|
+
## Development
|
28
|
+
|
29
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment.
|
30
|
+
|
31
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release` to create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
32
|
+
|
33
|
+
## Contributing
|
34
|
+
|
35
|
+
1. Fork it ( https://github.com/[my-github-username]/extra_validations/fork )
|
36
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
37
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
38
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
39
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'bundler/gem_tasks'
|
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'bundler/setup'
|
4
|
+
require 'extra_validations'
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require 'irb'
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'extra_validations/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = 'extra_validations'
|
8
|
+
spec.version = ExtraValidations::VERSION
|
9
|
+
spec.authors = ['Lenon Marcel']
|
10
|
+
spec.email = ['lenon.marcel@gmail.com']
|
11
|
+
|
12
|
+
spec.summary = 'Provides some extra validations for ActiveModel.'
|
13
|
+
spec.description = 'Provides some extra validations for ActiveModel.'
|
14
|
+
spec.homepage = 'https://github.com/estoulendo/extra_validations'
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
17
|
+
spec.bindir = 'exe'
|
18
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
19
|
+
spec.require_paths = ['lib']
|
20
|
+
|
21
|
+
spec.add_dependency 'activemodel', '~> 4.2'
|
22
|
+
spec.add_development_dependency 'bundler', '~> 1.7'
|
23
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
24
|
+
spec.add_development_dependency 'rspec', '~> 3.2', '>= 3.2.0'
|
25
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
module ExtraValidations
|
2
|
+
class CollectionLengthValidator < ActiveModel::EachValidator
|
3
|
+
def check_validity!
|
4
|
+
unless options[:in].is_a?(Range)
|
5
|
+
fail ArgumentError, ':in must be a Range'
|
6
|
+
end
|
7
|
+
end
|
8
|
+
|
9
|
+
def validate_each(record, attribute, collection)
|
10
|
+
min = options[:in].begin
|
11
|
+
max = options[:in].end
|
12
|
+
|
13
|
+
if collection.blank? || collection.length < min
|
14
|
+
record.errors.add(attribute, :too_few, count: min)
|
15
|
+
end
|
16
|
+
|
17
|
+
if collection.present? && collection.length > max
|
18
|
+
record.errors.add(attribute, :too_many, count: max)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
module ExtraValidations
|
2
|
+
class CollectionValidator < ActiveModel::EachValidator
|
3
|
+
def validate_each(record, attribute, collection)
|
4
|
+
return if collection.blank?
|
5
|
+
|
6
|
+
collection
|
7
|
+
.each(&:valid?)
|
8
|
+
.each_with_index do |obj, i|
|
9
|
+
obj.errors.each do |attr, error|
|
10
|
+
record.errors.add("#{attribute}/#{i}/#{attr}", error)
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
module ExtraValidations
|
2
|
+
class ExistenceValidator < ActiveModel::EachValidator
|
3
|
+
def check_validity!
|
4
|
+
unless options[:with].is_a?(Proc)
|
5
|
+
fail ArgumentError, ':with must be a Proc'
|
6
|
+
end
|
7
|
+
end
|
8
|
+
|
9
|
+
def validate_each(record, attribute, id)
|
10
|
+
return if id.blank?
|
11
|
+
|
12
|
+
success = options[:with].call(id)
|
13
|
+
record.errors.add(attribute, :not_found) unless success
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
module ExtraValidations
|
2
|
+
class NestedValidator < ActiveModel::EachValidator
|
3
|
+
def validate_each(record, attribute, nested)
|
4
|
+
return if !nested || nested.valid?
|
5
|
+
|
6
|
+
nested.errors.each do |nested_attr, error|
|
7
|
+
record.errors.add("#{attribute}/#{nested_attr}", error)
|
8
|
+
end
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
require 'active_model'
|
2
|
+
|
3
|
+
require 'extra_validations/version'
|
4
|
+
require 'extra_validations/collection_length_validator'
|
5
|
+
require 'extra_validations/collection_validator'
|
6
|
+
require 'extra_validations/nested_validator'
|
7
|
+
require 'extra_validations/existence_validator'
|
8
|
+
|
9
|
+
I18n.load_path.unshift(*Dir[File.expand_path(
|
10
|
+
File.join(%w(extra_validations locale *.yml)), File.dirname(__FILE__))])
|
11
|
+
|
12
|
+
module ExtraValidations
|
13
|
+
end
|
metadata
ADDED
@@ -0,0 +1,122 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: extra_validations
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Lenon Marcel
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-03-30 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: activemodel
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '4.2'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '4.2'
|
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.7'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.7'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '10.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '10.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rspec
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '3.2'
|
62
|
+
- - ">="
|
63
|
+
- !ruby/object:Gem::Version
|
64
|
+
version: 3.2.0
|
65
|
+
type: :development
|
66
|
+
prerelease: false
|
67
|
+
version_requirements: !ruby/object:Gem::Requirement
|
68
|
+
requirements:
|
69
|
+
- - "~>"
|
70
|
+
- !ruby/object:Gem::Version
|
71
|
+
version: '3.2'
|
72
|
+
- - ">="
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
version: 3.2.0
|
75
|
+
description: Provides some extra validations for ActiveModel.
|
76
|
+
email:
|
77
|
+
- lenon.marcel@gmail.com
|
78
|
+
executables: []
|
79
|
+
extensions: []
|
80
|
+
extra_rdoc_files: []
|
81
|
+
files:
|
82
|
+
- ".gitignore"
|
83
|
+
- ".rspec"
|
84
|
+
- ".travis.yml"
|
85
|
+
- Gemfile
|
86
|
+
- LICENSE.txt
|
87
|
+
- README.md
|
88
|
+
- Rakefile
|
89
|
+
- bin/console
|
90
|
+
- bin/setup
|
91
|
+
- extra_validations.gemspec
|
92
|
+
- lib/extra_validations.rb
|
93
|
+
- lib/extra_validations/collection_length_validator.rb
|
94
|
+
- lib/extra_validations/collection_validator.rb
|
95
|
+
- lib/extra_validations/existence_validator.rb
|
96
|
+
- lib/extra_validations/locale/en.yml
|
97
|
+
- lib/extra_validations/nested_validator.rb
|
98
|
+
- lib/extra_validations/version.rb
|
99
|
+
homepage: https://github.com/estoulendo/extra_validations
|
100
|
+
licenses: []
|
101
|
+
metadata: {}
|
102
|
+
post_install_message:
|
103
|
+
rdoc_options: []
|
104
|
+
require_paths:
|
105
|
+
- lib
|
106
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - ">="
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0'
|
111
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
112
|
+
requirements:
|
113
|
+
- - ">="
|
114
|
+
- !ruby/object:Gem::Version
|
115
|
+
version: '0'
|
116
|
+
requirements: []
|
117
|
+
rubyforge_project:
|
118
|
+
rubygems_version: 2.4.5
|
119
|
+
signing_key:
|
120
|
+
specification_version: 4
|
121
|
+
summary: Provides some extra validations for ActiveModel.
|
122
|
+
test_files: []
|