Module: DynamicValidation
- Defined in:
- lib/dynamic_validation.rb,
lib/dynamic_validation/version.rb
Overview
DynamicValidation
allows you to add validations at runtime # withouth having validations affect all of your records.
Defined Under Namespace
Classes: BlockValidator, Version
Constant Summary
Class Method Summary collapse
Instance Method Summary collapse
-
#add_validators(*args, &block) ⇒ Object
(also: #add_validator)
add_validator is the only public method that is added which allows any instance of a model to add a validator dyanmically.
-
#delete_validator(validator) ⇒ Object
delete_valdiators allows for a previously added validator to be removed from the list of validations to be ran.
Class Method Details
.included(klass) ⇒ Object
27 28 29 |
# File 'lib/dynamic_validation.rb', line 27 def self.included(klass) klass.validate :dynamic_validations end |
Instance Method Details
#add_validators(*args, &block) ⇒ Object Also known as: add_validator
add_validator is the only public method that is added which allows any instance of a model to add a validator dyanmically.
Usage
57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/dynamic_validation.rb', line 57 def add_validators(*args, &block) @dynamic_validators = {} unless defined? @dynamic_validators = args. args.each do |validator| if !validator.new().respond_to?(:validate) || validator.new().method(:validate).arity != 1 raise NotImplementedError, "#{validator} must implement a validate(record) method." end @dynamic_validators[validator] = end add_validator BlockValidator, block: block if block_given? end |
#delete_validator(validator) ⇒ Object
delete_valdiators allows for a previously added validator to be removed from the list of validations to be ran
73 74 75 |
# File 'lib/dynamic_validation.rb', line 73 def delete_validator(validator) @dynamic_validators.delete(validator) if defined? @dynamic_validators end |