Class: ROM::Model::Validator::UniquenessValidator

Inherits:
ActiveModel::EachValidator
  • Object
show all
Defined in:
lib/rom/rails/model/validator/uniqueness_validator.rb

Overview

Uniqueness validation

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ UniquenessValidator

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of UniquenessValidator.



23
24
25
26
27
# File 'lib/rom/rails/model/validator/uniqueness_validator.rb', line 23

def initialize(options)
  super
  @klass = options.fetch(:class)
  @message = options.fetch(:message) { :taken }
end

Instance Attribute Details

#klassObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Relation validator class



13
14
15
# File 'lib/rom/rails/model/validator/uniqueness_validator.rb', line 13

def klass
  @klass
end

#messageString, Symbol (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

error message

Returns:

  • (String, Symbol)


20
21
22
# File 'lib/rom/rails/model/validator/uniqueness_validator.rb', line 20

def message
  @message
end

Instance Method Details

#validate_each(validator, name, value) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Hook called by ActiveModel internally



32
33
34
# File 'lib/rom/rails/model/validator/uniqueness_validator.rb', line 32

def validate_each(validator, name, value)
  validator.errors.add(name, message) unless unique?(name, value)
end