format_validators 0.0.7 → 0.0.8

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/CHANGELOG CHANGED
@@ -1,5 +1,10 @@
1
1
  == unreleased changes
2
2
 
3
+ == 0.0.8
4
+
5
+ * Added a basic email format validator
6
+ * Fixed an error in the days of week validator
7
+
3
8
  == 0.0.7
4
9
 
5
10
  * Updated comments for several validators
@@ -4,6 +4,7 @@ A consolidation of a few of the most common validations.
4
4
 
5
5
  == What do we validate
6
6
 
7
+ * Email
7
8
  * Currency
8
9
  * Days of the Week
9
10
  * Florida Counties (a little too custom but someone requested it so here it is)
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.7
1
+ 0.0.8
@@ -1,4 +1,13 @@
1
+ # Checks validity of values supplied for an object's currency/cost attribute
2
+ #
3
+ # example usage:
4
+ # validates :atrribute_to_check, :currency_format => true
1
5
  class CurrencyFormatValidator < ActiveModel::EachValidator
6
+ # Compares object's currency/cost attribute against regex /^\d+(\.\d{2})?$/ for validity
7
+ # @param [BasicRecord] record, This is the object needing validation
8
+ # @param [Symbol] attribute, The attribute being validated, in this case :cost
9
+ # @param [String] value, The value of the attribute to compare against the regex
10
+ # @return [Boolean] True or false depending on if the value of the record's attribute is in the proper format
2
11
  def validate_each record, attribute, value
3
12
  strict_mode = options[:strict] || false
4
13
 
@@ -10,7 +10,7 @@ class DaysOfWeekValidator < ActiveModel::EachValidator
10
10
  # @return [Boolean] True or false depending on if the value of the record's attribute is contained within the days_of_week array
11
11
  def validate_each(record, attribute, value)
12
12
  value = value.to_s.downcase
13
- record.errors[attribute] << value + " is not a valid day of the week or abbreviation" unless self.days_of_week.include?(value)
13
+ record.errors[attribute] << value + " is not a valid day of the week or abbreviation" unless DaysOfWeekValidator.days_of_week.include?(value)
14
14
  end
15
15
 
16
16
  # All available days of the week and their abbreviations to be compared against for validation
@@ -0,0 +1,12 @@
1
+ # Custom Validations for Email format
2
+ # This is not a great regular expression but it will work for now
3
+ #
4
+ # e.g. validates :home_email, :email_format => true
5
+ # e.g. validates :work_email, :email_format => { :allow_blank => true, :if => :method_returns_true }
6
+ class EmailFormatValidator < ActiveModel::EachValidator
7
+ def validate_each record, attribute, value
8
+ format = /^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/
9
+ message = attribute.to_s.humanize + ' doesn\'t match an acceptable format.'
10
+ record.errors[attribute] << (options[:message] || message ) unless value =~ format
11
+ end
12
+ end
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "format_validators"
8
- s.version = "0.0.7"
8
+ s.version = "0.0.8"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Jeremiah Hemphill"]
12
- s.date = "2012-04-11"
12
+ s.date = "2012-04-25"
13
13
  s.description = "Complex format validators"
14
14
  s.email = "jeremiah@cloudspace.com"
15
15
  s.extra_rdoc_files = [
@@ -29,6 +29,7 @@ Gem::Specification.new do |s|
29
29
  "app/validators/currency_format_validator.rb",
30
30
  "app/validators/days_of_week_validator.rb",
31
31
  "app/validators/devise_password_validator.rb",
32
+ "app/validators/email_format_validator.rb",
32
33
  "app/validators/florida_counties_validator.rb",
33
34
  "app/validators/phone_format_validator.rb",
34
35
  "app/validators/ssn_format_validator.rb",
@@ -79,6 +80,7 @@ Gem::Specification.new do |s|
79
80
  "spec/validators/currency_format_validator_spec.rb",
80
81
  "spec/validators/days_of_week_validator_spec.rb",
81
82
  "spec/validators/devise_password_validator_spec.rb",
83
+ "spec/validators/email_format_validator_spec.rb",
82
84
  "spec/validators/florida_counties_integration_spec.rb",
83
85
  "spec/validators/florida_counties_spec.rb",
84
86
  "spec/validators/phone_format_validator_spec.rb",
@@ -0,0 +1,21 @@
1
+ require 'spec_helper'
2
+
3
+ describe EmailFormatValidator do
4
+ describe ".validate_each" do
5
+ before(:each) do
6
+ @options = {:attributes => {}}
7
+ @validator = EmailFormatValidator.new(@options)
8
+ @record = BasicRecord.new(:email)
9
+ end
10
+
11
+ it "should accept emails of the format jeremiah@cloudspace.com" do
12
+ @record.errors[:email].should_not_receive("<<")
13
+ @validator.validate_each(@record, :email, "jeremiah@cloudspace.com")
14
+ end
15
+
16
+ it "should not accept emails of the format jeremiah@cloudspace@com" do
17
+ @record.errors[:email].should_receive("<<")
18
+ @validator.validate_each(@record, :email, "jeremiah@cloudspace@com")
19
+ end
20
+ end
21
+ end
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: format_validators
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.0.7
5
+ version: 0.0.8
6
6
  platform: ruby
7
7
  authors:
8
8
  - Jeremiah Hemphill
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2012-04-11 00:00:00 Z
13
+ date: 2012-04-25 00:00:00 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: rails
@@ -166,6 +166,7 @@ files:
166
166
  - app/validators/currency_format_validator.rb
167
167
  - app/validators/days_of_week_validator.rb
168
168
  - app/validators/devise_password_validator.rb
169
+ - app/validators/email_format_validator.rb
169
170
  - app/validators/florida_counties_validator.rb
170
171
  - app/validators/phone_format_validator.rb
171
172
  - app/validators/ssn_format_validator.rb
@@ -216,6 +217,7 @@ files:
216
217
  - spec/validators/currency_format_validator_spec.rb
217
218
  - spec/validators/days_of_week_validator_spec.rb
218
219
  - spec/validators/devise_password_validator_spec.rb
220
+ - spec/validators/email_format_validator_spec.rb
219
221
  - spec/validators/florida_counties_integration_spec.rb
220
222
  - spec/validators/florida_counties_spec.rb
221
223
  - spec/validators/phone_format_validator_spec.rb
@@ -234,7 +236,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
234
236
  requirements:
235
237
  - - ">="
236
238
  - !ruby/object:Gem::Version
237
- hash: -2572695772213769967
239
+ hash: 4500710221847082787
238
240
  segments:
239
241
  - 0
240
242
  version: "0"