pg_inet_ops.rb

lib/sequel/extensions/pg_inet_ops.rb
Last Update: 2021-12-18 12:49:34 -0800

The pg_inet_ops extension adds support to Sequel’s DSL to make it easier to call PostgreSQL inet functions and operators.

To load the extension:

Sequel.extension :pg_inet_ops

The most common usage is passing an expression to Sequel.pg_inet_op:

r = Sequel.pg_inet_op(:inet)

Also, on most Sequel expression objects, you can call the pg_inet method:

r = Sequel[:ip].pg_inet

If you have loaded the core_extensions extension, or you have loaded the core_refinements extension and have activated refinements for the file, you can also use Symbol#pg_inet:

r = :inet.pg_inet

This creates a Sequel::Postgres::InetOp object that can be used for easier querying:

~r                                 # ~inet
r & other                          # inet & other
r | other                          # inet | other
r << :other                        # inet << other
r >> :other                        # inet >> other

r.contained_by(:other)             # inet << other
r.contained_by_or_equals(:other)   # inet <<= other
r.contains(:other)                 # inet >> other
r.contains_or_equals(:other)       # inet >>= other
r.contains_or_contained_by(:other) # inet && other

r.abbrev           # abbrev(inet)
r.broadcast        # broadcast(inet)
r.family           # family(inet)
r.host             # host(inet)
r.hostmask         # hostmask(inet)
r.masklen          # masklen(inet)
r.netmask          # netmask(inet)
r.network          # network(inet)
r.set_masklen(16)  # set_masklen(inet, 16)
r.text             # text(inet)

If a String or IPAddr instance is passed to Sequel.pg_inet_op, it will automatically be cast to inet. To treat the object as a cidr, you must cast it before passing it to Sequel.pg_inet_op:

r = Sequel.pg_inet_op(Sequel.cast('1.2.3.4', :cidr))

See the PostgreSQL network function and operator documentation for more details on what these functions and operators do.

Related module: Sequel::Postgres::InetOp

Required files

  1. ipaddr