validates_host 0.3.0 → 1.3.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.
Files changed (55) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +5 -4
  3. data/.rubocop.yml +57 -0
  4. data/.ruby-gemset +1 -0
  5. data/.travis.yml +45 -4
  6. data/Gemfile +3 -1
  7. data/README.md +15 -9
  8. data/Rakefile +9 -4
  9. data/gemfiles/Gemfile.rails3 +6 -0
  10. data/gemfiles/Gemfile.rails4 +5 -0
  11. data/gemfiles/Gemfile.rails5 +5 -0
  12. data/gemfiles/Gemfile.rails6 +5 -0
  13. data/lib/validates_host.rb +14 -5
  14. data/lib/validates_host/domain_name.rb +15 -0
  15. data/lib/validates_host/domain_name_validator.rb +10 -17
  16. data/lib/validates_host/host_name.rb +15 -0
  17. data/lib/validates_host/host_name_validator.rb +10 -17
  18. data/lib/validates_host/ip.rb +17 -0
  19. data/lib/validates_host/ip_validator.rb +10 -17
  20. data/lib/validates_host/{shoulda-matchers/domain_name_matcher.rb → require_a_valid_domain_name_matcher.rb} +9 -11
  21. data/lib/validates_host/{shoulda-matchers/host_name_matcher.rb → require_a_valid_host_name_matcher.rb} +9 -11
  22. data/lib/validates_host/{shoulda-matchers/ip_matcher.rb → require_a_valid_ip_matcher.rb} +9 -11
  23. data/lib/validates_host/{shoulda-matchers/subnet_matcher.rb → require_a_valid_subnet_matcher.rb} +9 -11
  24. data/lib/validates_host/subnet.rb +15 -0
  25. data/lib/validates_host/subnet_validator.rb +10 -17
  26. data/lib/validates_host/version.rb +3 -1
  27. data/spec/fake_app/server.rb +19 -5
  28. data/spec/fake_app/subnet.rb +11 -3
  29. data/spec/shoulda/matchers/active_model/require_a_valid_domain_name_matcher_spec.rb +23 -0
  30. data/spec/shoulda/matchers/active_model/require_a_valid_host_name_matcher_spec.rb +23 -0
  31. data/spec/shoulda/matchers/active_model/require_a_valid_ip_matcher_spec.rb +23 -0
  32. data/spec/shoulda/matchers/active_model/require_a_valid_subnet_matcher_spec.rb +23 -0
  33. data/spec/spec_helper.rb +11 -8
  34. data/spec/validates_host/domain_name_validator_spec.rb +23 -22
  35. data/spec/validates_host/host_name_validator_spec.rb +23 -22
  36. data/spec/validates_host/ip_validator_spec.rb +38 -22
  37. data/spec/validates_host/subnet_validator_spec.rb +23 -22
  38. data/validates_host.gemspec +23 -19
  39. metadata +84 -95
  40. data/lib/validates_host/remarkable.rb +0 -4
  41. data/lib/validates_host/remarkable/domain_name_matcher.rb +0 -29
  42. data/lib/validates_host/remarkable/host_name_matcher.rb +0 -29
  43. data/lib/validates_host/remarkable/ip_matcher.rb +0 -29
  44. data/lib/validates_host/remarkable/subnet_matcher.rb +0 -29
  45. data/lib/validates_host/shoulda-matchers.rb +0 -4
  46. data/spec/fake_app/db/migrations/create_servers.rb +0 -13
  47. data/spec/fake_app/db/migrations/create_subnets.rb +0 -12
  48. data/spec/validates_host/remarkable/domain_name_matcher_spec.rb +0 -20
  49. data/spec/validates_host/remarkable/host_name_matcher_spec.rb +0 -20
  50. data/spec/validates_host/remarkable/ip_matcher_spec.rb +0 -20
  51. data/spec/validates_host/remarkable/subnet_matcher_spec.rb +0 -20
  52. data/spec/validates_host/shoulda-matchers/domain_name_matcher_spec.rb +0 -20
  53. data/spec/validates_host/shoulda-matchers/host_name_matcher_spec.rb +0 -20
  54. data/spec/validates_host/shoulda-matchers/ip_matcher_spec.rb +0 -20
  55. data/spec/validates_host/shoulda-matchers/subnet_matcher_spec.rb +0 -20
@@ -1,17 +1,15 @@
1
- require "shoulda-matchers"
1
+ # frozen_string_literal: true
2
+
3
+ require 'shoulda-matchers'
2
4
 
3
5
  module Shoulda
4
6
  module Matchers
5
7
  module ActiveModel
6
8
  def require_a_valid_host_name(attribute = :host_name)
7
- HostNameMatcher.new(attribute)
9
+ RequireAValidHostNameMatcher.new(attribute)
8
10
  end
9
11
 
10
- class HostNameMatcher < ValidationMatcher
11
- def initialize(attribute)
12
- @attribute = attribute
13
- end
14
-
12
+ class RequireAValidHostNameMatcher < ValidationMatcher
15
13
  def description
16
14
  "require #{@attribute} to be a valid host name"
17
15
  end
@@ -19,19 +17,19 @@ module Shoulda
19
17
  def matches?(subject)
20
18
  super(subject)
21
19
 
22
- disallows_invalid_value and allows_valid_value
20
+ disallows_invalid_value && allows_valid_value
23
21
  end
24
22
 
25
23
  private
26
24
 
27
25
  def disallows_invalid_value
28
- disallows_value_of("1bd")
26
+ disallows_value_of('1bd')
29
27
  end
30
28
 
31
29
  def allows_valid_value
32
- allows_value_of("bd01")
30
+ allows_value_of('bd01')
33
31
  end
34
32
  end
35
33
  end
36
34
  end
37
- end
35
+ end
@@ -1,17 +1,15 @@
1
- require "shoulda-matchers"
1
+ # frozen_string_literal: true
2
+
3
+ require 'shoulda-matchers'
2
4
 
3
5
  module Shoulda
4
6
  module Matchers
5
7
  module ActiveModel
6
8
  def require_a_valid_ip(attribute = :ip)
7
- IpMatcher.new(attribute)
9
+ RequireAValidIpMatcher.new(attribute)
8
10
  end
9
11
 
10
- class IpMatcher < ValidationMatcher
11
- def initialize(attribute)
12
- @attribute = attribute
13
- end
14
-
12
+ class RequireAValidIpMatcher < ValidationMatcher
15
13
  def description
16
14
  "require #{@attribute} to be a valid ip"
17
15
  end
@@ -19,19 +17,19 @@ module Shoulda
19
17
  def matches?(subject)
20
18
  super(subject)
21
19
 
22
- disallows_invalid_value and allows_valid_value
20
+ disallows_invalid_value && allows_valid_value
23
21
  end
24
22
 
25
23
  private
26
24
 
27
25
  def disallows_invalid_value
28
- disallows_value_of("10.0.0")
26
+ disallows_value_of('10.0.0')
29
27
  end
30
28
 
31
29
  def allows_valid_value
32
- allows_value_of("10.10.10.1")
30
+ allows_value_of('10.10.10.1')
33
31
  end
34
32
  end
35
33
  end
36
34
  end
37
- end
35
+ end
@@ -1,17 +1,15 @@
1
- require "shoulda-matchers"
1
+ # frozen_string_literal: true
2
+
3
+ require 'shoulda-matchers'
2
4
 
3
5
  module Shoulda
4
6
  module Matchers
5
7
  module ActiveModel
6
8
  def require_a_valid_subnet(attribute = :value)
7
- SubnetMatcher.new(attribute)
9
+ RequireAValidSubnetMatcher.new(attribute)
8
10
  end
9
11
 
10
- class SubnetMatcher < ValidationMatcher
11
- def initialize(attribute)
12
- @attribute = attribute
13
- end
14
-
12
+ class RequireAValidSubnetMatcher < ValidationMatcher
15
13
  def description
16
14
  "require #{@attribute} to be a valid subnet"
17
15
  end
@@ -19,19 +17,19 @@ module Shoulda
19
17
  def matches?(subject)
20
18
  super(subject)
21
19
 
22
- disallows_invalid_value and allows_valid_value
20
+ disallows_invalid_value && allows_valid_value
23
21
  end
24
22
 
25
23
  private
26
24
 
27
25
  def disallows_invalid_value
28
- disallows_value_of("10.0.0")
26
+ disallows_value_of('10.0.0')
29
27
  end
30
28
 
31
29
  def allows_valid_value
32
- allows_value_of("10.10.10.1/28")
30
+ allows_value_of('10.10.10.1/28')
33
31
  end
34
32
  end
35
33
  end
36
34
  end
37
- end
35
+ end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ValidatesHost
4
+ class Subnet
5
+ def initialize(subnet)
6
+ @subnet = subnet
7
+ end
8
+
9
+ def valid?
10
+ return true if @subnet.blank?
11
+
12
+ @subnet =~ %r{^([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}/(2[4-9]|30)$}
13
+ end
14
+ end
15
+ end
@@ -1,23 +1,16 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class SubnetValidator < ActiveModel::EachValidator
2
4
  def validate_each(record, attribute, value)
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::Subnet.new(value).valid?
5
- end
6
- end
5
+ return if ValidatesHost::Subnet.new(value).valid?
7
6
 
8
- module ValidatesHost
9
- class Subnet
10
- def initialize(subnet)
11
- @subnet = subnet
12
- end
7
+ ruby_prior_version_three =
8
+ Gem::Version.new(RUBY_VERSION) < Gem::Version.new('3.0.0')
13
9
 
14
- def valid?
15
- return true if @subnet.blank?
16
- @subnet =~ /^([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}\/(2[4-9]|30)$/
17
- end
18
-
19
- def subnet
20
- @subnet
10
+ if ruby_prior_version_three
11
+ record.errors.add(attribute, :invalid, options)
12
+ else
13
+ record.errors.add(attribute, :invalid, **options)
21
14
  end
22
15
  end
23
- end
16
+ end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ValidatesHost
2
- VERSION = "0.3.0"
4
+ VERSION = '1.3.0'.freeze
3
5
  end
@@ -1,5 +1,19 @@
1
- class Server < ActiveRecord::Base
2
- validates :domain_name, :domain_name => true
3
- validates :host_name, :host_name => true
4
- validates :ip, :ip => true
5
- end
1
+ # frozen_string_literal: true
2
+
3
+ class Server
4
+ include ActiveModel::Validations
5
+ include ActiveModel::Conversion
6
+ extend ActiveModel::Naming
7
+
8
+ attr_accessor :domain_name, :host_name, :ip
9
+
10
+ validates :domain_name, domain_name: true
11
+ validates :host_name, host_name: true
12
+ validates :ip, ip: true
13
+
14
+ def initialize(attributes = {})
15
+ attributes.each do |name, value|
16
+ send("#{name}=", value)
17
+ end
18
+ end
19
+ end
@@ -1,3 +1,11 @@
1
- class Subnet < ActiveRecord::Base
2
- validates :value, :subnet => true
3
- end
1
+ # frozen_string_literal: true
2
+
3
+ class Subnet
4
+ include ActiveModel::Validations
5
+ include ActiveModel::Conversion
6
+ extend ActiveModel::Naming
7
+
8
+ attr_accessor :id, :value
9
+
10
+ validates :value, subnet: true
11
+ end
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+
5
+ describe Shoulda::Matchers::ActiveModel::RequireAValidDomainNameMatcher do
6
+ let(:server) { Server.new }
7
+
8
+ it 'accepts on domain_name' do
9
+ expect(server).to require_a_valid_domain_name(:domain_name)
10
+ end
11
+
12
+ it 'accepts without a specified attribute' do
13
+ expect(server).to require_a_valid_domain_name
14
+ end
15
+
16
+ it 'rejects on host_name' do
17
+ expect(server).not_to require_a_valid_domain_name(:host_name)
18
+ end
19
+
20
+ it 'has a description message' do
21
+ expect(require_a_valid_domain_name.description).to eq('require domain_name to be a valid domain name')
22
+ end
23
+ end
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+
5
+ describe Shoulda::Matchers::ActiveModel::RequireAValidHostNameMatcher do
6
+ let(:server) { Server.new }
7
+
8
+ it 'accepts on host_name' do
9
+ expect(server).to require_a_valid_host_name(:host_name)
10
+ end
11
+
12
+ it 'accepts without a specified attribute' do
13
+ expect(server).to require_a_valid_host_name
14
+ end
15
+
16
+ it 'rejects on domain_name' do
17
+ expect(server).not_to require_a_valid_host_name(:domain_name)
18
+ end
19
+
20
+ it 'has a description message' do
21
+ expect(require_a_valid_host_name.description).to eq('require host_name to be a valid host name')
22
+ end
23
+ end
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+
5
+ describe Shoulda::Matchers::ActiveModel::RequireAValidIpMatcher do
6
+ let(:server) { Server.new }
7
+
8
+ it 'accepts on ip' do
9
+ expect(server).to require_a_valid_ip(:ip)
10
+ end
11
+
12
+ it 'accepts without a specified attribute' do
13
+ expect(server).to require_a_valid_ip
14
+ end
15
+
16
+ it 'rejects on domain_name' do
17
+ expect(server).not_to require_a_valid_ip(:domain_name)
18
+ end
19
+
20
+ it 'has a description message' do
21
+ expect(require_a_valid_ip.description).to eq('require ip to be a valid ip')
22
+ end
23
+ end
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+
5
+ describe Shoulda::Matchers::ActiveModel::RequireAValidSubnetMatcher do
6
+ let(:subnet) { Subnet.new }
7
+
8
+ it 'accepts on value' do
9
+ expect(subnet).to require_a_valid_subnet(:value)
10
+ end
11
+
12
+ it 'accepts without a specified attribute' do
13
+ expect(subnet).to require_a_valid_subnet
14
+ end
15
+
16
+ it 'rejects on id' do
17
+ expect(subnet).not_to require_a_valid_subnet(:id)
18
+ end
19
+
20
+ it 'has a description message' do
21
+ expect(require_a_valid_subnet.description).to eq('require value to be a valid subnet')
22
+ end
23
+ end
data/spec/spec_helper.rb CHANGED
@@ -1,11 +1,14 @@
1
- require "rubygems"
2
- require "rspec"
3
- require "active_record"
1
+ # frozen_string_literal: true
4
2
 
5
- Dir.glob(File.dirname(__FILE__) + "/../lib/**/*.rb").each { |file| require file }
6
- Dir.glob(File.dirname(__FILE__) + "/fake_app/**/*.rb").each { |file| require file }
3
+ require 'rspec'
4
+ require 'active_model'
5
+ require 'coveralls'
6
+ require 'shoulda-matchers'
7
7
 
8
- ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => ":memory:")
8
+ Coveralls.wear!
9
9
 
10
- CreateServers.migrate(:up)
11
- CreateSubnets.migrate(:up)
10
+ require 'validates_host'
11
+ require 'fake_app/server'
12
+ require 'fake_app/subnet'
13
+
14
+ include Shoulda::Matchers::ActiveModel
@@ -1,40 +1,41 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'spec_helper'
2
4
 
3
5
  describe DomainNameValidator do
4
- context "when domain_name is invalid" do
5
- before :each do
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")
6
+ let(:server) { Server.new }
7
+
8
+ context 'when domain_name is invalid' do
9
+ before do
10
+ server.domain_name = 'http://'
11
+ server.valid?
9
12
  end
10
13
 
11
- it "should set object as invalid" do
12
- @server.valid?.should be_false
14
+ it 'sets object as invalid' do
15
+ expect(server).not_to be_valid
13
16
  end
14
17
 
15
- it "should set an error" do
16
- @server.valid?
17
- @server.errors[:domain_name].should == ['is invalid']
18
+ it 'sets an error' do
19
+ expect(server.errors[:domain_name]).to eq(['is invalid'])
18
20
  end
19
21
  end
20
22
 
21
- context "when domain_name is valid" do
22
- before :each do
23
- @server = Server.new(:domain_name => "example.com")
23
+ context 'when domain_name is valid' do
24
+ before do
25
+ server.domain_name = 'example.com'
26
+ server.valid?
24
27
  end
25
28
 
26
- it "should set object as valid" do
27
- @server.valid?.should be_true
29
+ it 'sets object as valid' do
30
+ expect(server).to be_valid
28
31
  end
29
32
 
30
- it "should not set an error on attribute" do
31
- @server.valid?
32
- @server.errors[:domain_name].should be_blank
33
+ it 'does not set an error on attribute' do
34
+ expect(server.errors[:domain_name]).to be_blank
33
35
  end
34
36
  end
35
37
 
36
- it "should be valid with a nil value" do
37
- @server = Server.new(:domain_name => nil)
38
- @server.valid?.should be_true
38
+ it 'is valid with a nil value' do
39
+ expect(server).to be_valid
39
40
  end
40
- end
41
+ end
@@ -1,40 +1,41 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'spec_helper'
2
4
 
3
5
  describe HostNameValidator do
4
- context "when host_name is invalid" do
5
- before :each do
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")
6
+ let(:server) { Server.new }
7
+
8
+ context 'when host_name is invalid' do
9
+ before do
10
+ server.host_name = 'http://'
11
+ server.valid?
9
12
  end
10
13
 
11
- it "should set object as invalid" do
12
- @server.valid?.should be_false
14
+ it 'sets object as invalid' do
15
+ expect(server).not_to be_valid
13
16
  end
14
17
 
15
- it "should set an error" do
16
- @server.valid?
17
- @server.errors[:host_name].should == ['is invalid']
18
+ it 'sets an error' do
19
+ expect(server.errors[:host_name]).to eq(['is invalid'])
18
20
  end
19
21
  end
20
22
 
21
- context "when host_name is valid" do
22
- before :each do
23
- @server = Server.new(:host_name => "bd01")
23
+ context 'when host_name is valid' do
24
+ before do
25
+ server.host_name = 'bd01'
26
+ server.valid?
24
27
  end
25
28
 
26
- it "should set object as valid" do
27
- @server.valid?.should be_true
29
+ it 'sets object as valid' do
30
+ expect(server).to be_valid
28
31
  end
29
32
 
30
- it "should not set an error on attribute" do
31
- @server.valid?
32
- @server.errors[:host_name].should be_blank
33
+ it 'does not set an error on attribute' do
34
+ expect(server.errors[:host_name]).to be_blank
33
35
  end
34
36
  end
35
37
 
36
- it "should be valid with a nil value" do
37
- @server = Server.new(:host_name => nil)
38
- @server.valid?.should be_true
38
+ it 'is valid with a nil value' do
39
+ expect(server).to be_valid
39
40
  end
40
- end
41
+ end