Class: DataMapper::Property::Legacy::NumericIPAddr

Inherits:
Integer
  • Object
show all
Defined in:
lib/dm-core/property/legacy/numeric_ip_addr.rb

Instance Method Summary collapse

Instance Method Details

#dump(value) ⇒ Integer?

Dumps an IP Address to a numeric value.

Parameters:

  • value (IPAddr, nil)

    The IP Address.

Returns:

  • (Integer, nil)

    The numeric IP Address.



51
52
53
# File 'lib/dm-core/property/legacy/numeric_ip_addr.rb', line 51

def dump(value)
  value.to_i unless value.nil?
end

#load(value) ⇒ IPAddr?

Loads a numeric IP Address.

Parameters:

  • value (Integer, nil)

    The numeric IP Address.

Returns:

  • (IPAddr, nil)

    The IP Address.



19
20
21
# File 'lib/dm-core/property/legacy/numeric_ip_addr.rb', line 19

def load(value)
  load_integer(value) unless value.nil?
end

#load_integer(value) ⇒ IPAddr (protected)

Loads an IPv4 or IPv6 address from an integer.

Parameters:

  • value (Integer)

    The numeric IP Address.

Returns:

  • (IPAddr)

    The IPv4 or IPv6 address.



66
67
68
69
70
71
72
# 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

#typecast(value) ⇒ IPAddr?

Typecasts an IP Address.

Parameters:

  • value (IPAddr, String, Integer, nil)

    The IP Address.

Returns:

  • (IPAddr, nil)

    The typecasted IP Address.



32
33
34
35
36
37
38
39
40
# 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