subnet_format 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/subnet_format/subnet_format_validator.rb +30 -12
- data/lib/subnet_format/version.rb +1 -1
- data/spec/spec_helper.rb +21 -7
- data/spec/subnet_format_validator_spec.rb +36 -6
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 717231b95db546203e4c6da350190cbed764959f
|
4
|
+
data.tar.gz: 5ff928829ff380a7e9d591094fbafb11e63aa1a7
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 72921daf1cbdce8f8a42bac730fda047995f4ca61dfb4689465407318d35d5a0c50fa642f733181b277ddc4a87be9fdaee4461d2783e652db02faa64f43227fe
|
7
|
+
data.tar.gz: 642745eb107fd2d06e49cedfa0a3877e41a771c6a6287259b436464e55dc09ab1abdab840c07613452d504f7866004d8f9aca5b2cf4c5243ff90445759496276
|
@@ -3,21 +3,39 @@ require 'ipaddr'
|
|
3
3
|
class SubnetFormatValidator < ActiveModel::Validator
|
4
4
|
def validate(record)
|
5
5
|
@record = record
|
6
|
-
|
7
|
-
validate_dhcp_range
|
6
|
+
|
7
|
+
validate_dhcp_range
|
8
8
|
rescue ArgumentError
|
9
9
|
ip_argument_error
|
10
10
|
end
|
11
11
|
|
12
12
|
private
|
13
13
|
|
14
|
-
def
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
14
|
+
def network_address_prefix
|
15
|
+
@record.send(options[:network_address_prefix] || :network_address_prefix)
|
16
|
+
end
|
17
|
+
|
18
|
+
def subnet_mask
|
19
|
+
@record.send(options[:subnet_mask] || :subnet_mask)
|
20
|
+
end
|
21
|
+
|
22
|
+
def dhcp_range_start
|
23
|
+
@record.send(options[:dhcp_range_start] || :dhcp_range_start)
|
24
|
+
end
|
25
|
+
|
26
|
+
def dhcp_range_stop
|
27
|
+
@record.send(options[:dhcp_range_stop] || :dhcp_range_stop)
|
28
|
+
end
|
29
|
+
|
30
|
+
def ip
|
31
|
+
IPAddr.new "#{ network_address_prefix }/#{ subnet_mask }"
|
32
|
+
end
|
33
|
+
|
34
|
+
def validate_dhcp_range
|
35
|
+
ip_to_validate = ip
|
36
|
+
|
37
|
+
unless ip_to_validate.include?(dhcp_range_start) and ip_to_validate.include?(dhcp_range_stop)
|
38
|
+
dhcp_range_error(ip_to_validate)
|
21
39
|
end
|
22
40
|
end
|
23
41
|
|
@@ -25,8 +43,8 @@ class SubnetFormatValidator < ActiveModel::Validator
|
|
25
43
|
@record.errors[:base] << (options[:message] || "is an invalid ip")
|
26
44
|
end
|
27
45
|
|
28
|
-
def dhcp_range_error
|
29
|
-
@record.errors[:base] << (options[:message] ||
|
30
|
-
"has an invalid DHCP range, valid range: #{
|
46
|
+
def dhcp_range_error(invalid_ip)
|
47
|
+
@record.errors[:base] << (options[:message] ||
|
48
|
+
"has an invalid DHCP range, valid range: #{ invalid_ip.to_range.first } to #{ invalid_ip.to_range.last }")
|
31
49
|
end
|
32
50
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -4,19 +4,33 @@ require 'rspec'
|
|
4
4
|
require 'active_model'
|
5
5
|
require 'subnet_format'
|
6
6
|
|
7
|
-
class
|
7
|
+
class FakeModelBase
|
8
8
|
include ActiveModel::Validations
|
9
|
+
include ActiveModel::Serialization
|
9
10
|
|
10
|
-
attr_accessor :
|
11
|
+
attr_accessor :network_address_prefix, :gateway_ip, :dhcp_range_start,
|
11
12
|
:dhcp_range_stop, :subnet_mask
|
12
13
|
|
14
|
+
def initialize(attrs = {})
|
15
|
+
self.network_address_prefix = attrs[:network_address_prefix]
|
16
|
+
self.gateway_ip = attrs[:gateway_ip]
|
17
|
+
self.dhcp_range_start = attrs[:dhcp_range_start]
|
18
|
+
self.dhcp_range_stop = attrs[:dhcp_range_stop]
|
19
|
+
self.subnet_mask = attrs[:subnet_mask]
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
class FakeModel < FakeModelBase
|
13
24
|
validates_with SubnetFormatValidator
|
25
|
+
end
|
26
|
+
|
27
|
+
class FakeModelWithValidationOptions < FakeModelBase
|
28
|
+
attr_accessor :new_network_address_prefix_attr
|
14
29
|
|
15
30
|
def initialize(attrs = {})
|
16
|
-
|
17
|
-
self.
|
18
|
-
self.dhcp_range_start = attrs[:dhcp_range_start]
|
19
|
-
self.dhcp_range_stop = attrs[:dhcp_range_stop]
|
20
|
-
self.subnet_mask = attrs[:subnet_mask]
|
31
|
+
super
|
32
|
+
self.new_network_address_prefix_attr = attrs[:network_address_prefix]
|
21
33
|
end
|
34
|
+
|
35
|
+
validates_with SubnetFormatValidator, network_address_prefix: :new_network_address_prefix_attr
|
22
36
|
end
|
@@ -1,12 +1,42 @@
|
|
1
1
|
describe SubnetFormatValidator do
|
2
2
|
|
3
|
-
let(:
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
3
|
+
let(:network) {
|
4
|
+
{
|
5
|
+
subnet_mask: '255.255.255.0',
|
6
|
+
network_address_prefix: '192.168.0.1',
|
7
|
+
dhcp_range_stop: '192.168.0.30',
|
8
|
+
dhcp_range_start: '192.168.0.5'
|
9
|
+
}
|
10
|
+
}
|
11
|
+
|
12
|
+
let(:fake_model) { FakeModel.new(network) }
|
8
13
|
|
9
14
|
subject { fake_model }
|
10
15
|
|
11
16
|
it { should be_valid }
|
12
|
-
|
17
|
+
|
18
|
+
context 'with an invalid subnet mask' do
|
19
|
+
|
20
|
+
let(:invalid_subnet_mask_options) { network.merge(subnet_mask: '123') }
|
21
|
+
|
22
|
+
let(:fake_model) { FakeModel.new(invalid_subnet_mask_options) }
|
23
|
+
|
24
|
+
it 'should be invalid' do
|
25
|
+
expect(fake_model.valid?).to be_falsey
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
context 'with options passed' do
|
30
|
+
|
31
|
+
let(:fake_model) { FakeModelWithValidationOptions.new(network) }
|
32
|
+
|
33
|
+
it 'should use them rather than the class method' do
|
34
|
+
expect(FakeModelWithValidationOptions).to_not receive(:network_address_prefix)
|
35
|
+
fake_model.valid?
|
36
|
+
end
|
37
|
+
|
38
|
+
it 'should still be valid' do
|
39
|
+
expect(fake_model.valid?).to be_truthy
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: subnet_format
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- John Otander
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-09-
|
11
|
+
date: 2014-09-03 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|