Class: Sequel::Postgres::InetOp

Inherits:
SQL::Wrapper show all
Includes:
SQL::BitwiseMethods
Defined in:
lib/sequel/extensions/pg_inet_ops.rb

Overview

The InetOp class is a simple container for a single object that defines methods that yield Sequel expression objects representing PostgreSQL inet operators and functions.

Most methods in this class are defined via metaprogramming, see the pg_inet_ops extension documentation for details on the API.

Constant Summary collapse

OPERATORS =
{
  :contained_by_or_equals => ["(".freeze, " <<= ".freeze, ")".freeze].freeze,
  :contains_or_equals => ["(".freeze, " >>= ".freeze, ")".freeze].freeze,
  :contains_or_contained_by => ["(".freeze, " && ".freeze, ")".freeze].freeze,
}.freeze

Instance Attribute Summary

Attributes inherited from SQL::Wrapper

#value

Instance Method Summary collapse

Methods included from HStoreOpMethods

#hstore

Methods included from RangeOpMethods

#pg_range

Methods included from ArrayOpMethods

#pg_array

Methods included from JSONOpMethods

#pg_json, #pg_jsonb

Methods included from PGRowOp::ExpressionMethods

#pg_row

Methods included from SQL::SubscriptMethods

#sql_subscript

Methods included from SQL::StringMethods

#ilike, #like

Methods included from SQL::PatternMatchMethods

#=~

Methods included from SQL::OrderMethods

#asc, #desc

Methods included from SQL::NumericMethods

#+

Methods included from SQL::ComplexExpressionMethods

#extract, #sql_boolean, #sql_number, #sql_string

Methods included from SQL::CastMethods

#cast, #cast_numeric, #cast_string

Methods included from SQL::AliasMethods

#as

Methods inherited from SQL::Expression

#==, attr_reader, #eql?, #hash, inherited, #inspect, #lit, #sql_literal

Constructor Details

#initialize(v) ⇒ InetOp

For String and IPAddr instances, wrap them in a cast to inet, to avoid ambiguity issues when calling operator methods.



73
74
75
76
77
78
79
80
81
# File 'lib/sequel/extensions/pg_inet_ops.rb', line 73

def initialize(v)
  case v
  when ::Sequel::LiteralString
    # nothing
  when String, IPAddr
    v = Sequel.cast(v, :inet)
  end
  super
end

Instance Method Details

#-(v) ⇒ Object

Return an expression for the subtraction of the argument from the receiver



124
125
126
127
128
129
130
131
# File 'lib/sequel/extensions/pg_inet_ops.rb', line 124

def -(v)
  case v
  when Integer
    self.class.new(super)
  else
    Sequel::SQL::NumericExpression.new(:NOOP, super)
  end
end

#pg_inetObject

Return the receiver.



114
115
116
# File 'lib/sequel/extensions/pg_inet_ops.rb', line 114

def pg_inet
  self
end

#set_masklen(v) ⇒ Object

Return an expression for the calling of the set_masklen function with the receiver and the given argument



134
135
136
# File 'lib/sequel/extensions/pg_inet_ops.rb', line 134

def set_masklen(v)
  self.class.new(Sequel::SQL::Function.new(:set_masklen, self, v))
end

#~Object

Return an expression for the bitwise NOT of the receiver



119
120
121
# File 'lib/sequel/extensions/pg_inet_ops.rb', line 119

def ~
  self.class.new(super)
end