module Rein::Constraint::Length

This module contains methods for defining length constraints.

Constants

OPERATORS

Public Instance Methods

add_length_constraint(*args) click to toggle source
# File lib/rein/constraint/length.rb, line 16
def add_length_constraint(*args)
  reversible do |dir|
    dir.up { _add_length_constraint(*args) }
    dir.down { _remove_length_constraint(*args) }
  end
end
remove_length_constraint(*args) click to toggle source
# File lib/rein/constraint/length.rb, line 23
def remove_length_constraint(*args)
  reversible do |dir|
    dir.up { _remove_length_constraint(*args) }
    dir.down { _add_length_constraint(*args) }
  end
end

Private Instance Methods

_add_length_constraint(table, attribute, options = {}) click to toggle source
# File lib/rein/constraint/length.rb, line 32
def _add_length_constraint(table, attribute, options = {})
  name = Util.constraint_name(table, attribute, 'length', options)
  table = Util.wrap_identifier(table)
  attribute = Util.wrap_identifier(attribute)
  attribute_length = "length(#{attribute})"
  conditions = OPERATORS.slice(*options.keys).map do |key, operator|
    value = options[key]
    [attribute_length, 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_length_constraint(table, attribute, options = {}) click to toggle source
# File lib/rein/constraint/length.rb, line 46
def _remove_length_constraint(table, attribute, options = {})
  name = Util.constraint_name(table, attribute, 'length', options)
  table = Util.wrap_identifier(table)
  execute("ALTER TABLE #{table} DROP CONSTRAINT #{name}")
end