Class: Y2Network::IPAddress
- Inherits:
-
Object
- Object
- Y2Network::IPAddress
- Extended by:
- Forwardable
- Includes:
- Yast2::Equatable
- Defined in:
- src/lib/y2network/ip_address.rb
Overview
This class represents an IP address
The IPAddr from the Ruby standard library drops the host bits according to the netmask. The problem is that YaST uses a CIDR-like string, including the host bits, to set IPADDR in ifcfg-* files (see man 5 ifcfg for further details).
However, what we need is to be able to keep the host part
Instance Attribute Summary collapse
-
#address ⇒ IPAddr
IP address.
-
#prefix ⇒ Integer
Prefix.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(address, prefix = nil) ⇒ IPAddress
constructor
Constructor.
-
#netmask=(netmask) ⇒ Object
Sets the prefix from a netmask.
-
#prefix? ⇒ Boolean
Determines whether a prefix is defined.
-
#to_s ⇒ Object
Returns a string representation of the address.
Constructor Details
#initialize(address, prefix = nil) ⇒ IPAddress
Constructor
73 74 75 76 |
# File 'src/lib/y2network/ip_address.rb', line 73 def initialize(address, prefix = nil) @address = IPAddr.new(address) @prefix = prefix end |
Instance Attribute Details
#address ⇒ IPAddr
Returns IP address.
52 53 54 |
# File 'src/lib/y2network/ip_address.rb', line 52 def address @address end |
#prefix ⇒ Integer
Returns Prefix.
54 55 56 |
# File 'src/lib/y2network/ip_address.rb', line 54 def prefix @prefix end |
Class Method Details
.from_string(str) ⇒ Object
61 62 63 64 65 |
# File 'src/lib/y2network/ip_address.rb', line 61 def from_string(str) address, prefix = str.split("/") prefix = prefix.to_i if prefix new(address, prefix) end |
Instance Method Details
#netmask=(netmask) ⇒ Object
Sets the prefix from a netmask
86 87 88 |
# File 'src/lib/y2network/ip_address.rb', line 86 def netmask=(netmask) self.prefix = IPAddr.new("#{netmask}/#{netmask}").prefix end |
#prefix? ⇒ Boolean
Determines whether a prefix is defined
100 101 102 |
# File 'src/lib/y2network/ip_address.rb', line 100 def prefix? !!@prefix end |
#to_s ⇒ Object
Returns a string representation of the address
79 80 81 |
# File 'src/lib/y2network/ip_address.rb', line 79 def to_s prefix? ? "#{@address}/#{@prefix}" : @address.to_s end |