lib/better_ipaddr/classes.rb in better_ipaddr-0.3.1 vs lib/better_ipaddr/classes.rb in better_ipaddr-0.4.0
- old
+ new
@@ -1,7 +1,8 @@
require "better_ipaddr/constants"
require "better_ipaddr/methods"
+require "better_ipaddr/host_methods"
class IPAddr
class Base < IPAddr
include BetterIpaddr::Constants
include BetterIpaddr::InstanceMethods
@@ -51,10 +52,25 @@
when Regex::IPV6, 0..V6::MAX_INT
V6[address]
end
end
+ # Create an IPAddr host subclass from the given object, guessing the type of
+ # address given based on its type and content.
+ #
+ # Uses .from internally, so the same concerns apply, though the returned
+ # object is guaranteed to be of a Host class or nil.
+
+ def self.host_from(address)
+ ip = from(address)
+ if ip.ipv4?
+ V4::Host[ip]
+ elsif ip.ipv6?
+ V6::Host[ip]
+ end
+ end
+
# Create an IPAddr from an Integer.
#
# @param address [Integer]
# @param mask [Integer, String] a netmask or prefix length
# @param family [Integer, Nil]
@@ -191,10 +207,12 @@
const_set(:NETMASK_TO_PREFIX_LENGTH,
NETMASK_TO_PREFIX_LENGTH.fetch(self::FAMILY))
const_set(:PREFIX_LENGTH_TO_NETMASK,
PREFIX_LENGTH_TO_NETMASK.fetch(self::FAMILY))
const_set(:MAX_INT, 2**self::BIT_LENGTH - 1)
+ const_set(:HOST_NETMASK,
+ self::PREFIX_LENGTH_TO_NETMASK.fetch(self::BIT_LENGTH))
end
def address_family_bit_length
self.class::BIT_LENGTH
end
@@ -210,15 +228,23 @@
class V4 < Base
specialize_constants Family::IPV4
REGEX = Regex::IPV4
+
+ class Host < V4
+ include BetterIpaddr::HostMethods
+ end
end
class V6 < Base
specialize_constants Family::IPV6
REGEX = Regex::IPV6
+
+ class Host < V6
+ include BetterIpaddr::HostMethods
+ end
end
class MAC < Base
specialize_constants Family::EUI48
end