Class: ROM::Model::Validator::UniquenessValidator
- Inherits:
-
ActiveModel::EachValidator
- Object
- ActiveModel::EachValidator
- ROM::Model::Validator::UniquenessValidator
- Defined in:
- lib/rom/model/validator/uniqueness_validator.rb
Overview
Uniqueness validation
Instance Attribute Summary collapse
-
#klass ⇒ Object
readonly
private
Relation validator class.
-
#message ⇒ String, Symbol
readonly
private
error message.
Instance Method Summary collapse
-
#initialize(options) ⇒ UniquenessValidator
constructor
private
A new instance of UniquenessValidator.
-
#validate_each(validator, name, value) ⇒ Object
private
Hook called by ActiveModel internally.
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() super @klass = .fetch(:class) = .fetch(:message) { :taken } @scope_keys = [:scope] end |
Instance Attribute Details
#klass ⇒ Object (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 |
#message ⇒ String, 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
20 21 22 |
# File 'lib/rom/model/validator/uniqueness_validator.rb', line 20 def 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, ) unless unique?(name, value, scope) end |