Sha256: 33a8fbaa7a3a0d238a49e62e3d7243236235e898ddd5f38f27463c81ef4e7693

Contents?: true

Size: 1.5 KB

Versions: 2

Compression:

Stored size: 1.5 KB

Contents

require "rein/util"

module Rein
  module Constraint
    # This module contains methods for defining length constraints.
    module Length
      OPERATORS = {
        greater_than: :>,
        greater_than_or_equal_to: :>=,
        equal_to: :"=",
        not_equal_to: :"!=",
        less_than: :<,
        less_than_or_equal_to: :<=
      }.freeze

      def add_length_constraint(*args)
        reversible do |dir|
          dir.up { _add_length_constraint(*args) }
          dir.down { _remove_length_constraint(*args) }
        end
      end

      def remove_length_constraint(*args)
        reversible do |dir|
          dir.up { _remove_length_constraint(*args) }
          dir.down { _add_length_constraint(*args) }
        end
      end

      private

      def _add_length_constraint(table, attribute, options = {})
        name = Util.constraint_name(table, attribute, "length", options)
        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)
        execute("ALTER TABLE #{table} ADD CONSTRAINT #{name} CHECK (#{conditions})")
      end

      def _remove_length_constraint(table, attribute, options = {})
        name = Util.constraint_name(table, attribute, "length", options)
        execute("ALTER TABLE #{table} DROP CONSTRAINT #{name}")
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
rein-3.2.0 lib/rein/constraint/length.rb
rein-3.1.0 lib/rein/constraint/length.rb