Sha256: eccfd602bcfff38cfc982f141c7753b5c5aeccc0b07c54c469f0a44e267d4c5f

Contents?: true

Size: 751 Bytes

Versions: 1

Compression:

Stored size: 751 Bytes

Contents

module RC
  module Numericality
    OPERATORS = {
      :greater_than             => :>,
      :greater_than_or_equal_to => :>=,
      :equal_to                 => :==,
      :less_than                => :<,
      :less_than_or_equal_to    => :<=
    }.freeze

    def add_numericality_constraint(table, attribute, options = {})
      conditions = OPERATORS.slice(*options.keys).map do |key, operator|
        value = options[key]
        [attribute, operator, value].join(" ")
      end

      conditions_sql = conditions.map {|condition| "(#{condition})" }.join(" AND ")
      conditions_sql = "(#{conditions_sql})" if conditions.length > 1

      execute "ALTER TABLE #{table} ADD CONSTRAINT #{attribute} CHECK #{conditions_sql}"
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rein-0.2.0 lib/rein/constraint/numericality.rb