Sha256: 1936eaf924fd8897768d560f71ba3c972484dae680f8f207e89fbf3f79281720

Contents?: true

Size: 726 Bytes

Versions: 2

Compression:

Stored size: 726 Bytes

Contents

class EqualityValidator < ActiveModel::EachValidator

  def validate_each(record, attribute, value)
    to_value = options[:to]
    operator = OPERATORS.fetch(options[:operator])

    if (options[:operator] == :less_than) || (options[:operator] == :greater_than)
	    raise Exception if to_value.nil? || operator.nil?
  	end

    unless value.send(operator, record.send(to_value.to_sym))
      record.errors[attribute] << (options[:message] || I18n.t('yukonisuru.errors.messages.equality', attr: to_value, operator: operator))
    end
  end

  private

  OPERATORS = {
    less_than: :<,
    less_than_or_equal_to: :<=,
    greater_than: :>,
    greater_than_or_equal_to: :>=,
    equal_to: :==,
    not_equal_to: :!=
  }

end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
yukonisuru-1.0.0 lib/yukonisuru/validators/equality_validator.rb
yukonisuru-0.0.1 lib/yukonisuru/validators/equality_validator.rb