module Rein::Constraint::Numericality
This module contains methods for defining numericality constraints.
Constants
- OPERATORS
Public Instance Methods
add_numericality_constraint(*args)
click to toggle source
# File lib/rein/constraint/numericality.rb, line 16 def add_numericality_constraint(*args) reversible do |dir| dir.up { _add_numericality_constraint(*args) } dir.down { _remove_numericality_constraint(*args) } end end
remove_numericality_constraint(*args)
click to toggle source
# File lib/rein/constraint/numericality.rb, line 23 def remove_numericality_constraint(*args) reversible do |dir| dir.up { _remove_numericality_constraint(*args) } dir.down { _add_numericality_constraint(*args) } end end
Private Instance Methods
_add_numericality_constraint(table, attribute, options = {})
click to toggle source
# File lib/rein/constraint/numericality.rb, line 32 def _add_numericality_constraint(table, attribute, options = {}) name = Util.constraint_name(table, attribute, 'numericality', options) table = Util.wrap_identifier(table) attribute = Util.wrap_identifier(attribute) conditions = OPERATORS.slice(*options.keys).map do |key, operator| value = options[key] [attribute, operator, value].join(' ') end.join(' AND ') conditions = Util.conditions_with_if(conditions, options) sql = "ALTER TABLE #{table} ADD CONSTRAINT #{name} CHECK (#{conditions})" execute(Util.add_not_valid_suffix_if_required(sql, options)) end
_remove_numericality_constraint(table, attribute, options = {})
click to toggle source
# File lib/rein/constraint/numericality.rb, line 45 def _remove_numericality_constraint(table, attribute, options = {}) name = Util.constraint_name(table, attribute, 'numericality', options) table = Util.wrap_identifier(table) execute("ALTER TABLE #{table} DROP CONSTRAINT #{name}") end