Class: Ifconf::Ipv4Config

Inherits:
Object
  • Object
show all
Defined in:
lib/ifconf/ipv4_config.rb

Overview

Holds an interface’s IPv4 address, prefix length, broadcast, and subnet math via CIDR.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(address, broadcast, cidr) ⇒ Ipv4Config

Returns a new instance of Ipv4Config.



16
17
18
19
20
21
# File 'lib/ifconf/ipv4_config.rb', line 16

def initialize(address, broadcast, cidr)
  @address = address
  @broadcast = broadcast
  @cidr = cidr
  freeze
end

Instance Attribute Details

#addressObject (readonly)

Returns the value of attribute address.



7
8
9
# File 'lib/ifconf/ipv4_config.rb', line 7

def address
  @address
end

#broadcastObject (readonly)

Returns the value of attribute broadcast.



7
8
9
# File 'lib/ifconf/ipv4_config.rb', line 7

def broadcast
  @broadcast
end

Class Method Details

.build(addr:, prefix:, bcast: nil) ⇒ Object



9
10
11
12
13
14
# File 'lib/ifconf/ipv4_config.rb', line 9

def self.build(addr:, prefix:, bcast: nil)
  address = Network::IP.new(addr)
  broadcast = bcast ? Network::IP.new(bcast) : nil
  cidr = Network::IPRange::CIDR.new("#{address}/#{prefix}")
  new(address, broadcast, cidr)
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?



32
33
34
35
36
37
# File 'lib/ifconf/ipv4_config.rb', line 32

def ==(other)
  other.is_a?(Ipv4Config) &&
    @address == other.address &&
    prefix_length == other.prefix_length &&
    @broadcast == other.broadcast
end

#broadcast_addressObject



25
# File 'lib/ifconf/ipv4_config.rb', line 25

def broadcast_address = Network::IP.new(@cidr.last.to_s)

#hashObject



41
# File 'lib/ifconf/ipv4_config.rb', line 41

def hash = [@address, prefix_length, @broadcast].hash

#host_countObject



28
# File 'lib/ifconf/ipv4_config.rb', line 28

def host_count        = @cidr.size - 2

#host_rangeObject



26
# File 'lib/ifconf/ipv4_config.rb', line 26

def host_range        = network_address..broadcast_address

#includes_ip?(ip) ⇒ Boolean

Returns:

  • (Boolean)


27
# File 'lib/ifconf/ipv4_config.rb', line 27

def includes_ip?(ip)  = @cidr.include?(ip.to_s)

#network_addressObject



24
# File 'lib/ifconf/ipv4_config.rb', line 24

def network_address   = Network::IP.new(@cidr.first.to_s)

#prefix_lengthObject



23
# File 'lib/ifconf/ipv4_config.rb', line 23

def prefix_length     = @cidr.prefix

#present?Boolean

Returns:

  • (Boolean)


30
# File 'lib/ifconf/ipv4_config.rb', line 30

def present?          = true

#to_cidr_stringObject



29
# File 'lib/ifconf/ipv4_config.rb', line 29

def to_cidr_string    = "#{@address}/#{prefix_length}"