Class: Ifconf::Ipv4Config
- Inherits:
-
Object
- Object
- Ifconf::Ipv4Config
- 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
-
#address ⇒ Object
readonly
Returns the value of attribute address.
-
#broadcast ⇒ Object
readonly
Returns the value of attribute broadcast.
Class Method Summary collapse
Instance Method Summary collapse
- #==(other) ⇒ Object (also: #eql?)
- #broadcast_address ⇒ Object
- #hash ⇒ Object
- #host_count ⇒ Object
- #host_range ⇒ Object
- #includes_ip?(ip) ⇒ Boolean
-
#initialize(address, broadcast, cidr) ⇒ Ipv4Config
constructor
A new instance of Ipv4Config.
- #network_address ⇒ Object
- #prefix_length ⇒ Object
- #present? ⇒ Boolean
- #to_cidr_string ⇒ Object
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
#address ⇒ Object (readonly)
Returns the value of attribute address.
7 8 9 |
# File 'lib/ifconf/ipv4_config.rb', line 7 def address @address end |
#broadcast ⇒ Object (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_address ⇒ Object
25 |
# File 'lib/ifconf/ipv4_config.rb', line 25 def broadcast_address = Network::IP.new(@cidr.last.to_s) |
#hash ⇒ Object
41 |
# File 'lib/ifconf/ipv4_config.rb', line 41 def hash = [@address, prefix_length, @broadcast].hash |
#host_count ⇒ Object
28 |
# File 'lib/ifconf/ipv4_config.rb', line 28 def host_count = @cidr.size - 2 |
#host_range ⇒ Object
26 |
# File 'lib/ifconf/ipv4_config.rb', line 26 def host_range = network_address..broadcast_address |
#includes_ip?(ip) ⇒ Boolean
27 |
# File 'lib/ifconf/ipv4_config.rb', line 27 def includes_ip?(ip) = @cidr.include?(ip.to_s) |
#network_address ⇒ Object
24 |
# File 'lib/ifconf/ipv4_config.rb', line 24 def network_address = Network::IP.new(@cidr.first.to_s) |
#prefix_length ⇒ Object
23 |
# File 'lib/ifconf/ipv4_config.rb', line 23 def prefix_length = @cidr.prefix |
#present? ⇒ Boolean
30 |
# File 'lib/ifconf/ipv4_config.rb', line 30 def present? = true |
#to_cidr_string ⇒ Object
29 |
# File 'lib/ifconf/ipv4_config.rb', line 29 def to_cidr_string = "#{@address}/#{prefix_length}" |