validates_host 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,20 +1,23 @@
1
1
  class DomainNameValidator < ActiveModel::EachValidator
2
2
  def validate_each(record, attribute, value)
3
- record.errors[attribute] << I18n.t("errors.messages.invalid") unless DomainName.new(value).valid?
3
+ key = :"activerecord.errors.models.#{record.class.name.downcase}.attributes.#{attribute.to_s}.invalid"
4
+ record.errors[attribute] << I18n.t(key, :default => :"activerecord.errors.messages.invalid") unless ValidatesHost::DomainName.new(value).valid?
4
5
  end
5
6
  end
6
7
 
7
- class DomainName
8
- def initialize(domain_name)
9
- @domain_name = domain_name
10
- end
8
+ module ValidatesHost
9
+ class DomainName
10
+ def initialize(domain_name)
11
+ @domain_name = domain_name
12
+ end
11
13
 
12
- def valid?
13
- return true if @domain_name.nil?
14
- @domain_name =~ /^[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(:[0-9]{1,5})?(\/.*)?$/
15
- end
14
+ def valid?
15
+ return true if @domain_name.nil?
16
+ @domain_name =~ /^[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(:[0-9]{1,5})?(\/.*)?$/
17
+ end
16
18
 
17
- def domain_name
18
- @domain_name
19
+ def domain_name
20
+ @domain_name
21
+ end
19
22
  end
20
23
  end
@@ -1,20 +1,23 @@
1
1
  class HostNameValidator < ActiveModel::EachValidator
2
2
  def validate_each(record, attribute, value)
3
- record.errors[attribute] << I18n.t("errors.messages.invalid") unless HostName.new(value).valid?
3
+ key = :"activerecord.errors.models.#{record.class.name.downcase}.attributes.#{attribute.to_s}.invalid"
4
+ record.errors[attribute] << I18n.t(key, :default => :"activerecord.errors.messages.invalid") unless ValidatesHost::HostName.new(value).valid?
4
5
  end
5
6
  end
6
7
 
7
- class HostName
8
- def initialize(host_name)
9
- @host_name = host_name
10
- end
8
+ module ValidatesHost
9
+ class HostName
10
+ def initialize(host_name)
11
+ @host_name = host_name
12
+ end
11
13
 
12
- def valid?
13
- return true if @host_name.blank?
14
- @host_name =~ /^[a-z][a-z0-9-]+$/
15
- end
14
+ def valid?
15
+ return true if @host_name.blank?
16
+ @host_name =~ /^[a-z][a-z0-9-]+$/
17
+ end
16
18
 
17
- def host_name
18
- @host_name
19
+ def host_name
20
+ @host_name
21
+ end
19
22
  end
20
23
  end
@@ -1,20 +1,23 @@
1
1
  class IpValidator < ActiveModel::EachValidator
2
2
  def validate_each(record, attribute, value)
3
- record.errors[attribute] << I18n.t("errors.messages.invalid") unless Ip.new(value).valid?
3
+ key = :"activerecord.errors.models.#{record.class.name.downcase}.attributes.#{attribute.to_s}.invalid"
4
+ record.errors[attribute] << I18n.t(key, :default => :"activerecord.errors.messages.invalid") unless ValidatesHost::Ip.new(value).valid?
4
5
  end
5
6
  end
6
7
 
7
- class Ip
8
- def initialize(ip)
9
- @ip = ip
10
- end
8
+ module ValidatesHost
9
+ class Ip
10
+ def initialize(ip)
11
+ @ip = ip
12
+ end
11
13
 
12
- def valid?
13
- return true if @ip.blank?
14
- @ip =~ /^([1-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])(\.([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])){3}$/
15
- end
14
+ def valid?
15
+ return true if @ip.blank?
16
+ @ip =~ /^([1-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])(\.([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])){3}$/
17
+ end
16
18
 
17
- def ip
18
- @ip
19
+ def ip
20
+ @ip
21
+ end
19
22
  end
20
23
  end
@@ -1,3 +1,3 @@
1
1
  module ValidatesHost
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
@@ -4,6 +4,8 @@ describe DomainNameValidator do
4
4
  context "when domain_name is invalid" do
5
5
  before :each do
6
6
  @server = Server.new(:domain_name => "http://")
7
+ I18n.stub(:t).with(:"activerecord.errors.models.server.attributes.domain_name.invalid",
8
+ :default => :"activerecord.errors.messages.invalid").and_return("is invalid")
7
9
  end
8
10
 
9
11
  it "should set object as invalid" do
@@ -4,6 +4,8 @@ describe HostNameValidator do
4
4
  context "when host_name is invalid" do
5
5
  before :each do
6
6
  @server = Server.new(:host_name => "http://")
7
+ I18n.stub(:t).with(:"activerecord.errors.models.server.attributes.host_name.invalid",
8
+ :default => :"activerecord.errors.messages.invalid").and_return("is invalid")
7
9
  end
8
10
 
9
11
  it "should set object as invalid" do
@@ -4,6 +4,8 @@ describe IpValidator do
4
4
  context "when ip is invalid" do
5
5
  before :each do
6
6
  @server = Server.new(:ip => "127.0.0")
7
+ I18n.stub(:t).with(:"activerecord.errors.models.server.attributes.ip.invalid",
8
+ :default => :"activerecord.errors.messages.invalid").and_return("is invalid")
7
9
  end
8
10
 
9
11
  it "should set object as invalid" do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: validates_host
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.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-19 00:00:00.000000000 Z
12
+ date: 2012-08-06 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activerecord
@@ -158,12 +158,18 @@ required_ruby_version: !ruby/object:Gem::Requirement
158
158
  - - ! '>='
159
159
  - !ruby/object:Gem::Version
160
160
  version: '0'
161
+ segments:
162
+ - 0
163
+ hash: -391399657668278307
161
164
  required_rubygems_version: !ruby/object:Gem::Requirement
162
165
  none: false
163
166
  requirements:
164
167
  - - ! '>='
165
168
  - !ruby/object:Gem::Version
166
169
  version: '0'
170
+ segments:
171
+ - 0
172
+ hash: -391399657668278307
167
173
  requirements: []
168
174
  rubyforge_project:
169
175
  rubygems_version: 1.8.24