Module: ROM::Model::Validator

Defined in:
lib/rom/model/validator.rb,
lib/rom/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, *args, &block) ⇒ 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.



120
121
122
123
124
125
126
# File 'lib/rom/model/validator.rb', line 120

def method_missing(name, *args, &block)
  if attr_names.include?(name)
    attributes[name]
  else
    super
  end
end

Instance Attribute Details

#attr_namesObject (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.



72
73
74
# File 'lib/rom/model/validator.rb', line 72

def attr_names
  @attr_names
end

#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:



69
70
71
# File 'lib/rom/model/validator.rb', line 69

def attributes
  @attributes
end

#parentObject (readonly)



82
83
84
# File 'lib/rom/model/validator.rb', line 82

def parent
  @parent
end

#rootObject (readonly)



77
78
79
# File 'lib/rom/model/validator.rb', line 77

def root
  @root
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



52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/rom/model/validator.rb', line 52

def self.included(base)
  base.class_eval do
    extend ClassMethods
    extend ROM::ClassMacros

    include ActiveModel::Validations
    include Dry::Equalizer(:attributes, :errors)

    base.defines :embedded_validators

    embedded_validators({})
  end
end

Instance Method Details

#callModel::Attributes

Trigger validations and return attributes on success

Returns:

Raises:



108
109
110
111
# File 'lib/rom/model/validator.rb', line 108

def call
  raise ValidationError, errors unless valid?
  attributes
end

#initialize(attributes, root = attributes, parent = nil) ⇒ 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.



87
88
89
90
91
92
# File 'lib/rom/model/validator.rb', line 87

def initialize(attributes, root = attributes, parent = nil)
  @attributes = attributes
  @root = root
  @parent = parent
  @attr_names = self.class.validators.map(&:attributes).flatten.uniq
end

#to_modelModel::Attributes

Returns:



97
98
99
# File 'lib/rom/model/validator.rb', line 97

def to_model
  attributes
end