lib/better_ipaddr/methods.rb in better_ipaddr-0.2.3 vs lib/better_ipaddr/methods.rb in better_ipaddr-0.3.0

- old
+ new

@@ -1,53 +1,8 @@ require "ipaddr" -require "socket" +require "better_ipaddr/constants" module BetterIpaddr - module Constants - # Integer codes representing supported address clases. - # Reuse values from Socket namespace where possible. - module Family - IPV4 = Socket::AF_INET - IPV6 = Socket::AF_INET6 - EUI48 = 48 - EUI64 = 64 - end - - # Map well known address family names to constants. - SYMBOL_TO_FAMILY = { - ipv4: Family::IPV4, - ipv6: Family::IPV6, - eui48: Family::EUI48, - eui64: Family::EUI64, - mac: Family::EUI48 - } - - # Map each address family to the size of its address space, in bits. - FAMILY_TO_BIT_LENGTH = { - Family::IPV4 => 32, - Family::IPV6 => 128, - Family::EUI48 => 48, - Family::EUI64 => 64 - } - - # Map all possible prefix lengths to the corresponding netmasks. - PREFIX_LENGTH_TO_NETMASK = {} - FAMILY_TO_BIT_LENGTH.each_pair do |family, size| - netmasks = [] - (0..size).each do |prefix_length| - netmasks[prefix_length] = 2**size - 2**(size - prefix_length) - end - PREFIX_LENGTH_TO_NETMASK[family] = netmasks - end - - # Map all possible netmasks to the corresponding prefix lengths. - NETMASK_TO_PREFIX_LENGTH = {} - PREFIX_LENGTH_TO_NETMASK.each_pair do |family, hash| - NETMASK_TO_PREFIX_LENGTH[family] = - Hash[hash.map.with_index { |e, i| [e, i] }] - end - end - module ClassMethods include Constants # @overload [](address, family) # @param address [Integer] the integer representation of the address