Class: Sequent::Core::Helpers::DefaultValidators
- Inherits:
-
Object
- Object
- Sequent::Core::Helpers::DefaultValidators
- Defined in:
- lib/sequent/core/helpers/default_validators.rb
Constant Summary collapse
- VALIDATORS =
{ Integer => ->(klass, field) do klass.validates_numericality_of field, only_integer: true, allow_nil: true, allow_blank: true end, Date => ->(klass, field) do klass.validates field, "sequent::Core::Helpers::Date" => true end, DateTime => ->(klass, field) do klass.validates field, "sequent::Core::Helpers::DateTime" => true end, Boolean => -> (klass, field) do klass.validates field, "sequent::Core::Helpers::Boolean" => true end, String => -> (klass, field) do klass.validates field, "sequent::Core::Helpers::String" => true end, Sequent::Core::Helpers::Secret => -> (klass, field) do klass.after_validation do |object| if object.errors&.any? object.send("#{field}=", nil) else raw_value = object.send(field) object.send("#{field}=", Sequent::Secret.new(raw_value)) if raw_value end end end }
Class Method Summary collapse
Instance Method Summary collapse
- #add_validations_for(klass, field) ⇒ Object
-
#initialize(type) ⇒ DefaultValidators
constructor
A new instance of DefaultValidators.
Constructor Details
#initialize(type) ⇒ DefaultValidators
Returns a new instance of DefaultValidators.
43 44 45 |
# File 'lib/sequent/core/helpers/default_validators.rb', line 43 def initialize(type) @type = type end |
Class Method Details
.for(type) ⇒ Object
39 40 41 |
# File 'lib/sequent/core/helpers/default_validators.rb', line 39 def self.for(type) new(type) end |
Instance Method Details
#add_validations_for(klass, field) ⇒ Object
47 48 49 50 |
# File 'lib/sequent/core/helpers/default_validators.rb', line 47 def add_validations_for(klass, field) validator = VALIDATORS[@type] validator.call(klass, field) if validator end |