validates_formatting_of 0.6.9 → 0.7.0

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.
data/.travis.yml CHANGED
@@ -1,3 +1,4 @@
1
+ script: SKIP_SIMPLECOV=true bundle exec rspec spec
1
2
  rvm:
2
3
  - 1.9.2
3
4
  - 1.9.3
data/CHANGELOG.md CHANGED
@@ -1,3 +1,13 @@
1
+ ## v0.7.0
2
+
3
+ * Removed :ip_address validation method
4
+ * Bug fix for :simple_email validation. Identical to :email validation.
5
+
6
+ There were a few changes between v0.5.0 and v0.7.0, however I neglected to updated them here. They are as follows:
7
+
8
+ * Refactored entire gem for better testing
9
+ * Deprecating and then removing :ip_address validation in favor of :ip_address_v4 validation.
10
+
1
11
  ## v0.5.0
2
12
  * Dropped support for Ruby 1.8.7
3
13
  * Dropped support for REE
data/README.markdown CHANGED
@@ -1,7 +1,10 @@
1
- # validates_formatting_of
1
+ # Validates Formatting Of
2
+
2
3
  [![Build Status](https://secure.travis-ci.org/mattdbridges/validates_formatting_of.png)](http://travis-ci.org/mattdbridges/validates_formatting_of)
3
4
  [![Dependency Status](https://gemnasium.com/mattdbridges/validates_formatting_of.png?travis)](https://gemnasium.com/mattdbridges/validates_formatting_of)
4
5
 
6
+ [![Code Climate](https://codeclimate.com/badge.png)](https://codeclimate.com/github/mattdbridges/validates_formatting_of)
7
+
5
8
  The `validates_formatting_of` gem adds several convenient methods to validate things such as emails, urls, and phone numbers in a Rails application.
6
9
 
7
10
  # Unsupported versions
@@ -23,13 +26,23 @@ Then bundle install:
23
26
 
24
27
  # Usage
25
28
 
26
- Using validates_formatting_of is as simple as using Rails' built-in validation methods in models.
29
+ Using `validates_formatting_of` is as simple as using Rails' built-in validation methods in models.
30
+
31
+ ```ruby
32
+ class User < ActiveRecord::Base
33
+ validates_formatting_of :email
34
+ end
35
+ ```
27
36
 
28
- class User < ActiveRecord::Base
29
- validates_formatting_of :email, :using => :email
30
- end
37
+ If the name of the column is identical to the validation method you would use, it auto-selects that validation method. Thus, this is a shortcut for the following:
31
38
 
32
- This call will ensure that the user-provided email is a valid email. This way, you will not need to find or write your own regex to validate. All of that logic is contained within `validates_formatting_of`
39
+ ```ruby
40
+ class User < ActiveRecord::Base
41
+ validates_formatting_of :email, :using => :email
42
+ end
43
+ ```
44
+
45
+ This call will ensure that the user-provided email is a valid email. This way, you will not need to find or write your own regex to validate. All of that logic is contained within `validates_formatting_of`.
33
46
 
34
47
  # Rails validation options still available
35
48
 
@@ -48,83 +61,128 @@ You can still add the following options when using `validates_formatting_of`:
48
61
 
49
62
  ### Email
50
63
 
51
- class User < ActiveRecord::Base
52
- validates_formatting_of :email, :using => :email
53
- end
64
+ ```ruby
65
+ class User < ActiveRecord::Base
66
+ validates_formatting_of :email, :using => :email
67
+ end
68
+ ```
69
+
70
+ **Note**: If you use `client_side_validations` then you need to use the following validation method:
71
+
72
+ ```ruby
73
+ class User < ActiveRecord::Base
74
+ validates_formatting_of :email, :using => :simple_email
75
+ end
76
+ ```
54
77
 
55
78
  ### URL
56
79
 
57
- class Sites < ActiveRecord::Base
58
- validates_formatting_of :website, :using => :url
59
- end
80
+ ```ruby
81
+ class Sites < ActiveRecord::Base
82
+ validates_formatting_of :website, :using => :url
83
+ end
84
+ ```
60
85
 
61
86
  ### Alpha
62
87
 
63
- class Name < ActiveRecord::Base
64
- validates_formatting_of :first_name, :using => :alpha
65
- end
88
+ ```ruby
89
+ class Name < ActiveRecord::Base
90
+ validates_formatting_of :first_name, :using => :alpha
91
+ end
92
+ ```
66
93
 
67
94
  ### Alphanumeric
68
95
 
69
- class Sites < ActiveRecord::Base
70
- validates_formatting_of :text, :using => :alphanum
71
- end
96
+ ```ruby
97
+ class Sites < ActiveRecord::Base
98
+ validates_formatting_of :text, :using => :alphanum
99
+ end
100
+ ```
72
101
 
73
102
  ### Credit Card (Visa, Mastercard, Discover, and American Express)
74
103
 
75
- class Purchases < ActiveRecord::Base
76
- validates_formatting_of :cc, :using => :credit_card
77
- end
104
+ ```ruby
105
+ class Purchases < ActiveRecord::Base
106
+ validates_formatting_of :cc, :using => :credit_card
107
+ end
108
+ ```
78
109
 
79
110
  ### US Zipcodes
80
111
 
81
- class Location < ActiveRecord::Base
82
- validates_formatting_of :zipcode, :using => :us_zip
83
- end
112
+ ```ruby
113
+ class Location < ActiveRecord::Base
114
+ validates_formatting_of :zipcode, :using => :us_zip
115
+ end
116
+ ```
84
117
 
85
118
  ### US Phone numbers
86
119
 
87
- class Phones < ActiveRecord::Base
88
- validates_formatting_of :phone, :using => :us_phone
89
- end
120
+ ```ruby
121
+ class Phones < ActiveRecord::Base
122
+ validates_formatting_of :phone, :using => :us_phone
123
+ end
124
+ ```
90
125
 
91
- ### IP Address
126
+ ### IPv4
127
+
128
+ ```ruby
129
+ class Location < ActiveRecord::Base
130
+ validates_formatting_of :ip, :using => :ip_address_v4
131
+ end
132
+ ```
92
133
 
93
- class Location < ActiveRecord::Base
94
- validates_formatting_of :website, :using => :ip_address
95
- end
96
134
  ### Social Security Number
97
135
 
98
- class User < ActiveRecord::Base
99
- validates_formatting_of :ssn, :using => :ssn
100
- end
136
+ ```ruby
137
+ class User < ActiveRecord::Base
138
+ validates_formatting_of :ssn, :using => :ssn
139
+ end
140
+ ```
101
141
 
102
142
  ### Hex Colors
103
143
 
104
- class Color < ActiveRecord::Base
105
- validates_formatting_of :color, :using => :hex_color
106
- end
144
+ ```ruby
145
+ class Color < ActiveRecord::Base
146
+ validates_formatting_of :color, :using => :hex_color
147
+ end
148
+ ```
107
149
 
108
150
  ### Dollar Amount
109
151
 
110
- class Invoice < ActiveRecord::Base
111
- validates_formatting_of :amount, :using => :dollars
112
- end
113
-
114
- # Customizable
152
+ ```ruby
153
+ class Invoice < ActiveRecord::Base
154
+ validates_formatting_of :amount, :using => :dollars
155
+ end
156
+ ```
115
157
 
116
- If, for any reason, you want to use your own regex instead of Rail's built-in methods, you can specify what you want to use with the `:regex` option. For example,
158
+ # Over-writable
117
159
 
160
+ If, for any reason, you want to overwrite the regex specified in the gem, you can using the `:with` option just like in ActiveModel's built-in `validates_format_of`.
118
161
 
119
- class Person < ActiveRecord::Base
120
- validates_formatting_of :first_name, :regex => /[A-Z]/i
121
- end
162
+ ```ruby
163
+ class Person < ActiveRecord::Base
164
+ validates_formatting_of :first_name, :with => /[A-Z]/i
165
+ end
166
+ ```
122
167
 
123
168
  # Development and Contribution
124
169
 
125
- It is very easy to contribute to this gem. Full documentation to do so will be added in the near future.
170
+ To add a simple regex validation, all that is necessary is to add a single line to the `Method` module in the `lib/method.rb` file (with a line or two of documentation explaining it's use).
171
+
172
+ For example:
173
+
174
+ ```ruby
175
+ module Method
176
+ # This :validation_name method is for example purposes only.
177
+ # You can use this in production, but it would require a value of 'example regex' to pass.
178
+ add :validation_name, %r{example regex}iux, "is a message for validation method :validation_name"
179
+ end
180
+ ```
181
+
182
+ 1. Fork it
183
+ 2. Make it awesome (with passing tests)
184
+ 3. Create a new Pull Request.
126
185
 
127
186
  # Have Ideas?
128
187
 
129
188
  Do you use a particular pattern on a regular basis that isn't here or you would like to contribute? For now, [create a new issue](https://github.com/mattdbridges/validates_formatting_of/issues/new) in the issue tracker. I would be more than happy to consider adding it to the project.
130
-
@@ -2,9 +2,6 @@ require 'validates_formatting_of/validation'
2
2
  require 'validates_formatting_of/validation_addition'
3
3
 
4
4
  module ValidatesFormattingOf
5
-
6
- class MissingValidation < StandardError; end
7
-
8
5
  module Method
9
6
 
10
7
  extend ValidationAddition
@@ -15,7 +12,7 @@ module ValidatesFormattingOf
15
12
  # Thie version of the email exists to support common validation libraries (such
16
13
  # as client_side_validations) that require access to Rails' validation. Look-behinds
17
14
  # are not supported in javascript.
18
- add :simple_email, %r{\A([^@\s]+)@((?:(?!-)[-a-z0-9]+(?<!-)\.)+[a-z]{2,})\Z}i, "is not a valid email"
15
+ add :simple_email, %r{\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z}i, "is not a valid email"
19
16
 
20
17
  # Taken from Ryan Bates' screencast on extracting gems. Works extremely well. Thanks Ryan!
21
18
  # (slightly revised to work on MRI 1.8.7 and ree)
@@ -44,7 +41,6 @@ module ValidatesFormattingOf
44
41
  add :us_phone, %r{\A(\((\d{3})\)|\d{3})[ |\.|\-]?(\d{3})[ |\.|\-]?(\d{4})\Z}, "is not a valid phone number"
45
42
 
46
43
  # IP Address validation
47
- add :ip_address, %r{\A(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\Z}, "is not a valid IPv4 address"
48
44
  add :ip_address_v4, %r{\A(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\Z}, "is not a valid IPv4 address"
49
45
 
50
46
  # Social Security Number pattern
@@ -1,6 +1,10 @@
1
1
  module ValidatesFormattingOf
2
2
 
3
- class InvalidRegularExpression < StandardError; end
3
+ class InvalidRegularExpression < StandardError
4
+ def initialize(name)
5
+ super("You must specify a Regexp, a proc, or a lambda for the #{name.inspect} validation.")
6
+ end
7
+ end
4
8
 
5
9
  class Validation
6
10
  attr_reader :name, :regex, :message
@@ -8,7 +12,7 @@ module ValidatesFormattingOf
8
12
  def initialize(name, regexp, message = "is not correctly formatted")
9
13
  callable = regexp.respond_to? :call
10
14
  if !callable && regexp.class.to_s != "Regexp"
11
- raise InvalidRegularExpression, "You must specify a Regexp, a proc, or a lambda for the #{name.inspect} validation."
15
+ raise InvalidRegularExpression.new(name)
12
16
  end
13
17
  @name, @regex, @message = name, regexp, message
14
18
  end
@@ -1,4 +1,13 @@
1
+ require 'active_support/core_ext/object/blank'
2
+
1
3
  module ValidatesFormattingOf
4
+
5
+ class MissingValidation < StandardError
6
+ def initialize(method)
7
+ super("The validation method #{method.to_sym.inspect} has not been defined.")
8
+ end
9
+ end
10
+
2
11
  module ValidationAddition
3
12
  attr_reader :validations
4
13
 
@@ -8,18 +17,20 @@ module ValidatesFormattingOf
8
17
  end
9
18
 
10
19
  def find(attribute, opts = {})
11
- method = opts[:using].nil? ? attribute : opts[:using]
12
- if !exists? method
13
- raise MissingValidation, "The validation method #{method.to_sym.inspect} has not been defined."
14
- end
20
+ method = opts.fetch(:using, attribute)
21
+ raise MissingValidation.new(method) if missing? method
15
22
  if method.to_sym == :ip_address
16
- warn "[DEPRECATION] The :ip_address validation for `validates_formatting_of` is DEPRECATED. Please update your model validations to use :ip_address_v4. This method will be removed by version 0.7.0."
23
+ raise ArgumentError, "[REMOVED] The :ip_address validation for `validates_formatting_of` has been removed since version 0.7.0. Please update your model validations to use :ip_address_v4."
17
24
  end
18
25
  @validations[method.to_sym]
19
26
  end
20
27
 
28
+ def missing?(name)
29
+ !exists?(name)
30
+ end
31
+
21
32
  def exists?(name)
22
- !@validations[name.to_sym].nil?
33
+ @validations[name.to_sym].present?
23
34
  end
24
35
  end
25
36
  end
@@ -1,3 +1,3 @@
1
1
  module ValidatesFormattingOf
2
- VERSION = "0.6.9"
2
+ VERSION = "0.7.0"
3
3
  end
data/spec/spec_helper.rb CHANGED
@@ -1,5 +1,10 @@
1
1
  require 'simplecov'
2
- SimpleCov.start
2
+
3
+ unless ENV["SKIP_SIMPLECOV"] == 'true'
4
+ SimpleCov.start do
5
+ load_adapter 'test_frameworks'
6
+ end
7
+ end
3
8
 
4
9
  require 'validates_formatting_of'
5
10
  require 'active_model'
@@ -109,33 +109,18 @@ describe ValidatesFormattingOf::ModelAdditions do
109
109
  end
110
110
  end
111
111
 
112
- describe "ip_address" do
113
- class IPAddress < TestActiveRecord
114
- attr_accessor :ip
115
- validates_formatting_of :ip, :using => :ip_address
116
- end
117
- it "validates that the IP address provided is valid" do
118
- IPAddress.new(:ip => '10.10.10').should_not be_valid
119
- IPAddress.new(:ip => '999.10.10.20').should_not be_valid
120
- IPAddress.new(:ip => '2222.22.22.22').should_not be_valid
121
- IPAddress.new(:ip => '22.2222.22.2').should_not be_valid
122
- IPAddress.new(:ip => '127.0.0.1').should be_valid
123
- IPAddress.new(:ip => '132.254.111.10').should be_valid
124
- end
125
- end
126
-
127
112
  describe "ip_address_v4" do
128
- class IPAddress2 < TestActiveRecord
113
+ class IPAddress < TestActiveRecord
129
114
  attr_accessor :ipv4
130
115
  validates_formatting_of :ipv4, :using => :ip_address_v4
131
116
  end
132
117
  it "validates that the IP address provided is valid" do
133
- IPAddress2.new(:ipv4 => '10.10.10').should_not be_valid
134
- IPAddress2.new(:ipv4 => '999.10.10.20').should_not be_valid
135
- IPAddress2.new(:ipv4 => '2222.22.22.22').should_not be_valid
136
- IPAddress2.new(:ipv4 => '22.2222.22.2').should_not be_valid
137
- IPAddress2.new(:ipv4 => '127.0.0.1').should be_valid
138
- IPAddress2.new(:ipv4 => '132.254.111.10').should be_valid
118
+ IPAddress.new(:ipv4 => '10.10.10').should_not be_valid
119
+ IPAddress.new(:ipv4 => '999.10.10.20').should_not be_valid
120
+ IPAddress.new(:ipv4 => '2222.22.22.22').should_not be_valid
121
+ IPAddress.new(:ipv4 => '22.2222.22.2').should_not be_valid
122
+ IPAddress.new(:ipv4 => '127.0.0.1').should be_valid
123
+ IPAddress.new(:ipv4 => '132.254.111.10').should be_valid
139
124
  end
140
125
  end
141
126
 
@@ -10,6 +10,9 @@ module ValidatesFormattingOf
10
10
  before do
11
11
  TestAdding.add :email, /email/i
12
12
  end
13
+ after do
14
+ TestAdding.instance_variable_set("@validations", nil)
15
+ end
13
16
  it "should be able to add new validations" do
14
17
  TestAdding.add :another, /another/i
15
18
  TestAdding.validations.count.should == 2
@@ -27,9 +30,17 @@ module ValidatesFormattingOf
27
30
  it "should raise an error if the method does not exist" do
28
31
  expect { TestAdding.find(:fake) }.to raise_error MissingValidation
29
32
  end
30
- it "should be able to determine if the method exists" do
33
+ it "should be able to determine if the method does not exist" do
31
34
  TestAdding.exists?(:email).should be_true
32
35
  TestAdding.exists?(:non_existent).should be_false
33
36
  end
37
+ it "should be able to accept strings for validation names (for namespacing)" do
38
+ TestAdding.add "namespace:phone", /namespace/i
39
+ TestAdding.exists?("namespace:phone").should be_true
40
+ end
41
+ it "should be able to determine if the method is missing" do
42
+ TestAdding.missing?(:non_existent).should be_true
43
+ TestAdding.missing?(:email).should be_false
44
+ end
34
45
  end
35
46
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: validates_formatting_of
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.9
4
+ version: 0.7.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-07-16 00:00:00.000000000 Z
12
+ date: 2012-07-20 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activemodel