Module: ROM::Model::Validator

Defined in:
lib/rom/rails/model/validator.rb,
lib/rom/rails/model/validator/uniqueness_validator.rb

Overview

Mixin for ROM-compliant validator objects

Examples:


class UserAttributes
  include ROM::Model::Attributes

  attribute :name

  validates :name, presence: true
end

class UserValidator
  include ROM::Model::Validator
end

attrs = UserAttributes.new(name: '')
UserValidator.call(attrs) # raises ValidationError

Defined Under Namespace

Modules: ClassMethods Classes: UniquenessValidator

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name) ⇒ Object (private)

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.

This is needed for ActiveModel::Validations to work properly as it expects the object to provide attribute values. Meh.



75
76
77
# File 'lib/rom/rails/model/validator.rb', line 75

def method_missing(name)
  attributes[name]
end

Instance Attribute Details

#attributesModel::Attributes (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.

Returns:



41
42
43
# File 'lib/rom/rails/model/validator.rb', line 41

def attributes
  @attributes
end

Class Method Details

.included(base) ⇒ 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.

Inclusion hook that extends a class with required interfaces



30
31
32
33
34
35
36
# File 'lib/rom/rails/model/validator.rb', line 30

def self.included(base)
  base.class_eval do
    extend ClassMethods
    include ActiveModel::Validations
    include Equalizer.new(:attributes, :errors)
  end
end

Instance Method Details

#callModel::Attributes

Trigger validations and return attributes on success

Returns:

Raises:



64
65
66
67
# File 'lib/rom/rails/model/validator.rb', line 64

def call
  raise ValidationError, errors unless valid?
  attributes
end

#initialize(attributes) ⇒ 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.



46
47
48
# File 'lib/rom/rails/model/validator.rb', line 46

def initialize(attributes)
  @attributes = attributes
end

#to_modelModel::Attributes

Returns:



53
54
55
# File 'lib/rom/rails/model/validator.rb', line 53

def to_model
  attributes
end