missing_validators 0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.rspec ADDED
@@ -0,0 +1,4 @@
1
+ --order random
2
+ --colour
3
+ --backtrace
4
+ --format progress
data/.travis.yml ADDED
@@ -0,0 +1,3 @@
1
+ rvm:
2
+ - 1.9.3
3
+ - 1.9.2
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Andrew Gridnev
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,29 @@
1
+ # MissingValidators
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'missing_validators'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install missing_validators
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
@@ -0,0 +1,13 @@
1
+ en:
2
+ errors:
3
+ messages:
4
+ inequality: "can't be equal to %{attr}"
5
+ email: "isn't a valid email address"
6
+ missing_validators:
7
+ matchers:
8
+ ensure_inequality_of:
9
+ failure_message_for_should: "#{model} should ensure inequality on attribute #{attr}"
10
+ failure_message_for_should_not: "#{model} should not ensure inequality on attribute #{attr}"
11
+ ensure_valid_email_format_of:
12
+ failure_message_for_should: "#{model} should ensure valid email format of attribute #{attr}"
13
+ failure_message_for_should_not: "#{model} should not ensure valid email format of attribute #{attr}"
@@ -0,0 +1,27 @@
1
+ RSpec::Matchers.define :ensure_inequality_of do |attribute|
2
+ chain :to do |to|
3
+ @to = to
4
+ end
5
+
6
+ match do |model|
7
+ raise Exception if @to.nil?
8
+
9
+ value = model.send(attribute)
10
+ model.send("#{@to}=", value)
11
+ model.valid?
12
+
13
+ if model.errors.has_key?(attribute)
14
+ model.errors[attribute].include?(I18n.t('errors.messages.inequality', attr: @to))
15
+ end
16
+ end
17
+
18
+ failure_message_for_should do |model|
19
+ I18n.t 'missing_validators.matchers.ensure_inequality_of.failure_message_for_should',
20
+ model: model.class, attr: attribute.inspect
21
+ end
22
+
23
+ failure_message_for_should_not do |model|
24
+ I18n.t 'missing_validators.matchers.ensure_inequality_of.failure_message_for_should_not',
25
+ model: model.class, attr: attribute.inspect
26
+ end
27
+ end
@@ -0,0 +1,20 @@
1
+ RSpec::Matchers.define :ensure_valid_email_format_of do |attribute|
2
+ match do |model|
3
+ model.send("#{attribute}=", "invalid@email_address")
4
+ model.valid?
5
+
6
+ if model.errors.has_key?(attribute)
7
+ model.errors[attribute].include?(I18n.t('errors.messages.email'))
8
+ end
9
+ end
10
+
11
+ failure_message_for_should do |model|
12
+ I18n.t 'missing_validators.matchers.ensure_valid_email_format_of.failure_message_for_should',
13
+ model: model.class
14
+ end
15
+
16
+ failure_message_for_should_not do |model|
17
+ I18n.t 'missing_validators.matchers.ensure_valid_email_format_of.failure_message_for_should_not',
18
+ model: model.class
19
+ end
20
+ end
@@ -0,0 +1,8 @@
1
+ class EmailValidator < ActiveModel::EachValidator
2
+ def validate_each(record, attribute, value)
3
+ unless value =~ /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\z/i
4
+ message = options[:message] || I18n.t('errors.messages.email')
5
+ record.errors[attribute] << message
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,10 @@
1
+ class InequalityValidator < ActiveModel::EachValidator
2
+ def validate_each(record, attribute, value)
3
+ unequal_to_attr = options[:to]
4
+
5
+ if unequal_to_attr.present? && value == record.send(unequal_to_attr.to_sym)
6
+ message = options[:message] || I18n.t('errors.messages.inequality', attr: unequal_to_attr)
7
+ record.errors[attribute] << message
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,3 @@
1
+ module MissingValidators
2
+ VERSION = "0.1"
3
+ end
@@ -0,0 +1,6 @@
1
+ require 'active_model'
2
+ require 'missing_validators/version'
3
+ require 'missing_validators/validators/inequality_validator'
4
+ require 'missing_validators/matchers/ensure_inequality_of_matcher' if defined?(RSpec)
5
+ require 'missing_validators/validators/email_validator'
6
+ require 'missing_validators/matchers/ensure_valid_email_format_of' if defined?(RSpec)
@@ -0,0 +1,21 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/missing_validators/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Andrew Gridnev"]
6
+ gem.email = ["andrew.gridnev@gmail.com"]
7
+ gem.description = %q{Adds some handy validators.}
8
+ gem.summary = %q{Adds some handy validators.}
9
+ gem.homepage = ""
10
+
11
+ gem.files = `git ls-files`.split($\)
12
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
13
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
14
+ gem.name = "missing_validators"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = MissingValidators::VERSION
17
+
18
+ gem.add_development_dependency 'rspec'
19
+
20
+ gem.add_dependency 'activemodel', '> 3.0.0'
21
+ end
@@ -0,0 +1,7 @@
1
+ require 'missing_validators'
2
+
3
+ I18n.load_path << File.expand_path("../../config/locales/en.yml", __FILE__)
4
+
5
+ RSpec.configure do |config|
6
+ config.mock_with :rspec
7
+ end
@@ -0,0 +1,31 @@
1
+ require 'spec_helper'
2
+
3
+ describe EmailValidator do
4
+ class User
5
+ include ActiveModel::Validations
6
+ attr_accessor :email, :name
7
+ validates :email, email: true
8
+ end
9
+
10
+ context do
11
+ subject(:user){ User.new }
12
+
13
+ it { should ensure_valid_email_format_of(:email) }
14
+ it { should_not ensure_valid_email_format_of(:name) }
15
+
16
+ specify "is valid with a valid email address" do
17
+ user.email = "super.user@example.com"
18
+ expect(user).to be_valid
19
+ end
20
+
21
+ specify "is valid with a valid email address" do
22
+ user.email = "user@example.com"
23
+ expect(user).to be_valid
24
+ end
25
+
26
+ specify "fields have different value" do
27
+ user.email = "user_example.com"
28
+ expect(user).to be_invalid
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,44 @@
1
+ require 'spec_helper'
2
+
3
+ describe InequalityValidator do
4
+ class Flight
5
+ include ActiveModel::Validations
6
+ attr_accessor :origin, :destination, :airline
7
+ validates :origin, inequality: { to: :destination }
8
+ end
9
+
10
+ context do
11
+ subject(:flight){ Flight.new }
12
+
13
+ it { should ensure_inequality_of(:origin).to(:destination) }
14
+ it { should_not ensure_inequality_of(:origin).to(:airline) }
15
+
16
+ specify "both fields have same values" do
17
+ flight.origin = flight.destination = "MOW"
18
+ expect(flight).to be_invalid
19
+ end
20
+
21
+ specify "fields have different value" do
22
+ flight.origin = "NYC"
23
+ flight.destination = "MOW"
24
+ expect(flight).to be_valid
25
+ end
26
+
27
+ specify "first field has value, the second is nil" do
28
+ flight.origin = "NYC"
29
+ flight.destination = nil
30
+ expect(flight).to be_valid
31
+ end
32
+
33
+ specify "first field is nil, the second has value" do
34
+ flight.origin = nil
35
+ flight.destination = "NYC"
36
+ expect(flight).to be_valid
37
+ end
38
+
39
+ specify "both fields are nil" do
40
+ flight.origin = flight.destination = nil
41
+ expect(flight).to be_invalid
42
+ end
43
+ end
44
+ end
metadata ADDED
@@ -0,0 +1,88 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: missing_validators
3
+ version: !ruby/object:Gem::Version
4
+ version: '0.1'
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Andrew Gridnev
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-01-28 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
16
+ requirement: &2157735960 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: *2157735960
25
+ - !ruby/object:Gem::Dependency
26
+ name: activemodel
27
+ requirement: &2157735340 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>'
31
+ - !ruby/object:Gem::Version
32
+ version: 3.0.0
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: *2157735340
36
+ description: Adds some handy validators.
37
+ email:
38
+ - andrew.gridnev@gmail.com
39
+ executables: []
40
+ extensions: []
41
+ extra_rdoc_files: []
42
+ files:
43
+ - .gitignore
44
+ - .rspec
45
+ - .travis.yml
46
+ - Gemfile
47
+ - LICENSE
48
+ - README.md
49
+ - Rakefile
50
+ - config/locales/en.yml
51
+ - lib/missing_validators.rb
52
+ - lib/missing_validators/matchers/ensure_inequality_of_matcher.rb
53
+ - lib/missing_validators/matchers/ensure_valid_email_format_of.rb
54
+ - lib/missing_validators/validators/email_validator.rb
55
+ - lib/missing_validators/validators/inequality_validator.rb
56
+ - lib/missing_validators/version.rb
57
+ - missing_validators.gemspec
58
+ - spec/spec_helper.rb
59
+ - spec/validators/email_validator_spec.rb
60
+ - spec/validators/inequality_validator_spec.rb
61
+ homepage: ''
62
+ licenses: []
63
+ post_install_message:
64
+ rdoc_options: []
65
+ require_paths:
66
+ - lib
67
+ required_ruby_version: !ruby/object:Gem::Requirement
68
+ none: false
69
+ requirements:
70
+ - - ! '>='
71
+ - !ruby/object:Gem::Version
72
+ version: '0'
73
+ required_rubygems_version: !ruby/object:Gem::Requirement
74
+ none: false
75
+ requirements:
76
+ - - ! '>='
77
+ - !ruby/object:Gem::Version
78
+ version: '0'
79
+ requirements: []
80
+ rubyforge_project:
81
+ rubygems_version: 1.8.15
82
+ signing_key:
83
+ specification_version: 3
84
+ summary: Adds some handy validators.
85
+ test_files:
86
+ - spec/spec_helper.rb
87
+ - spec/validators/email_validator_spec.rb
88
+ - spec/validators/inequality_validator_spec.rb