validatious 0.1.0

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,2 @@
1
+ --color
2
+ --format=progress
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'http://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in validatious.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2011 Joel Moss
2
+
3
+ Permission is hereby granted, free of charge, to any person
4
+ obtaining a copy of this software and associated documentation
5
+ files (the "Software"), to deal in the Software without
6
+ restriction, including without limitation the rights to use,
7
+ copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ copies of the Software, and to permit persons to whom the
9
+ Software is furnished to do so, subject to the following
10
+ conditions:
11
+
12
+ The above copyright notice and this permission notice shall be
13
+ included in all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
17
+ OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
19
+ HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
20
+ WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21
+ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22
+ OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,75 @@
1
+ Validatious -- A collection of delicious Rails validators
2
+ =========================================================
3
+
4
+ ## DESCRIPTION
5
+
6
+ No matter what app you are working on, Rails never seems to include all the
7
+ validations you need, and you end having to repeat yourself over and over by
8
+ writing regex validations and/or custom validators.
9
+
10
+ Validatious solves this problem by packaging up all these often used, but
11
+ missing Rails validators, into one single RubyGem.
12
+
13
+
14
+ ## INSTALLATION
15
+
16
+ The best way to install Validatious is with RubyGems:
17
+
18
+ $ [sudo] gem install validatious
19
+
20
+ If you're installing from source, you can use [Bundler][bundler] to pick up all the
21
+ gems:
22
+
23
+ $ bundle install # ([more info](http://gembundler.com/bundle_install.html))
24
+
25
+ [bundler]: http://gembundler.com/
26
+
27
+
28
+ ## USAGE
29
+
30
+ All the validators in Validatious are designed to work with Rails 3 and up, and
31
+ take advantage of ActiveModel::Validator. This means that they are all used in the
32
+ usual Rails way:
33
+
34
+ validates :url, :url => true
35
+
36
+ The old Rails 2 style validations are also supported:
37
+
38
+ validates_url_format_of :url
39
+
40
+
41
+ ## Validations
42
+
43
+ Validatious currently provides the following validators:
44
+
45
+
46
+ ### URL
47
+
48
+ This validates the format of a URL, ensuring that the attribute conforms to the
49
+ correct format of a URL.
50
+
51
+ validates :url, :url => true
52
+
53
+ or
54
+
55
+ validates_url_format_of :url
56
+
57
+
58
+ ## CONTRIBUTE
59
+
60
+ If you'd like to hack on Validatious, start by forking my repo on GitHub:
61
+
62
+ http://github.com/joelmoss/validatious
63
+
64
+ To get all of the dependencies, install the gem first. The best way to get
65
+ your changes merged back into core is as follows:
66
+
67
+ 1. Clone down your fork
68
+ 1. Create a thoughtfully named topic branch to contain your change
69
+ 1. Hack away
70
+ 1. Add tests and make sure everything still passes by running `rake`
71
+ 1. If you are adding new functionality, document it in the README
72
+ 1. Do not change the version number, I will do that on my end
73
+ 1. If necessary, rebase your commits into logical chunks, without errors
74
+ 1. Push the branch up to GitHub
75
+ 1. Send a pull request to the joelmoss/validatious project.
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env rake
2
+ require 'bundler/gem_tasks'
3
+
4
+ require 'rspec/core'
5
+ require 'rspec/core/rake_task'
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ task :default => :spec
@@ -0,0 +1,11 @@
1
+ module ActiveModel
2
+ module Validations
3
+ module HelperMethods
4
+
5
+ def validates_url_format_of(*attr_names)
6
+ validates_with UrlValidator, _merge_attributes(attr_names)
7
+ end
8
+
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,4 @@
1
+ en:
2
+ errors:
3
+ messages:
4
+ invalid_url: "is not a valid URL"
@@ -0,0 +1,26 @@
1
+ module Validatious
2
+ module Validators
3
+
4
+ # == Active Model Url Validator
5
+ class UrlValidator < ActiveModel::EachValidator
6
+ IPv4_PART = /\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5]/ # 0-255
7
+ REGEX = %r{
8
+ \A
9
+ https?:// # http:// or https://
10
+ ([^\s:@]+:[^\s:@]*@)? # optional username:pw@
11
+ ( (xn--)?[a-z0-9]+([-.][a-z0-9]+)*\.[a-z]{2,6}\.? | # domain (including Punycode/IDN)...
12
+ #{IPv4_PART}(\.#{IPv4_PART}){3} ) # or IPv4
13
+ (:\d{1,5})? # optional port
14
+ ([/?]\S*)? # optional /whatever or ?whatever
15
+ \Z
16
+ }iux
17
+
18
+ def validate_each(record, attribute, value)
19
+ record.errors.add(attribute, :invalid_url, options) if value.to_s !~ REGEX
20
+ end
21
+ end
22
+ end
23
+ end
24
+
25
+ # Compatibility with ActiveModel validates method which matches option keys to their validator class
26
+ ActiveModel::Validations::UrlValidator = Validatious::Validators::UrlValidator
@@ -0,0 +1,3 @@
1
+ module Validatious
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,13 @@
1
+ require "validatious/version"
2
+
3
+ module Validatious
4
+ module Validators
5
+ # nothing to see here, move along...
6
+ end
7
+ end
8
+
9
+ require "validatious/validators/url_validator"
10
+ require "validatious/helper_methods"
11
+
12
+ require 'active_support/i18n'
13
+ I18n.load_path << File.dirname(__FILE__) + '/validatious/locale/en.yml'
@@ -0,0 +1,18 @@
1
+ require 'active_model'
2
+ require "validatious"
3
+
4
+ # Requires supporting ruby files with custom matchers and macros, etc,
5
+ # in spec/support/ and its subdirectories.
6
+ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each { |f| require f }
7
+
8
+ RSpec.configure do |config|
9
+ config.mock_with :rspec
10
+
11
+ config.before(:all) do
12
+ DeferredGarbageCollection.start
13
+ end
14
+
15
+ config.after(:all) do
16
+ DeferredGarbageCollection.reconsider
17
+ end
18
+ end
@@ -0,0 +1,21 @@
1
+ # Taken from http://makandra.com/notes/950-speed-up-rspec-by-deferring-garbage-collection
2
+ class DeferredGarbageCollection
3
+
4
+ DEFERRED_GC_THRESHOLD = (ENV['DEFER_GC'] || 10.0).to_f
5
+
6
+ @@last_gc_run = Time.now
7
+
8
+ def self.start
9
+ GC.disable if DEFERRED_GC_THRESHOLD > 0
10
+ end
11
+
12
+ def self.reconsider
13
+ if DEFERRED_GC_THRESHOLD > 0 && Time.now - @@last_gc_run >= DEFERRED_GC_THRESHOLD
14
+ GC.enable
15
+ GC.start
16
+ GC.disable
17
+ @@last_gc_run = Time.now
18
+ end
19
+ end
20
+
21
+ end
@@ -0,0 +1,13 @@
1
+ class Post
2
+ include ActiveModel::Validations
3
+ include ActiveModel::Validations::Callbacks
4
+
5
+ attr_accessor :title, :url
6
+
7
+ def initialize(attributes = {})
8
+ attributes.each do |key, value|
9
+ send "#{key}=", value
10
+ end
11
+ end
12
+
13
+ end
@@ -0,0 +1,9 @@
1
+ require "spec_helper"
2
+
3
+ describe Validatious, 'HelperMethods' do
4
+
5
+ it 'should define class validation methods' do
6
+ Post.should respond_to(:validates_url_format_of)
7
+ end
8
+
9
+ end
@@ -0,0 +1,68 @@
1
+ # encoding: utf-8
2
+
3
+ require "spec_helper"
4
+
5
+ describe Validatious::Validators::UrlValidator do
6
+
7
+ before(:each) do
8
+ Post.validates_url_format_of(:url)
9
+ end
10
+
11
+ let(:post) { post = Post.new(:title => "The title", :url => "invalid URL") }
12
+
13
+ [
14
+ 'http://example.com',
15
+ 'http://example.com/',
16
+ 'http://www.example.com/',
17
+ 'http://sub.domain.example.com/',
18
+ 'http://bbc.co.uk',
19
+ 'http://example.com?foo',
20
+ 'http://example.com?url=http://example.com',
21
+ 'http://example.com:8000',
22
+ 'http://www.sub.example.com/page.html?foo=bar&baz=%23#anchor',
23
+ 'http://user:pass@example.com',
24
+ 'http://user:@example.com',
25
+ 'http://example.com/~user',
26
+ 'http://example.xy', # Not a real TLD, but we're fine with anything of 2-6 chars
27
+ 'http://example.museum',
28
+ 'http://1.0.255.249',
29
+ 'http://1.2.3.4:80',
30
+ 'HttP://example.com',
31
+ 'https://example.com',
32
+ # 'http://räksmörgås.nu', # IDN
33
+ 'http://xn--rksmrgs-5wao1o.nu', # Punycode
34
+ 'http://example.com.', # Explicit TLD root period
35
+ 'http://example.com./foo'
36
+ ].each do |url|
37
+ describe url.inspect do
38
+ it "should be valid" do
39
+ post.url = url
40
+ post.should be_valid
41
+ end
42
+ end
43
+ end
44
+
45
+ [
46
+ nil, 1, "", " ", "url",
47
+ "www.example.com",
48
+ "http://ex ample.com",
49
+ "http://example.com/foo bar",
50
+ 'http://256.0.0.1',
51
+ 'http://u:u:u@example.com',
52
+ 'http://r?ksmorgas.com',
53
+
54
+ # These can all be valid local URLs, but should not be considered valid
55
+ # for public consumption.
56
+ "http://example",
57
+ "http://example.c",
58
+ 'http://example.toolongtld'
59
+ ].each do |url|
60
+ describe url.inspect do
61
+ it "should not be valid" do
62
+ post.url = url
63
+ post.should_not be_valid
64
+ end
65
+ end
66
+ end
67
+
68
+ end
@@ -0,0 +1,21 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/validatious/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Joel Moss"]
6
+ gem.email = ["joel@developwithstyle.com"]
7
+ gem.description = %q{A collection of delicious Rails validators}
8
+ gem.summary = %q{A collection of delicious Rails validators}
9
+ gem.homepage = 'https://github.com/joelmoss/validatious'
10
+
11
+ gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
12
+ gem.files = `git ls-files`.split("\n")
13
+ gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
14
+ gem.name = "validatious"
15
+ gem.require_paths = ['lib']
16
+ gem.version = Validatious::VERSION
17
+
18
+ gem.add_dependency 'activemodel'
19
+
20
+ gem.add_development_dependency "rspec"
21
+ end
metadata ADDED
@@ -0,0 +1,98 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: validatious
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.1.0
6
+ platform: ruby
7
+ authors:
8
+ - Joel Moss
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2011-06-28 00:00:00 +01:00
14
+ default_executable:
15
+ dependencies:
16
+ - !ruby/object:Gem::Dependency
17
+ name: activemodel
18
+ prerelease: false
19
+ requirement: &id001 !ruby/object:Gem::Requirement
20
+ none: false
21
+ requirements:
22
+ - - ">="
23
+ - !ruby/object:Gem::Version
24
+ version: "0"
25
+ type: :runtime
26
+ version_requirements: *id001
27
+ - !ruby/object:Gem::Dependency
28
+ name: rspec
29
+ prerelease: false
30
+ requirement: &id002 !ruby/object:Gem::Requirement
31
+ none: false
32
+ requirements:
33
+ - - ">="
34
+ - !ruby/object:Gem::Version
35
+ version: "0"
36
+ type: :development
37
+ version_requirements: *id002
38
+ description: A collection of delicious Rails validators
39
+ email:
40
+ - joel@developwithstyle.com
41
+ executables: []
42
+
43
+ extensions: []
44
+
45
+ extra_rdoc_files: []
46
+
47
+ files:
48
+ - .gitignore
49
+ - .rspec
50
+ - Gemfile
51
+ - LICENSE
52
+ - README.md
53
+ - Rakefile
54
+ - lib/validatious.rb
55
+ - lib/validatious/helper_methods.rb
56
+ - lib/validatious/locale/en.yml
57
+ - lib/validatious/validators/url_validator.rb
58
+ - lib/validatious/version.rb
59
+ - spec/spec_helper.rb
60
+ - spec/support/deferred_garbage_collection.rb
61
+ - spec/support/models/post.rb
62
+ - spec/validatious/helper_methods_spec.rb
63
+ - spec/validatious/validators/url_spec.rb
64
+ - validatious.gemspec
65
+ has_rdoc: true
66
+ homepage: https://github.com/joelmoss/validatious
67
+ licenses: []
68
+
69
+ post_install_message:
70
+ rdoc_options: []
71
+
72
+ require_paths:
73
+ - lib
74
+ required_ruby_version: !ruby/object:Gem::Requirement
75
+ none: false
76
+ requirements:
77
+ - - ">="
78
+ - !ruby/object:Gem::Version
79
+ version: "0"
80
+ required_rubygems_version: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ">="
84
+ - !ruby/object:Gem::Version
85
+ version: "0"
86
+ requirements: []
87
+
88
+ rubyforge_project:
89
+ rubygems_version: 1.3.9.1
90
+ signing_key:
91
+ specification_version: 3
92
+ summary: A collection of delicious Rails validators
93
+ test_files:
94
+ - spec/spec_helper.rb
95
+ - spec/support/deferred_garbage_collection.rb
96
+ - spec/support/models/post.rb
97
+ - spec/validatious/helper_methods_spec.rb
98
+ - spec/validatious/validators/url_spec.rb