Module: ROM::Model::Validator::ClassMethods
- Defined in:
- lib/rom/model/validator.rb
Instance Method Summary collapse
-
#>>(other) ⇒ Composite
Compose a validation with a command.
-
#call(attributes) ⇒ Model::Attributes
Trigger validation for specific attributes.
-
#embedded(name, options = {}, &block) ⇒ Object
Specify an embedded validator for nested structures.
-
#relation(name = nil) ⇒ Symbol
Set relation name for a validator.
- #set_model_name(name) ⇒ Object private
Instance Method Details
#>>(other) ⇒ Composite
Compose a validation with a command
The command will be called if validations succeed
183 184 185 |
# File 'lib/rom/model/validator.rb', line 183 def >>(other) Composite.new(self, other) end |
#call(attributes) ⇒ Model::Attributes
Trigger validation for specific attributes
167 168 169 170 |
# File 'lib/rom/model/validator.rb', line 167 def call(attributes) validator = new(attributes) validator.call end |
#embedded(name, options = {}, &block) ⇒ Object
Specify an embedded validator for nested structures
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 |
# File 'lib/rom/model/validator.rb', line 211 def (name, = {}, &block) presence = .fetch(:presence, true) validator_class = Class.new { include ROM::Model::Validator } validator_class.set_model_name(name.to_s.classify) validator_class.class_eval(&block) [name] = validator_class validates name, presence: true if presence validate do value = attributes[name] if value.present? Array([value]).flatten.each do |object| validator = validator_class.new(object, root, attributes) validator.validate if validator.errors.any? errors.add(name, validator.errors) else errors.add(name, []) end end end end end |
#relation(name = nil) ⇒ Symbol
Set relation name for a validator
This is needed for validators that require database access
146 147 148 149 |
# File 'lib/rom/model/validator.rb', line 146 def relation(name = nil) @relation = name if name @relation end |
#set_model_name(name) ⇒ 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.
152 153 154 155 156 157 158 |
# File 'lib/rom/model/validator.rb', line 152 def set_model_name(name) class_eval " def self.model_name\n @model_name ||= ActiveModel::Name.new(self, nil, \#{name.inspect})\n end\n RUBY\nend\n" |