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
Defined Under Namespace
Modules: ClassMethods Classes: UniquenessValidator
Instance Attribute Summary collapse
- #attr_names ⇒ Object readonly private
- #attributes ⇒ Model::Attributes readonly private
- #parent ⇒ Object readonly
- #root ⇒ Object readonly
Class Method Summary collapse
-
.included(base) ⇒ Object
private
Inclusion hook that extends a class with required interfaces.
Instance Method Summary collapse
-
#call ⇒ Model::Attributes
Trigger validations and return attributes on success.
- #initialize(attributes, root = attributes, parent = nil) ⇒ Object private
- #to_model ⇒ Model::Attributes
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.
119 120 121 122 123 124 125 |
# 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 |
Instance Attribute Details
#attr_names ⇒ 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.
71 72 73 |
# File 'lib/rom/model/validator.rb', line 71 def attr_names @attr_names end |
#attributes ⇒ Model::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.
68 69 70 |
# File 'lib/rom/model/validator.rb', line 68 def attributes @attributes end |
#parent ⇒ Object (readonly)
81 82 83 |
# File 'lib/rom/model/validator.rb', line 81 def parent @parent 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
51 52 53 54 55 56 57 58 59 60 61 62 63 |
# 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 ({}) end end |
Instance Method Details
#call ⇒ Model::Attributes
Trigger validations and return attributes on success
107 108 109 110 |
# File 'lib/rom/model/validator.rb', line 107 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.
86 87 88 89 90 91 |
# 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 |
#to_model ⇒ Model::Attributes
96 97 98 |
# File 'lib/rom/model/validator.rb', line 96 def to_model attributes end |