extra_validations 0.1.0 → 0.1.1

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: a352c493f0a5a9e7079bf9d25fc69d6f866a9589
4
- data.tar.gz: c4b3e96aca1b7773ebe74d4bc95206c8a6c95706
3
+ metadata.gz: b6c67fad9b6d632a739ee5597a3587f26c604b0e
4
+ data.tar.gz: 7c92df5f7924b4da49e673871c93a14f50299602
5
5
  SHA512:
6
- metadata.gz: fd6f237ae867ba6da83839a14576c24172d52c81d416d7e5c19fe924103e496bff10e135878f9ebe92d7dc0340658d4dd6917568bffd44f4795bc1a6580185a8
7
- data.tar.gz: a1f33cbedc6e08dd42f28bab7ae2d8de21cb8d0a81277abc08356f19a9dd74ce4129a9ed5f38f5618996090422247956f4c4117678b396104bf7b4d300908a2f
6
+ metadata.gz: 3930615ba2c63ca0dc24d2aa078e63eca5b06e2480bd08a7e045a1b9bd3f74e1c32a151d33c7a9e1879d895f5e2eb15d6417ea0ccb07b1ea320e3baf365fd890
7
+ data.tar.gz: 62bae244afdbb9a59395cb05f0d33f14a49b8a42c0629df4e99e2f50a90c6ca30241725d1db6f40a6826e13548e0281b5f42da2b771c56f4f0264f5798c3d00b
data/.travis.yml CHANGED
@@ -1,4 +1,7 @@
1
1
  language: ruby
2
2
  rvm:
3
+ - 2.0.0
4
+ - 2.1.0
3
5
  - 2.2.0
6
+ - ruby-head
4
7
  script: bundle exec rspec
data/README.md CHANGED
@@ -1,8 +1,9 @@
1
1
  # ExtraValidations
2
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.
3
+ [![Build Status][travis_badge]][travis_link]
4
+ [![Code Climate][cclimate_badge]][cclimate_link]
4
5
 
5
- TODO: Delete this and the text above, and describe your gem
6
+ This gem provides some extra validations for ActiveModel objects.
6
7
 
7
8
  ## Installation
8
9
 
@@ -22,18 +23,59 @@ Or install it yourself as:
22
23
 
23
24
  ## Usage
24
25
 
25
- TODO: Write usage instructions here
26
+ The following validators are available:
27
+
28
+ Collection length:
29
+
30
+ ```ruby
31
+ validates :attr, collection_length: { in: 1..5 }
32
+ ```
33
+
34
+ Collection members validator (ensures that each member of the collection is
35
+ valid):
36
+
37
+ ```ruby
38
+ validates :attr, collection: true
39
+ ```
40
+
41
+ Existence validator:
42
+
43
+ ```ruby
44
+ validates :model_id, existence: ->(value) { Model.exists?(value) }
45
+ ```
46
+
47
+ Nested validator (ensures that the object is valid):
48
+
49
+ ```ruby
50
+ validates :model, nested: true
51
+ ```
52
+
53
+ Uniqueness validator:
54
+
55
+ ```ruby
56
+ validates :email, uniqueness: ->(email) { Model.exists?(email: email) }
57
+ ```
26
58
 
27
59
  ## Development
28
60
 
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.
61
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run
62
+ `bin/console` for an interactive prompt that will allow you to experiment.
30
63
 
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).
64
+ To install this gem onto your local machine, run `bundle exec rake install`.
65
+ To release a new version, update the version number in `version.rb`, and then
66
+ run `bundle exec rake release` to create a git tag for the version, push git
67
+ commits and tags, and push the `.gem` file to
68
+ [rubygems.org](https://rubygems.org).
32
69
 
33
70
  ## Contributing
34
71
 
35
- 1. Fork it ( https://github.com/[my-github-username]/extra_validations/fork )
72
+ 1. Fork it ( https://github.com/estoulendo/extra_validations/fork )
36
73
  2. Create your feature branch (`git checkout -b my-new-feature`)
37
74
  3. Commit your changes (`git commit -am 'Add some feature'`)
38
75
  4. Push to the branch (`git push origin my-new-feature`)
39
76
  5. Create a new Pull Request
77
+
78
+ [travis_badge]: https://travis-ci.org/estoulendo/extra_validations.svg?branch=master
79
+ [travis_link]: https://travis-ci.org/estoulendo/extra_validations
80
+ [cclimate_badge]: https://codeclimate.com/github/estoulendo/extra_validations/badges/gpa.svg
81
+ [cclimate_link]: https://codeclimate.com/github/estoulendo/extra_validations
@@ -19,7 +19,7 @@ Gem::Specification.new do |spec|
19
19
  spec.require_paths = ['lib']
20
20
 
21
21
  spec.add_dependency 'activemodel', '~> 4.2'
22
- spec.add_development_dependency 'bundler', '~> 1.7'
22
+ spec.add_development_dependency 'bundler'
23
23
  spec.add_development_dependency 'rake', '~> 10.0'
24
24
  spec.add_development_dependency 'rspec', '~> 3.2', '>= 3.2.0'
25
25
  end
@@ -5,6 +5,7 @@ require 'extra_validations/collection_length_validator'
5
5
  require 'extra_validations/collection_validator'
6
6
  require 'extra_validations/nested_validator'
7
7
  require 'extra_validations/existence_validator'
8
+ require 'extra_validations/uniqueness_validator'
8
9
 
9
10
  I18n.load_path.unshift(*Dir[File.expand_path(
10
11
  File.join(%w(extra_validations locale *.yml)), File.dirname(__FILE__))])
@@ -1,11 +1,35 @@
1
1
  module ExtraValidations
2
+ # Validates the length of a collection.
3
+ #
4
+ # Suppose that you have the following class:
5
+ #
6
+ # class Book
7
+ # include ActiveModel::Model
8
+ # include ExtraValidations
9
+ #
10
+ # attr_accessor :authors
11
+ #
12
+ # validates :authors, collection_length: 1..5
13
+ # end
14
+ #
15
+ # This validator will make sure that the _authors_ array will have at least
16
+ # 1 item and at most 5 items:
17
+ #
18
+ # book = Book.new
19
+ # book.authors = []
20
+ # book.valid? # => false
21
+ #
22
+ # book.authors = [Author.new]
23
+ # book.valid? # => true
24
+ #
2
25
  class CollectionLengthValidator < ActiveModel::EachValidator
3
26
  def check_validity!
4
- unless options[:in].is_a?(Range)
5
- fail ArgumentError, ':in must be a Range'
6
- end
27
+ fail ArgumentError, ':in must be a Range' unless options[:in].is_a?(Range)
7
28
  end
8
29
 
30
+ # @param record An object that has ActiveModel::Validations included
31
+ # @param attribute [Symbol]
32
+ # @param collection [Array]
9
33
  def validate_each(record, attribute, collection)
10
34
  min = options[:in].begin
11
35
  max = options[:in].end
@@ -1,5 +1,36 @@
1
1
  module ExtraValidations
2
+ # Makes sure that each member of the collection is a valid object.
3
+ #
4
+ # Suppose that you have the following class:
5
+ #
6
+ # class Book
7
+ # include ActiveModel::Model
8
+ # include ExtraValidations
9
+ #
10
+ # attr_accessor :authors
11
+ #
12
+ # validates :authors, collection: true
13
+ # end
14
+ #
15
+ # This validator will call +#valid?+ for each member of the collection:
16
+ #
17
+ # book = Book.new
18
+ # book.authors = [invalid_author1, valid_author1]
19
+ # book.valid? # => false
20
+ #
21
+ # book.authors = [valid_author1, valid_author2]
22
+ # book.valid? # => true
23
+ #
24
+ # Each validation error found on members will be appended on to the errors
25
+ # object:
26
+ #
27
+ # book.errors.messages # => {:"authors/1/name"=>["can't be blank"]}
28
+ #
2
29
  class CollectionValidator < ActiveModel::EachValidator
30
+ # @param record An object that has ActiveModel::Validations included
31
+ # @param attribute [Symbol]
32
+ # @param collection [Array] An array of objects that have
33
+ # ActiveModel::Validations included
3
34
  def validate_each(record, attribute, collection)
4
35
  return if collection.blank?
5
36
 
@@ -1,4 +1,27 @@
1
1
  module ExtraValidations
2
+ # Makes sure that an object/record exists.
3
+ #
4
+ # Suppose that you have the following class:
5
+ #
6
+ # class Book
7
+ # include ActiveModel::Model
8
+ # include ExtraValidations
9
+ #
10
+ # attr_accessor :author_id
11
+ #
12
+ # validates :author_id, existence: -> (id) { Author.exists?(id) }
13
+ # end
14
+ #
15
+ # This validator will execute the given block and, if it returns false, the
16
+ # object will not be valid:
17
+ #
18
+ # book = Book.new
19
+ # book.author_id = 100
20
+ # book.valid? # => false
21
+ #
22
+ # book.author_id = Author.first.id
23
+ # book.valid? # => true
24
+ #
2
25
  class ExistenceValidator < ActiveModel::EachValidator
3
26
  def check_validity!
4
27
  unless options[:with].is_a?(Proc)
@@ -6,6 +29,9 @@ module ExtraValidations
6
29
  end
7
30
  end
8
31
 
32
+ # @param record An object that has ActiveModel::Validations included
33
+ # @param attribute [Symbol]
34
+ # @param id [Integer]
9
35
  def validate_each(record, attribute, id)
10
36
  return if id.blank?
11
37
 
@@ -8,3 +8,4 @@ en:
8
8
  one: "must have at most one"
9
9
  other: "must have at most %{count}"
10
10
  not_found: "not found"
11
+ taken: "has already been taken"
@@ -1,5 +1,34 @@
1
1
  module ExtraValidations
2
+ # Makes sure that the nested object is valid.
3
+ #
4
+ # Suppose that you have the following class:
5
+ #
6
+ # class Book
7
+ # include ActiveModel::Model
8
+ # include ExtraValidations
9
+ #
10
+ # attr_accessor :author
11
+ #
12
+ # validates :author, nested: true
13
+ # end
14
+ #
15
+ # This validator will call +#valid?+ on the nested object:
16
+ #
17
+ # book = Book.new
18
+ # book.author = invalid_author
19
+ # book.valid? # => false
20
+ #
21
+ # book.author = valid_author
22
+ # book.valid? # => true
23
+ #
24
+ # Each validation error found will be appended on to the errors object:
25
+ #
26
+ # book.errors.messages # => {:"author/name"=>["can't be blank"]}
27
+ #
2
28
  class NestedValidator < ActiveModel::EachValidator
29
+ # @param record An object that has ActiveModel::Validations included
30
+ # @param attribute [Symbol]
31
+ # @param nested An object that has ActiveModel::Validations included
3
32
  def validate_each(record, attribute, nested)
4
33
  return if !nested || nested.valid?
5
34
 
@@ -0,0 +1,42 @@
1
+ module ExtraValidations
2
+ # Makes sure that an object/record is unique.
3
+ #
4
+ # Suppose that you have the following class:
5
+ #
6
+ # class User
7
+ # include ActiveModel::Model
8
+ # include ExtraValidations
9
+ #
10
+ # attr_accessor :email
11
+ #
12
+ # validates :email, uniqueness: ->(email) { User.exists?(email: email) }
13
+ # end
14
+ #
15
+ # This validator will execute the given block and, if it returns true, the
16
+ # object will not be valid:
17
+ #
18
+ # user = User.new
19
+ # user.email = 'foo@example.com'
20
+ # user.valid? # => false
21
+ #
22
+ # user.email = 'bar@example.com'
23
+ # user.valid? # => true
24
+ #
25
+ class UniquenessValidator < ActiveModel::EachValidator
26
+ def check_validity!
27
+ unless options[:with].is_a?(Proc)
28
+ fail ArgumentError, ':with must be a Proc'
29
+ end
30
+ end
31
+
32
+ # @param record An object that has ActiveModel::Validations included
33
+ # @param attribute [Symbol]
34
+ # @param value
35
+ def validate_each(record, attribute, value)
36
+ return if value.blank?
37
+
38
+ exists = options[:with].call(value)
39
+ record.errors.add(attribute, :taken) if exists
40
+ end
41
+ end
42
+ end
@@ -1,3 +1,3 @@
1
1
  module ExtraValidations
2
- VERSION = '0.1.0'
2
+ VERSION = '0.1.1'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: extra_validations
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Lenon Marcel
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2015-03-30 00:00:00.000000000 Z
11
+ date: 2015-06-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activemodel
@@ -28,16 +28,16 @@ dependencies:
28
28
  name: bundler
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - "~>"
31
+ - - ">="
32
32
  - !ruby/object:Gem::Version
33
- version: '1.7'
33
+ version: '0'
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - "~>"
38
+ - - ">="
39
39
  - !ruby/object:Gem::Version
40
- version: '1.7'
40
+ version: '0'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: rake
43
43
  requirement: !ruby/object:Gem::Requirement
@@ -95,6 +95,7 @@ files:
95
95
  - lib/extra_validations/existence_validator.rb
96
96
  - lib/extra_validations/locale/en.yml
97
97
  - lib/extra_validations/nested_validator.rb
98
+ - lib/extra_validations/uniqueness_validator.rb
98
99
  - lib/extra_validations/version.rb
99
100
  homepage: https://github.com/estoulendo/extra_validations
100
101
  licenses: []