validates_host 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (26) hide show
  1. data/.travis.yml +4 -0
  2. data/README.md +19 -3
  3. data/lib/validates_host.rb +2 -0
  4. data/lib/validates_host/remarkable.rb +2 -1
  5. data/lib/validates_host/remarkable/ip_matcher.rb +2 -2
  6. data/lib/validates_host/remarkable/subnet_matcher.rb +29 -0
  7. data/lib/validates_host/shoulda-matchers.rb +2 -1
  8. data/lib/validates_host/shoulda-matchers/subnet_matcher.rb +37 -0
  9. data/lib/validates_host/subnet_validator.rb +23 -0
  10. data/lib/validates_host/version.rb +1 -1
  11. data/spec/fake_app/db/migrations/create_subnets.rb +12 -0
  12. data/spec/fake_app/subnet.rb +3 -0
  13. data/spec/spec_helper.rb +2 -1
  14. data/spec/{validates_host.rb → validates_host}/domain_name_validator_spec.rb +0 -0
  15. data/spec/{validates_host.rb → validates_host}/host_name_validator_spec.rb +0 -0
  16. data/spec/{validates_host.rb → validates_host}/ip_validator_spec.rb +0 -0
  17. data/spec/{validates_host.rb → validates_host}/remarkable/domain_name_matcher_spec.rb +0 -0
  18. data/spec/{validates_host.rb → validates_host}/remarkable/host_name_matcher_spec.rb +0 -0
  19. data/spec/{validates_host.rb → validates_host}/remarkable/ip_matcher_spec.rb +0 -0
  20. data/spec/validates_host/remarkable/subnet_matcher_spec.rb +20 -0
  21. data/spec/{validates_host.rb → validates_host}/shoulda-matchers/domain_name_matcher_spec.rb +0 -0
  22. data/spec/{validates_host.rb → validates_host}/shoulda-matchers/host_name_matcher_spec.rb +0 -0
  23. data/spec/{validates_host.rb → validates_host}/shoulda-matchers/ip_matcher_spec.rb +0 -0
  24. data/spec/validates_host/shoulda-matchers/subnet_matcher_spec.rb +20 -0
  25. data/spec/validates_host/subnet_validator_spec.rb +40 -0
  26. metadata +35 -21
@@ -0,0 +1,4 @@
1
+ rvm:
2
+ - 1.8.7
3
+ - 1.9.2
4
+ - 1.9.3
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
- # ValidatesHost
1
+ # ValidatesHost [![Build Status](https://secure.travis-ci.org/plribeiro3000/validates_host.png?branch=master)](http://travis-ci.org/plribeiro3000/validates_host)
2
2
 
3
- TODO: Write a gem description
3
+ Rails gem to validate Host Related fields.
4
4
 
5
5
  ## Installation
6
6
 
@@ -18,7 +18,23 @@ Or install it yourself as:
18
18
 
19
19
  ## Usage
20
20
 
21
- TODO: Write usage instructions here
21
+ Lets say you have a model that you want to have valid host fields. Just add this to your model:
22
+
23
+ ```ruby
24
+ class Server < ActiveRecord::Base
25
+ validates :domain_name, :domain_name => true
26
+ validates :host_name, :host_name => true
27
+ validates :ip, :ip => true
28
+ end
29
+
30
+ class Subnet < ActiveRecord::Base
31
+ validates :value, :subnet => true
32
+ end
33
+ ```
34
+
35
+ ## Test
36
+
37
+ This gem has builtin matchers for shoulda-matchers and remarkable.
22
38
 
23
39
  ## Contributing
24
40
 
@@ -4,6 +4,8 @@ require "validates_host/version"
4
4
  require 'validates_host/host_name_validator'
5
5
  require 'validates_host/domain_name_validator'
6
6
  require 'validates_host/ip_validator'
7
+ require 'validates_host/subnet_validator'
8
+
7
9
 
8
10
  #Rspec Matchers
9
11
  require "validates_host/shoulda-matchers" if defined?(::Shoulda)
@@ -1,3 +1,4 @@
1
1
  require 'validates_host/remarkable/domain_name_matcher'
2
2
  require 'validates_host/remarkable/host_name_matcher'
3
- require 'validates_host/remarkable/ip_matcher'
3
+ require 'validates_host/remarkable/ip_matcher'
4
+ require 'validates_host/remarkable/subnet_matcher'
@@ -21,8 +21,8 @@ module Remarkable
21
21
  end
22
22
  end
23
23
 
24
- def require_a_valid_domain_name(*args, &block)
25
- HostNameMatcher.new(*args, &block).spec(self)
24
+ def require_a_valid_ip(*args, &block)
25
+ IpMatcher.new(*args, &block).spec(self)
26
26
  end
27
27
  end
28
28
  end
@@ -0,0 +1,29 @@
1
+ require 'remarkable/active_model'
2
+
3
+ module Remarkable
4
+ module ActiveModel
5
+ module Matchers
6
+ class SubnetMatcher < Remarkable::ActiveModel::Base
7
+ arguments :value
8
+
9
+ collection_assertions :accept_valid_value?, :reject_valid_value?
10
+
11
+ protected
12
+
13
+ def accept_valid_value?
14
+ @subject.value = '10.10.10.1/28'
15
+ @subject.valid?.errors[:value].should == []
16
+ end
17
+
18
+ def reject_valid_value?
19
+ @subject.value = '01'
20
+ @subject.valid?.errors[:value].should == ['is invalid']
21
+ end
22
+ end
23
+
24
+ def require_a_valid_domain_name(*args, &block)
25
+ SubnetMatcher.new(*args, &block).spec(self)
26
+ end
27
+ end
28
+ end
29
+ end
@@ -1,3 +1,4 @@
1
1
  require 'validates_host/shoulda-matchers/domain_name_matcher'
2
2
  require 'validates_host/shoulda-matchers/host_name_matcher'
3
- require 'validates_host/shoulda-matchers/ip_matcher'
3
+ require 'validates_host/shoulda-matchers/ip_matcher'
4
+ require 'validates_host/shoulda-matchers/subnet_matcher'
@@ -0,0 +1,37 @@
1
+ require "shoulda-matchers"
2
+
3
+ module Shoulda
4
+ module Matchers
5
+ module ActiveModel
6
+ def require_a_valid_subnet(attribute = :value)
7
+ SubnetMatcher.new(attribute)
8
+ end
9
+
10
+ class SubnetMatcher < ValidationMatcher
11
+ def initialize(attribute)
12
+ @attribute = attribute
13
+ end
14
+
15
+ def description
16
+ "require #{@attribute} to be a valid subnet"
17
+ end
18
+
19
+ def matches?(subject)
20
+ super(subject)
21
+
22
+ disallows_invalid_value and allows_valid_value
23
+ end
24
+
25
+ private
26
+
27
+ def disallows_invalid_value
28
+ disallows_value_of("10.0.0")
29
+ end
30
+
31
+ def allows_valid_value
32
+ allows_value_of("10.10.10.1/28")
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,23 @@
1
+ class SubnetValidator < ActiveModel::EachValidator
2
+ 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
7
+
8
+ module ValidatesHost
9
+ class Subnet
10
+ def initialize(subnet)
11
+ @subnet = subnet
12
+ end
13
+
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
21
+ end
22
+ end
23
+ end
@@ -1,3 +1,3 @@
1
1
  module ValidatesHost
2
- VERSION = "0.2.0"
2
+ VERSION = "0.3.0"
3
3
  end
@@ -0,0 +1,12 @@
1
+ class CreateSubnets < ActiveRecord::Migration
2
+ def self.up
3
+ create_table :subnets do |s|
4
+ s.integer :id
5
+ s.string :value
6
+ end
7
+ end
8
+
9
+ def self.down
10
+ drop_table :subnets
11
+ end
12
+ end
@@ -0,0 +1,3 @@
1
+ class Subnet < ActiveRecord::Base
2
+ validates :value, :subnet => true
3
+ end
@@ -7,4 +7,5 @@ Dir.glob(File.dirname(__FILE__) + "/fake_app/**/*.rb").each { |file| require fil
7
7
 
8
8
  ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => ":memory:")
9
9
 
10
- CreateServers.migrate(:up)
10
+ CreateServers.migrate(:up)
11
+ CreateSubnets.migrate(:up)
@@ -0,0 +1,20 @@
1
+ require 'spec_helper'
2
+ require 'remarkable/active_model'
3
+
4
+ describe Remarkable::ActiveModel::Matchers::SubnetMatcher do
5
+ before :each do
6
+ @subnet = Subnet.new
7
+ end
8
+
9
+ it "should accept on value" do
10
+ @subnet.should require_a_valid_subnet(:value)
11
+ end
12
+
13
+ it "should accept without a specified attribute" do
14
+ @subnet.should require_a_valid_subnet
15
+ end
16
+
17
+ it "should reject on id" do
18
+ @subnet.should_not require_a_valid_subnet(:id)
19
+ end
20
+ end
@@ -0,0 +1,20 @@
1
+ require 'spec_helper'
2
+ require 'shoulda-matchers'
3
+
4
+ describe Shoulda::Matchers::ActiveModel::SubnetMatcher do
5
+ before :each do
6
+ @subnet = Subnet.new
7
+ end
8
+
9
+ it "should accept on value" do
10
+ @subnet.should require_a_valid_subnet(:value)
11
+ end
12
+
13
+ it "should accept without a specified attribute" do
14
+ @subnet.should require_a_valid_subnet
15
+ end
16
+
17
+ it "should reject on id" do
18
+ @subnet.should_not require_a_valid_subnet(:id)
19
+ end
20
+ end
@@ -0,0 +1,40 @@
1
+ require 'spec_helper'
2
+
3
+ describe SubnetValidator do
4
+ context "when subnet is invalid" do
5
+ before :each do
6
+ @subnet = Subnet.new(:value => "127.0.0")
7
+ I18n.stub(:t).with(:"activerecord.errors.models.subnet.attributes.value.invalid",
8
+ :default => :"activerecord.errors.messages.invalid").and_return("is invalid")
9
+ end
10
+
11
+ it "should set object as invalid" do
12
+ @subnet.valid?.should be_false
13
+ end
14
+
15
+ it "should set an error" do
16
+ @subnet.valid?
17
+ @subnet.errors[:value].should == ['is invalid']
18
+ end
19
+ end
20
+
21
+ context "when subnet is valid" do
22
+ before :each do
23
+ @subnet = Subnet.new(:value => "10.10.10.1/28")
24
+ end
25
+
26
+ it "should set object as valid" do
27
+ @subnet.valid?.should be_true
28
+ end
29
+
30
+ it "should not set an error on attribute" do
31
+ @subnet.valid?
32
+ @subnet.errors[:value].should be_blank
33
+ end
34
+ end
35
+
36
+ it "should be valid with a nil value" do
37
+ @subnet = Subnet.new(:value => nil)
38
+ @subnet.valid?.should be_true
39
+ end
40
+ end
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.2.0
4
+ version: 0.3.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -116,6 +116,7 @@ files:
116
116
  - .gitignore
117
117
  - .rspec
118
118
  - .rvmrc
119
+ - .travis.yml
119
120
  - Gemfile
120
121
  - LICENSE
121
122
  - README.md
@@ -128,23 +129,31 @@ files:
128
129
  - lib/validates_host/remarkable/domain_name_matcher.rb
129
130
  - lib/validates_host/remarkable/host_name_matcher.rb
130
131
  - lib/validates_host/remarkable/ip_matcher.rb
132
+ - lib/validates_host/remarkable/subnet_matcher.rb
131
133
  - lib/validates_host/shoulda-matchers.rb
132
134
  - lib/validates_host/shoulda-matchers/domain_name_matcher.rb
133
135
  - lib/validates_host/shoulda-matchers/host_name_matcher.rb
134
136
  - lib/validates_host/shoulda-matchers/ip_matcher.rb
137
+ - lib/validates_host/shoulda-matchers/subnet_matcher.rb
138
+ - lib/validates_host/subnet_validator.rb
135
139
  - lib/validates_host/version.rb
136
140
  - spec/fake_app/db/migrations/create_servers.rb
141
+ - spec/fake_app/db/migrations/create_subnets.rb
137
142
  - spec/fake_app/server.rb
143
+ - spec/fake_app/subnet.rb
138
144
  - spec/spec_helper.rb
139
- - spec/validates_host.rb/domain_name_validator_spec.rb
140
- - spec/validates_host.rb/host_name_validator_spec.rb
141
- - spec/validates_host.rb/ip_validator_spec.rb
142
- - spec/validates_host.rb/remarkable/domain_name_matcher_spec.rb
143
- - spec/validates_host.rb/remarkable/host_name_matcher_spec.rb
144
- - spec/validates_host.rb/remarkable/ip_matcher_spec.rb
145
- - spec/validates_host.rb/shoulda-matchers/domain_name_matcher_spec.rb
146
- - spec/validates_host.rb/shoulda-matchers/host_name_matcher_spec.rb
147
- - spec/validates_host.rb/shoulda-matchers/ip_matcher_spec.rb
145
+ - spec/validates_host/domain_name_validator_spec.rb
146
+ - spec/validates_host/host_name_validator_spec.rb
147
+ - spec/validates_host/ip_validator_spec.rb
148
+ - spec/validates_host/remarkable/domain_name_matcher_spec.rb
149
+ - spec/validates_host/remarkable/host_name_matcher_spec.rb
150
+ - spec/validates_host/remarkable/ip_matcher_spec.rb
151
+ - spec/validates_host/remarkable/subnet_matcher_spec.rb
152
+ - spec/validates_host/shoulda-matchers/domain_name_matcher_spec.rb
153
+ - spec/validates_host/shoulda-matchers/host_name_matcher_spec.rb
154
+ - spec/validates_host/shoulda-matchers/ip_matcher_spec.rb
155
+ - spec/validates_host/shoulda-matchers/subnet_matcher_spec.rb
156
+ - spec/validates_host/subnet_validator_spec.rb
148
157
  - validates_host.gemspec
149
158
  homepage: ''
150
159
  licenses: []
@@ -160,7 +169,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
160
169
  version: '0'
161
170
  segments:
162
171
  - 0
163
- hash: -391399657668278307
172
+ hash: 873921845933518174
164
173
  required_rubygems_version: !ruby/object:Gem::Requirement
165
174
  none: false
166
175
  requirements:
@@ -169,7 +178,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
169
178
  version: '0'
170
179
  segments:
171
180
  - 0
172
- hash: -391399657668278307
181
+ hash: 873921845933518174
173
182
  requirements: []
174
183
  rubyforge_project:
175
184
  rubygems_version: 1.8.24
@@ -178,14 +187,19 @@ specification_version: 3
178
187
  summary: Host Validation Gem
179
188
  test_files:
180
189
  - spec/fake_app/db/migrations/create_servers.rb
190
+ - spec/fake_app/db/migrations/create_subnets.rb
181
191
  - spec/fake_app/server.rb
192
+ - spec/fake_app/subnet.rb
182
193
  - spec/spec_helper.rb
183
- - spec/validates_host.rb/domain_name_validator_spec.rb
184
- - spec/validates_host.rb/host_name_validator_spec.rb
185
- - spec/validates_host.rb/ip_validator_spec.rb
186
- - spec/validates_host.rb/remarkable/domain_name_matcher_spec.rb
187
- - spec/validates_host.rb/remarkable/host_name_matcher_spec.rb
188
- - spec/validates_host.rb/remarkable/ip_matcher_spec.rb
189
- - spec/validates_host.rb/shoulda-matchers/domain_name_matcher_spec.rb
190
- - spec/validates_host.rb/shoulda-matchers/host_name_matcher_spec.rb
191
- - spec/validates_host.rb/shoulda-matchers/ip_matcher_spec.rb
194
+ - spec/validates_host/domain_name_validator_spec.rb
195
+ - spec/validates_host/host_name_validator_spec.rb
196
+ - spec/validates_host/ip_validator_spec.rb
197
+ - spec/validates_host/remarkable/domain_name_matcher_spec.rb
198
+ - spec/validates_host/remarkable/host_name_matcher_spec.rb
199
+ - spec/validates_host/remarkable/ip_matcher_spec.rb
200
+ - spec/validates_host/remarkable/subnet_matcher_spec.rb
201
+ - spec/validates_host/shoulda-matchers/domain_name_matcher_spec.rb
202
+ - spec/validates_host/shoulda-matchers/host_name_matcher_spec.rb
203
+ - spec/validates_host/shoulda-matchers/ip_matcher_spec.rb
204
+ - spec/validates_host/shoulda-matchers/subnet_matcher_spec.rb
205
+ - spec/validates_host/subnet_validator_spec.rb