module ROM::Model::Validator
Mixin for ROM-compliant validator objects
@example
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
@api public
Attributes
attr_names[R]
@api private
attributes[R]
@return [Model::Attributes]
@api private
parent[R]
@attr_reader [Object] root The parent node in attributes hash of an embedded validator
@api public
root[R]
@attr_reader [Object] root The root node in attributes hash of an embedded validator
@api public
Public Class Methods
included(base)
click to toggle source
Inclusion hook that extends a class with required interfaces
@api private
# File lib/rom/model/validator.rb, line 51 def self.included(base) base.class_eval do extend ClassMethods extend Dry::Core::ClassAttributes include ActiveModel::Validations include Dry::Equalizer(:attributes, :errors) base.defines :embedded_validators embedded_validators({}) end end
new(attributes, root = attributes, parent = nil)
click to toggle source
@api private
# File lib/rom/model/validator.rb, line 86 def initialize(attributes, root = attributes, parent = nil) @attributes = attributes @root = root @parent = parent @attr_names = self.class.validators.map(&:attributes).flatten.uniq end
Public Instance Methods
call()
click to toggle source
Trigger validations and return attributes on success
@raises ValidationError
@return [Model::Attributes]
@api public
# File lib/rom/model/validator.rb, line 107 def call raise ValidationError, errors unless valid? attributes end
to_model()
click to toggle source
@return [Model::Attributes]
@api public
# File lib/rom/model/validator.rb, line 96 def to_model attributes end
Private Instance Methods
method_missing(name, *args, &block)
click to toggle source
This is needed for ActiveModel::Validations to work properly as it expects the object to provide attribute values. Meh.
@api private
Calls superclass method
# File lib/rom/model/validator.rb, line 119 def method_missing(name, *args, &block) if attr_names.include?(name) attributes[name] else super end end