Class: ValidatedObject::Base Abstract
- Inherits:
-
Object
- Object
- ValidatedObject::Base
- Extended by:
- T::Sig
- Includes:
- ActiveModel::Validations
- Defined in:
- lib/validated_object.rb
Overview
This class is abstract.
Subclass and add ‘attr_accessor` and validations to create custom validating objects.
Uses ActiveModel::Validations to create self-validating Plain Old Ruby objects. This is especially useful when importing data from one system into another. This class also creates very readable error messages.
Defined Under Namespace
Classes: Boolean, TypeValidator
Constant Summary collapse
- SymbolHash =
T.type_alias { T::Hash[Symbol, T.untyped] }
- EMPTY_HASH =
T.let({}.freeze, SymbolHash)
Instance Method Summary collapse
- #check_validations! ⇒ Object
-
#initialize(attributes = EMPTY_HASH) ⇒ Base
constructor
A new instance of Base.
Constructor Details
#initialize(attributes = EMPTY_HASH) ⇒ Base
Returns a new instance of Base.
67 68 69 70 71 |
# File 'lib/validated_object.rb', line 67 def initialize(attributes = EMPTY_HASH) set_instance_variables from_hash: attributes check_validations! nil end |
Instance Method Details
#check_validations! ⇒ Object
78 79 80 81 82 |
# File 'lib/validated_object.rb', line 78 def check_validations! raise ArgumentError, errors..join('; ') if invalid? self end |