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
ipaddress_2-0.14.0 lib/ipaddress_2/mongoid.rb
ipaddress_2-0.13.0 lib/ipaddress_2/mongoid.rb
ipaddress_2-0.12.1 lib/ipaddress_2/mongoid.rb
ipaddress_2-0.12.0 lib/ipaddress_2/mongoid.rb
ipaddress_2-0.11.1 lib/ipaddress_2/mongoid.rb
ipaddress_2-0.11.0 lib/ipaddress_2/mongoid.rb
vagrant-packet-0.1.1 vendor/bundle/ruby/2.3.0/gems/ipaddress-0.8.3/lib/ipaddress/mongoid.rb
vagrant-packet-0.1.1 vendor/bundle/ruby/2.5.0/gems/ipaddress-0.8.3/lib/ipaddress/mongoid.rb
vagrant-packet-0.1.1 vendor/bundle/ruby/2.4.0/gems/ipaddress-0.8.3/lib/ipaddress/mongoid.rb
echonet_lite-0.1.0 vendor/bundle/gems/ipaddress-0.8.3/lib/ipaddress/mongoid.rb
savant-echo-1.0.4 bin/packaging/vendor/ruby/2.2.0/gems/ipaddress-0.8.3/lib/ipaddress/mongoid.rb
savant-echo-1.0.4 bin/alexa-0.1.2-osx/lib/vendor/ruby/2.2.0/gems/ipaddress-0.8.3/lib/ipaddress/mongoid.rb
savant-echo-1.0.1 bin/alexa-0.1.2-osx/lib/vendor/ruby/2.2.0/gems/ipaddress-0.8.3/lib/ipaddress/mongoid.rb
savant-echo-1.0.1 bin/packaging/vendor/ruby/2.2.0/gems/ipaddress-0.8.3/lib/ipaddress/mongoid.rb
savant-echo-0.1.91 bin/packaging/vendor/ruby/2.2.0/gems/ipaddress-0.8.3/lib/ipaddress/mongoid.rb
savant-echo-0.1.91 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.9 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.9 bin/packaging/vendor/ruby/2.2.0/gems/ipaddress-0.8.3/lib/ipaddress/mongoid.rb
savant-echo-0.1.6 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.6 bin/packaging/vendor/ruby/2.2.0/gems/ipaddress-0.8.3/lib/ipaddress/mongoid.rb