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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 6c5e37eb8ba898c02c6a639eeb9f947de86aff72
4
- data.tar.gz: 8d8a808c81fb60a08abbdef96734752f5522fb70
3
+ metadata.gz: 717231b95db546203e4c6da350190cbed764959f
4
+ data.tar.gz: 5ff928829ff380a7e9d591094fbafb11e63aa1a7
5
5
  SHA512:
6
- metadata.gz: 7dbe40df185d17801413f4edf8e40814b30d122ae02eb4be96e32e70aff4dacdd5a74306d8db5dc8514c12d2888484fc16a69bfd6ba32be437f488f53d62121e
7
- data.tar.gz: 2dee15edbd2badc8695a8d4bd25e2b11d2c895880bf21843e233e60ab20b0b632016c7922116077b78ca7fa64d06e041cbd9bad22a80b16b60e54e8c852acbf5
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
- @ip = IPAddr.new "#{ record.network_address_ip }/#{ record.subnet_mask }"
7
- validate_dhcp_range dhcp_start: @record.dhcp_range_start, dhcp_end: @record.dhcp_range_stop
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 validate_dhcp_range(dhcp_values)
15
- dhcp_start = dhcp_values[:dhcp_start]
16
- dhcp_end = dhcp_values[:dhcp_end]
17
-
18
- return if dhcp_start.blank? and dhcp_end.blank?
19
- unless @ip.include?(dhcp_start) and @ip.include?(dhcp_end)
20
- dhcp_range_error
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: #{ @ip.to_range.first } to #{ @ip.to_range.last }")
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
@@ -1,3 +1,3 @@
1
1
  module SubnetFormat
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  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 FakeModel
7
+ class FakeModelBase
8
8
  include ActiveModel::Validations
9
+ include ActiveModel::Serialization
9
10
 
10
- attr_accessor :network_address_ip, :gateway_ip, :dhcp_range_start,
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
- self.network_address_ip = attrs[:network_address_ip]
17
- self.gateway_ip = attrs[:gateway_ip]
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(:fake_model) { FakeModel.new(
4
- subnet_mask: '255.255.255.0',
5
- network_address_ip: '192.168.0.1',
6
- dhcp_range_stop: '192.168.0.30',
7
- dhcp_range_start: '192.168.0.5') }
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
- end
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.1
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-02 00:00:00.000000000 Z
11
+ date: 2014-09-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler