format_validator 0.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/CHANGELOG ADDED
@@ -0,0 +1,3 @@
1
+ ## v0.0.1
2
+
3
+ * Initial release
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in format_validator.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Jamal El Milahi
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
+ # FormatValidator
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'format_validator'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install format_validator
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 'Add 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,9 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rake/testtask'
3
+
4
+ desc 'Default: run specs'
5
+ task default: :test
6
+
7
+ Rake::TestTask.new do |test|
8
+ test.pattern = './spec/**/*_spec.rb'
9
+ end
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'format_validator/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'format_validator'
8
+ spec.version = FormatValidator::VERSION
9
+ spec.authors = ['Jamal El Milahi']
10
+ spec.email = ['jamal@elmilahi.com']
11
+ spec.description = %q{Active Model missing format validators}
12
+ spec.summary = %q{format_validator is a gem that adds the missing format validators to Active Model}
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.3'
22
+ spec.add_development_dependency 'rake'
23
+ end
@@ -0,0 +1,11 @@
1
+ module ActiveModel
2
+ module Validations
3
+ class EmailFormatValidator < ActiveModel::EachValidator
4
+ def validate_each(object, attribute, value)
5
+ unless value =~ /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i
6
+ object.errors[attribute] << (options[:message] || 'is not valid')
7
+ end
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,11 @@
1
+ module ActiveModel
2
+ module Validations
3
+ class UrlFormatValidator < ActiveModel::EachValidator
4
+ def validate_each(object, attribute, value)
5
+ unless value =~ /\Ahttps?:\/\/([^\s:@]+:[^\s:@]*@)?[A-Za-z\d\-]+(\.[A-Za-z\d\-]+)+\.?(:\d{1,5})?([\/?]\S*)?\Z/i
6
+ object.errors[attribute] << (options[:message] || 'is not valid')
7
+ end
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,3 @@
1
+ module FormatValidator
2
+ VERSION = '0.0.1'
3
+ end
@@ -0,0 +1,11 @@
1
+ module ActiveModel
2
+ module Validations
3
+ class ZipCodeFormatValidator < ActiveModel::EachValidator
4
+ def validate_each(object, attribute, value)
5
+ unless value =~ /^\d{5}(-\d{4})?$/
6
+ object.errors[attribute] << (options[:message] || 'is not valid')
7
+ end
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,4 @@
1
+ require 'format_validator/version'
2
+ require 'format_validator/email_format_validator'
3
+ require 'format_validator/url_format_validator'
4
+ require 'format_validator/zip_code_format_validator'
@@ -0,0 +1,34 @@
1
+ require_relative '../spec_helper'
2
+
3
+ describe 'EmailFormatValidator' do
4
+ describe '#validate_each' do
5
+ let(:klass) do
6
+ Class.new do
7
+ attr_accessor :email
8
+ include ActiveModel::Validations
9
+ validates :email, email_format: true
10
+ end
11
+ end
12
+
13
+ subject { klass.new }
14
+
15
+ describe 'valid' do
16
+ it 'should not add any error' do
17
+ subject.email = 'foo@bar.com'
18
+ subject.valid?.must_equal true
19
+ subject.errors.messages.must_be_empty
20
+ end
21
+ end
22
+
23
+ describe 'invalid' do
24
+ ['invalid_email', 'foo@example. com'].each do |invalid_email|
25
+ it "should add error when email is #{invalid_email}" do
26
+ subject.email = invalid_email
27
+ subject.valid?.must_equal false
28
+ subject.errors.messages.size.must_equal 1
29
+ subject.errors.messages[:email].must_equal ['is not valid']
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,36 @@
1
+ require_relative '../spec_helper'
2
+
3
+ describe 'UrlFormatValidator' do
4
+ describe '#validate_each' do
5
+ let(:klass) do
6
+ Class.new do
7
+ attr_accessor :url
8
+ include ActiveModel::Validations
9
+ validates :url, url_format: true
10
+ end
11
+ end
12
+
13
+ subject { klass.new }
14
+
15
+ describe 'valid' do
16
+ %w[http://foo.com https://foo.com].each do |_url|
17
+ it "should not add any error when the url is #{_url}" do
18
+ subject.url = _url
19
+ subject.valid?.must_equal true
20
+ subject.errors.messages.must_be_empty
21
+ end
22
+ end
23
+ end
24
+
25
+ describe 'invalid' do
26
+ %w[foo.com http://foo foo].each do |_url|
27
+ it "should add error when url is (#{_url})" do
28
+ subject.url = _url
29
+ subject.valid?.must_equal false
30
+ subject.errors.messages.size.must_equal 1
31
+ subject.errors.messages[:url].must_equal ['is not valid']
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,36 @@
1
+ require_relative '../spec_helper'
2
+
3
+ describe 'ZipCodeFormatValidator' do
4
+ describe '#validate_each' do
5
+ let(:klass) do
6
+ Class.new do
7
+ attr_accessor :zip_code
8
+ include ActiveModel::Validations
9
+ validates :zip_code, zip_code_format: true
10
+ end
11
+ end
12
+
13
+ subject { klass.new }
14
+
15
+ describe 'valid' do
16
+ %w[12345 12345-6789].each do |zip|
17
+ it 'should not add any error' do
18
+ subject.zip_code = zip
19
+ subject.valid?.must_equal true
20
+ subject.errors.messages.must_be_empty
21
+ end
22
+ end
23
+ end
24
+
25
+ describe 'invalid' do
26
+ ['123', '123456', '123 45', '1234X', '12345-123', '12345-234X', '12345 6789'].each do |invalid_zip_code|
27
+ it "should add error when zip code is #{invalid_zip_code}" do
28
+ subject.zip_code = invalid_zip_code
29
+ subject.valid?.must_equal false
30
+ subject.errors.messages.size.must_equal 1
31
+ subject.errors.messages[:zip_code].must_equal ['is not valid']
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,4 @@
1
+ gem 'minitest'
2
+ require 'active_model'
3
+ require 'format_validator'
4
+ require 'minitest/autorun'
metadata ADDED
@@ -0,0 +1,99 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: format_validator
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Jamal El Milahi
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-03-10 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '1.3'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '1.3'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rake
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ description: Active Model missing format validators
47
+ email:
48
+ - jamal@elmilahi.com
49
+ executables: []
50
+ extensions: []
51
+ extra_rdoc_files: []
52
+ files:
53
+ - .gitignore
54
+ - CHANGELOG
55
+ - Gemfile
56
+ - LICENSE.txt
57
+ - README.md
58
+ - Rakefile
59
+ - format_validator.gemspec
60
+ - lib/format_validator.rb
61
+ - lib/format_validator/email_format_validator.rb
62
+ - lib/format_validator/url_format_validator.rb
63
+ - lib/format_validator/version.rb
64
+ - lib/format_validator/zip_code_format_validator.rb
65
+ - spec/format_validator/email_format_validator_spec.rb
66
+ - spec/format_validator/url_format_validator_spec.rb
67
+ - spec/format_validator/zip_code_format_validator_spec.rb
68
+ - spec/spec_helper.rb
69
+ homepage: ''
70
+ licenses:
71
+ - MIT
72
+ post_install_message:
73
+ rdoc_options: []
74
+ require_paths:
75
+ - lib
76
+ required_ruby_version: !ruby/object:Gem::Requirement
77
+ none: false
78
+ requirements:
79
+ - - ! '>='
80
+ - !ruby/object:Gem::Version
81
+ version: '0'
82
+ required_rubygems_version: !ruby/object:Gem::Requirement
83
+ none: false
84
+ requirements:
85
+ - - ! '>='
86
+ - !ruby/object:Gem::Version
87
+ version: '0'
88
+ requirements: []
89
+ rubyforge_project:
90
+ rubygems_version: 1.8.24
91
+ signing_key:
92
+ specification_version: 3
93
+ summary: format_validator is a gem that adds the missing format validators to Active
94
+ Model
95
+ test_files:
96
+ - spec/format_validator/email_format_validator_spec.rb
97
+ - spec/format_validator/url_format_validator_spec.rb
98
+ - spec/format_validator/zip_code_format_validator_spec.rb
99
+ - spec/spec_helper.rb