format_for_extensions 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +11 -11
- data/VERSION +1 -1
- data/config/format_for_extensions.yml +2 -2
- data/format_for_extensions.gemspec +2 -1
- data/lib/format_for_extensions/config.rb +11 -1
- data/lib/format_for_extensions.rb +14 -16
- data/pkg/format_for_extensions-0.1.0.gem +0 -0
- data/test/lib/format_for_extensions/test_config.rb +24 -4
- data/test/test_format_for_extensions.rb +1 -1
- metadata +4 -3
data/README.rdoc
CHANGED
@@ -1,18 +1,18 @@
|
|
1
|
-
=
|
1
|
+
= format_for_extensions
|
2
2
|
|
3
|
-
Tired of repeating 'validates_format_of' with the same regex expression across your models only to validate the same email address and postal code fields? So am I. Use
|
3
|
+
Tired of repeating 'validates_format_of' with the same regex expression across your models only to validate the same email address and postal code fields? So am I. Use format_for_extensions to dynamically define reusable formats for any field you want.
|
4
4
|
|
5
|
-
By default,
|
5
|
+
By default, format_for_extensions comes with smart formats for email addresses and postal codes. Even better, no code repetition! Use constructs that make sense such as "validates_email_for :account_owner" and "validates_postal_code_for :address_postal_code".
|
6
6
|
|
7
7
|
|
8
8
|
== System Requirements
|
9
|
-
|
9
|
+
format_for_extensions should work with Rails 2.3.5 and above. However, I have not yet tested this with Rails 3.1.
|
10
10
|
|
11
11
|
|
12
12
|
== Installation
|
13
|
-
Modify your Gemfile to include
|
13
|
+
Modify your Gemfile to include format_for_extensions:
|
14
14
|
|
15
|
-
gem '
|
15
|
+
gem 'format_for_extensions'
|
16
16
|
|
17
17
|
Run `bundle install`. You thought it would be harder?
|
18
18
|
|
@@ -30,11 +30,11 @@ By default, ArValidationExtensions supports email and postal code formats. Thes
|
|
30
30
|
|
31
31
|
=== Configuration
|
32
32
|
|
33
|
-
Default configurations are specified in the gem's config/
|
33
|
+
Default configurations are specified in the gem's config/format_for_extensions.yml file. If you wish to modify the default regular expressions or messages reported when validation fails, simply copy this file to the config directory in your Rails root folder. You can also manually create [rails_root]/config/format_for_extensions.yml if you'd like to.
|
34
34
|
|
35
35
|
The format of this file is as follows:
|
36
36
|
|
37
|
-
#
|
37
|
+
# format_for_extensions.yml
|
38
38
|
en:
|
39
39
|
email:
|
40
40
|
regex: /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/
|
@@ -46,9 +46,9 @@ If you modify this file, you will need to bounce your application server (or res
|
|
46
46
|
|
47
47
|
|
48
48
|
=== The Hotness
|
49
|
-
You can dynamically create your own format validators! For instance, add this to your [rails_root]/config/
|
49
|
+
You can dynamically create your own format validators! For instance, add this to your [rails_root]/config/format_for_extensions.yml file:
|
50
50
|
|
51
|
-
#
|
51
|
+
# format_for_extensions.yml
|
52
52
|
en:
|
53
53
|
hotness:
|
54
54
|
regex: /^hot/
|
@@ -69,7 +69,7 @@ As you might have guessed, the gem is driven off of the configuration file. The
|
|
69
69
|
- Support some other highly repetitious fields, such as phone number.
|
70
70
|
|
71
71
|
|
72
|
-
== Contributing to
|
72
|
+
== Contributing to format_for_extensions
|
73
73
|
|
74
74
|
* Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
|
75
75
|
* Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.1.
|
1
|
+
0.1.1
|
@@ -1,9 +1,9 @@
|
|
1
1
|
# Default validation configurations...
|
2
2
|
en:
|
3
3
|
postal_code:
|
4
|
-
|
4
|
+
regexp: "^[0-9]{5}(-[0-9]{4})?|[ABCEGHJKLMNPRSTVXY]\d[A-Z] *\d[A-Z]\d?$"
|
5
5
|
message: "is invalid. Valid formats: 94105-0011 or 94105 or T2X 1V4 or T2X1V4"
|
6
6
|
|
7
7
|
email:
|
8
|
-
|
8
|
+
regexp: "^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$"
|
9
9
|
message: "is invalid. The address should be in a format similar to 'user@example.com'."
|
@@ -5,7 +5,7 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{format_for_extensions}
|
8
|
-
s.version = "0.1.
|
8
|
+
s.version = "0.1.1"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Karle Durante"]
|
@@ -27,6 +27,7 @@ Gem::Specification.new do |s|
|
|
27
27
|
"format_for_extensions.gemspec",
|
28
28
|
"lib/format_for_extensions.rb",
|
29
29
|
"lib/format_for_extensions/config.rb",
|
30
|
+
"pkg/format_for_extensions-0.1.0.gem",
|
30
31
|
"test/helper.rb",
|
31
32
|
"test/lib/format_for_extensions/test_config.rb",
|
32
33
|
"test/models/abstract_model.rb",
|
@@ -19,6 +19,16 @@ module FormatForExtensions
|
|
19
19
|
end
|
20
20
|
|
21
21
|
@@config[locale]
|
22
|
-
end
|
22
|
+
end
|
23
|
+
|
24
|
+
# Returns the configured fields
|
25
|
+
def self.fields
|
26
|
+
values.keys
|
27
|
+
end
|
28
|
+
|
29
|
+
# returns the configured properties (of the first field)
|
30
|
+
def self.properties
|
31
|
+
values[fields.first].keys
|
32
|
+
end
|
23
33
|
end
|
24
34
|
end
|
@@ -11,7 +11,8 @@ module FormatForExtensions
|
|
11
11
|
require 'format_for_extensions/config'
|
12
12
|
|
13
13
|
begin
|
14
|
-
FIELDS = FormatForExtensions::Config.
|
14
|
+
FIELDS = FormatForExtensions::Config.fields
|
15
|
+
PROPERTIES = FormatForExtensions::Config.properties
|
15
16
|
rescue Exception => e
|
16
17
|
puts "Exception loading config: #{e.inspect}"
|
17
18
|
raise(RuntimeError, "Your format_for_extensions.yml file seems to be invalid, perhaps you've mis-keyed something?")
|
@@ -19,11 +20,11 @@ module FormatForExtensions
|
|
19
20
|
|
20
21
|
FIELDS.each do |field|
|
21
22
|
define_method("validates_#{field}_for") do |*attr_names|
|
22
|
-
configuration = {:with => self.send("#{field}
|
23
|
+
configuration = {:with => self.send("#{field}_regexp"),
|
23
24
|
:message => self.send("#{field}_message")
|
24
25
|
}
|
25
26
|
|
26
|
-
# Add in user supplied options (yes, users can still override the
|
27
|
+
# Add in user supplied options (yes, users can still override the regexp and
|
27
28
|
# message with custom options)
|
28
29
|
configuration.update(attr_names.extract_options!)
|
29
30
|
|
@@ -33,22 +34,19 @@ module FormatForExtensions
|
|
33
34
|
end
|
34
35
|
|
35
36
|
# Compose accessor methods for each of the specified field types
|
36
|
-
# e.g.
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
end
|
44
|
-
|
45
|
-
define_method("#{field}_message".to_sym) do
|
46
|
-
message = FormatForExtensions::Config.values[field]['message']
|
47
|
-
raise(ArgumentError, "Your format_for_extensions.yml is missing the mapping '#{field}: message:'") if message.blank?
|
37
|
+
# e.g. postal_code_regexp, postal_code_message, etc...
|
38
|
+
FIELDS.each do |field|
|
39
|
+
PROPERTIES.each do |property|
|
40
|
+
define_method("#{field}_#{property}".to_sym) do
|
41
|
+
value = FormatForExtensions::Config.values[field][property]
|
42
|
+
|
43
|
+
raise(ArgumentError, "Your format_for_extensions.yml is missing the mapping '#{property}' for '#{field}'") if value.blank?
|
48
44
|
|
49
|
-
|
45
|
+
return property == 'regexp' ? Regexp.new(value) : value
|
46
|
+
end
|
50
47
|
end
|
51
48
|
end
|
49
|
+
|
52
50
|
end
|
53
51
|
end
|
54
52
|
end
|
Binary file
|
@@ -2,11 +2,31 @@ require 'helper'
|
|
2
2
|
require 'format_for_extensions/config'
|
3
3
|
|
4
4
|
class TestConfig < Test::Unit::TestCase
|
5
|
-
|
6
|
-
|
5
|
+
context "When loading the default yml file" do
|
6
|
+
context "and inspecting field values" do
|
7
|
+
setup {@config = FormatForExtensions::Config.values}
|
8
|
+
should "return the postal code message" do
|
9
|
+
assert_equal "is invalid. Valid formats: 94105-0011 or 94105 or T2X 1V4 or T2X1V4", @config['postal_code']['message']
|
10
|
+
end
|
11
|
+
|
12
|
+
should "return the email message" do
|
13
|
+
assert_equal "is invalid. The address should be in a format similar to 'user@example.com'.", @config['email']['message']
|
14
|
+
end
|
15
|
+
end
|
7
16
|
|
8
|
-
|
9
|
-
|
17
|
+
should "return the configured fields" do
|
18
|
+
fields = FormatForExtensions::Config.fields
|
19
|
+
['postal_code', 'email'].each do |field|
|
20
|
+
assert fields.include?(field)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
should "return the configured properties" do
|
25
|
+
properties = FormatForExtensions::Config.properties
|
26
|
+
['regexp', 'message'].each do |property|
|
27
|
+
assert properties.include?(property)
|
28
|
+
end
|
29
|
+
end
|
10
30
|
end
|
11
31
|
|
12
32
|
end
|
@@ -14,7 +14,7 @@ class TestFormatForExtensions < Test::Unit::TestCase
|
|
14
14
|
|
15
15
|
should "define data accessor methods for each configured field" do
|
16
16
|
@fields.each do |field|
|
17
|
-
assert ActiveRecord::Base.respond_to?("#{field}
|
17
|
+
assert ActiveRecord::Base.respond_to?("#{field}_regexp")
|
18
18
|
assert ActiveRecord::Base.respond_to?("#{field}_message")
|
19
19
|
end
|
20
20
|
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: format_for_extensions
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 25
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 1
|
9
|
-
-
|
10
|
-
version: 0.1.
|
9
|
+
- 1
|
10
|
+
version: 0.1.1
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Karle Durante
|
@@ -130,6 +130,7 @@ files:
|
|
130
130
|
- format_for_extensions.gemspec
|
131
131
|
- lib/format_for_extensions.rb
|
132
132
|
- lib/format_for_extensions/config.rb
|
133
|
+
- pkg/format_for_extensions-0.1.0.gem
|
133
134
|
- test/helper.rb
|
134
135
|
- test/lib/format_for_extensions/test_config.rb
|
135
136
|
- test/models/abstract_model.rb
|