Class: PgMeta::CheckConstraint

Inherits:
Constraint show all
Defined in:
lib/pg_meta/meta.rb

Overview

Note that #columns is always empty for check constraints

Instance Attribute Summary collapse

Attributes inherited from Constraint

#columns

Attributes inherited from Node

#name, #parent, #root

Instance Method Summary collapse

Methods inherited from Constraint

#column, #kind

Methods inherited from Node

#dump, #dump_value, #guid, #inspect, #sid, #to_s, #to_yaml, #uid

Constructor Details

#initialize(table, name, expression) ⇒ CheckConstraint

Returns a new instance of CheckConstraint.



478
479
480
481
482
483
484
485
486
487
488
# File 'lib/pg_meta/meta.rb', line 478

def initialize(table, name, expression)
  super(table, name, [])
  @expression = expression
  table.check_constraints[name] = self
  if @expression =~ /^\(\((\S+) IS NOT NULL\)\)$/
    columns = [table.columns[$1]]
  else
    columns = []
  end
  super(table, name, columns)
end

Instance Attribute Details

#expressionObject (readonly)

SQL check expression



465
466
467
# File 'lib/pg_meta/meta.rb', line 465

def expression
  @expression
end

Instance Method Details

#not_null?Boolean

True if this is a not-null check constraint. In that case, #columns will contain the column which is otherwise empty for CheckConstraint objects. Note that the column is not registered directly in Postgres meta tables and have to be parsed from the expression

Returns:

  • (Boolean)


476
# File 'lib/pg_meta/meta.rb', line 476

def not_null?() !columns.empty? end

#ruby_expressionObject

Half-baked SQL-to-ruby expression transpiler



468
469
470
# File 'lib/pg_meta/meta.rb', line 468

def ruby_expression # Very simple
  @ruby ||= sql.sub(/\((.*)\)/, "\\1").gsub(/\((\w+) IS NOT NULL\)/, "!\\1.nil?").gsub(/ OR /, " || ")
end

#to_hObject



490
# File 'lib/pg_meta/meta.rb', line 490

def to_h() attrs_to_h(:name, :kind, :expression) end