lib/better_ipaddr/methods.rb in better_ipaddr-0.5.0 vs lib/better_ipaddr/methods.rb in better_ipaddr-0.6.0
- old
+ new
@@ -1,49 +1,10 @@
require "ipaddr"
require "better_ipaddr/constants"
module BetterIpaddr
- module ClassMethods
- include Constants
-
- # @overload [](address, family)
- # @param address [Integer] the integer representation of the address
- # @param family [Symbol] a symbol named for the address's
- # address family, one of +:ipv4+, +:ipv6+, or +:mac+.
- # @return [IPAddr]
- # Wrapper for IPAddr.new that accepts a symbolic family name and
- # returns a specialized IPAddr subclass.
- #
- # @overload [](address, family)
- # @param address [Integer] the integer representation of the address
- # @param family [Integer] the magic number representing the address's
- # address family.
- # @return [IPAddr]
- # Wrapper for IPAddr.new that accepts a symbolic family name and
- # returns a specialized IPAddr subclass.
- #
- # @overload [](address)
- # @param address [String] the string representation of the address
- # @return [IPAddr]
- # Wrapper for IPAddr.new that accepts the string representation
- # of an address returns a specialized IPAddr subclass.
-
- def [](address, family = nil)
- instance = case family
- when Symbol
- self[address, SYMBOL_TO_FAMILY.fetch(family)]
- when IPAddr
- address
- when nil
- new(address)
- else
- new(address, family)
- end
- IPAddr::Base.specialize(instance)
- end
- end
-
+ # Methods included in IPAddr::Base and its descendants
module InstanceMethods
include Constants
def self.included(base)
return unless base <= IPAddr
@@ -83,11 +44,11 @@
# @param offset [Integer] the difference between the original
# address and the returned address
# @return [IPAddr]
def -(offset)
- self + (-offset)
+ self + -offset
end
# @overload <=>(other)
# Compare this address with another address of the same address
# family.
@@ -103,11 +64,11 @@
def <=>(other)
if other.is_a?(IPAddr)
family_difference = family <=> other.family
return family_difference unless family_difference == 0
elsif !other.is_a?(Integer)
- fail ArgumentError, "Can't compare #{self.class} with #{other.class}"
+ return nil
end
address_difference = to_i <=> other.to_i
if address_difference != 0 || !other.is_a?(IPAddr)
@@ -135,11 +96,11 @@
# @param offset [Integer] the index within the network of the
# desired address
# @return [IPAddr] the address at the given index
def [](offset)
- return self if offset.zero? && host?
+ return self if offset == 0 && host?
offset2 = offset >= 0 ? offset : size + offset
self.class[to_i + offset2, family: family]
end
# Returns the number of bits allowed by the address family.
@@ -254,9 +215,10 @@
def prefix_length
NETMASK_TO_PREFIX_LENGTH[family][mask_addr]
end
+ alias_method :prefix, :prefix_length unless 1.respond_to?(:positive?)
alias_method :prefixlen, :prefix_length
# Return a new address with the prefix length increased by the
# given amount. The old address will cover the new address.
# @param shift [Integer] the increase in the prefix length