class DataMapper::Property::Legacy::NumericIPAddr

Public Instance Methods

dump(value) click to toggle source

Dumps an IP Address to a numeric value.

@param [IPAddr, nil] value

The IP Address.

@return [Integer, nil]

The numeric IP Address.
# File lib/dm-core/property/legacy/numeric_ip_addr.rb, line 51
def dump(value)
  value.to_i unless value.nil?
end
load(value) click to toggle source

Loads a numeric IP Address.

@param [Integer, nil] value

The numeric IP Address.

@return [IPAddr, nil]

The IP Address.
# File lib/dm-core/property/legacy/numeric_ip_addr.rb, line 19
def load(value)
  load_integer(value) unless value.nil?
end
typecast(value) click to toggle source

Typecasts an IP Address.

@param [IPAddr, String, Integer, nil] value

The IP Address.

@return [IPAddr, nil]

The typecasted IP Address.
# File lib/dm-core/property/legacy/numeric_ip_addr.rb, line 32
def typecast(value)
  if value.kind_of?(::IPAddr)
    value
  elsif value.kind_of?(::String)
    ::IPAddr.new(value) unless value.empty?
  elsif value.kind_of?(::Integer)
    load_integer(value)
  end
end

Protected Instance Methods

load_integer(value) click to toggle source

Loads an IPv4 or IPv6 address from an integer.

@param [Integer] value

The numeric IP Address.

@return [IPAddr]

The IPv4 or IPv6 address.
# File lib/dm-core/property/legacy/numeric_ip_addr.rb, line 66
def load_integer(value)
  if value > 4294967295 # (2 ** 32) - 1
    ::IPAddr.new(value,Socket::AF_INET6)
  elsif value >= 0
    ::IPAddr.new(value,Socket::AF_INET)
  end
end