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

Inherits:
ActiveModel::EachValidator
  • Object
show all
Defined in:
lib/rom/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
28
# File 'lib/rom/model/validator/uniqueness_validator.rb', line 23

def initialize(options)
  super
  @klass = options.fetch(:class)
  @message = options.fetch(:message) { :taken }
  @scope_keys = options[:scope]
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/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/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



33
34
35
36
37
38
# File 'lib/rom/model/validator/uniqueness_validator.rb', line 33

def validate_each(validator, name, value)
  scope = Array(@scope_keys).each_with_object({}) do |key, scope|
    scope[key] = validator.to_model[key]
  end
  validator.errors.add(name, message) unless unique?(name, value, scope)
end