Class: MetaDb::CheckConstraint

Inherits:
Constraint show all
Defined in:
lib/meta_db/db_object.rb

Overview

Note that #columns is always empty for check constraints

Instance Attribute Summary collapse

Attributes inherited from Constraint

#columns

Attributes inherited from DbObject

#children, #name, #parent

Instance Method Summary collapse

Methods inherited from Constraint

#kind

Methods inherited from DbObject

#<=>, #[], attrs, #dot, #dump, #dump_attrs, init, #inspect, #path

Constructor Details

#initialize(table, name, expression) ⇒ CheckConstraint

Returns a new instance of CheckConstraint.



306
307
308
309
# File 'lib/meta_db/db_object.rb', line 306

def initialize(table, name, expression)
  super(table, name, [])
  @expression = expression
end

Instance Attribute Details

#expressionObject (readonly)

SQL check expression



299
300
301
# File 'lib/meta_db/db_object.rb', line 299

def expression
  @expression
end

Instance Method Details

#ruby_expressionObject

Half-baked SQL-to-ruby expression transpiler



302
303
304
# File 'lib/meta_db/db_object.rb', line 302

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