Sha256: acd58ef7356e8758ff92599deedb65391c396e35695d71b9532a19ad1eca2203

Contents?: true

Size: 1.51 KB

Versions: 2

Compression:

Stored size: 1.51 KB

Contents

require 'rein/util'

module Rein
  module Constraint
    # This module contains methods for defining regex match constraints.
    module Match
      include ActiveRecord::ConnectionAdapters::Quoting

      OPERATORS = {
        accepts: :~,
        rejects: :"!~"
      }.freeze

      def add_match_constraint(*args)
        reversible do |dir|
          dir.up { _add_match_constraint(*args) }
          dir.down { _remove_match_constraint(*args) }
        end
      end

      def remove_match_constraint(*args)
        reversible do |dir|
          dir.up { _remove_match_constraint(*args) }
          dir.down { _add_match_constraint(*args) }
        end
      end

      private

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

      def _remove_match_constraint(table, attribute, options = {})
        name = Util.constraint_name(table, attribute, 'match', options)
        table = Util.wrap_identifier(table)
        execute("ALTER TABLE #{table} DROP CONSTRAINT #{name}")
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
rein-5.0.0 lib/rein/constraint/match.rb
rein-4.0.0 lib/rein/constraint/match.rb