Sha256: e755f71c91fb61f933c3f10f294adbc1aeaece202d5b92ae1feecf148564c6dd

Contents?: true

Size: 1.62 KB

Versions: 29

Compression:

Stored size: 1.62 KB

Contents

module IPAddress

  #
  # Mongoid field serialization
  #
  # IPAddress objects are converted to String
  #
  #   IPAddress.mongoize IPAddress.parse("172.16.10.1")
  #     #=> "172.16.10.1"
  #
  # Prefix will be removed from host adresses
  #
  #   IPAddress.mongoize "172.16.10.1/32"
  #     #=> "172.16.10.1"
  #
  # Prefix will be kept for network addresses
  #
  #   IPAddress.mongoize "172.16.10.1/24"
  #     #=> "172.16.10.1/24"
  #
  # IPv6 addresses will be stored uncompressed to ease DB search and sorting
  #
  #   IPAddress.mongoize "2001:db8::8:800:200c:417a"
  #     #=> "2001:0db8:0000:0000:0008:0800:200c:417a"
  #   IPAddress.mongoize "2001:db8::8:800:200c:417a/64"
  #     #=> "2001:0db8:0000:0000:0008:0800:200c:417a/64"
  #
  # Invalid addresses will be serialized as nil
  #
  #   IPAddress.mongoize "invalid"
  #     #=> nil
  #   IPAddress.mongoize ""
  #     #=> nil
  #   IPAddress.mongoize 1
  #     #=> nil
  #   IPAddress.mongoize nil
  #     #=> nil
  #
  def self.mongoize(ipaddress)
    ipaddress = self.parse(ipaddress) unless ipaddress.is_a?(IPAddress)
    if ipaddress.bits.length == ipaddress.prefix
      ipaddress.address
    elsif ipaddress.is_a?(IPAddress::IPv6)
      ipaddress.to_string_uncompressed
    else
      ipaddress.to_string
    end
  rescue ArgumentError
    nil
  end

  #
  # Mongoid field deserialization
  #
  def self.demongoize(string)
    parse(string)
  rescue ArgumentError
    nil
  end

  #
  # Delegates to IPAddress.mongoize
  #
  def self.evolve(ipaddress)
    mongoize(ipaddress)
  end

  #
  # Sends self object to IPAddress#mongoize
  #
  def mongoize
    IPAddress.mongoize(self)
  end

end

Version data entries

29 entries across 19 versions & 6 rubygems

Version Path
savant-echo-0.1.5 bin/packaging/vendor/ruby/2.2.0/gems/ipaddress-0.8.3/lib/ipaddress/mongoid.rb
savant-echo-0.1.5 bin/alexa-0.1.2-osx/lib/vendor/ruby/2.2.0/gems/ipaddress-0.8.3/lib/ipaddress/mongoid.rb
savant-echo-0.1.4 bin/packaging/vendor/ruby/2.2.0/gems/ipaddress-0.8.3/lib/ipaddress/mongoid.rb
savant-echo-0.1.4 bin/alexa-0.1.2-osx/lib/vendor/ruby/2.2.0/gems/ipaddress-0.8.3/lib/ipaddress/mongoid.rb
savant-echo-0.1.3 bin/packaging/vendor/ruby/2.2.0/gems/ipaddress-0.8.3/lib/ipaddress/mongoid.rb
savant-echo-0.1.3 bin/alexa-0.1.2-osx/lib/vendor/ruby/2.2.0/gems/ipaddress-0.8.3/lib/ipaddress/mongoid.rb
ipaddress_link_local-0.8.3 lib/ipaddress/mongoid.rb
ipaddress-0.8.3 lib/ipaddress/mongoid.rb
ipaddress-0.8.2 lib/ipaddress/mongoid.rb