Class: DataMapper::Validations::ContextualValidators
- Extended by:
- Forwardable
- Includes:
- Enumerable
- Defined in:
- lib/dm-validations/contextual_validators.rb
Overview
Instance Attribute Summary collapse
- #contexts ⇒ Object readonly
Instance Method Summary collapse
-
#clear! ⇒ Object
Clear all named context validators off of the resource.
-
#context(name) ⇒ Array<DataMapper::Validations::GenericValidator>
Return an array of validators for a named context.
-
#execute(named_context, target) ⇒ Boolean
Execute all validators in the named context against the target.
-
#initialize ⇒ ContextualValidators
constructor
A new instance of ContextualValidators.
Constructor Details
#initialize ⇒ ContextualValidators
Returns a new instance of ContextualValidators.
22 23 24 |
# File 'lib/dm-validations/contextual_validators.rb', line 22 def initialize @contexts = {} end |
Instance Attribute Details
#contexts ⇒ Object (readonly)
20 21 22 |
# File 'lib/dm-validations/contextual_validators.rb', line 20 def contexts @contexts end |
Instance Method Details
#clear! ⇒ Object
Clear all named context validators off of the resource
42 43 44 |
# File 'lib/dm-validations/contextual_validators.rb', line 42 def clear! contexts.clear end |
#context(name) ⇒ Array<DataMapper::Validations::GenericValidator>
Return an array of validators for a named context
36 37 38 |
# File 'lib/dm-validations/contextual_validators.rb', line 36 def context(name) contexts[name] ||= [] end |
#execute(named_context, target) ⇒ Boolean
Execute all validators in the named context against the target
54 55 56 57 58 59 60 |
# File 'lib/dm-validations/contextual_validators.rb', line 54 def execute(named_context, target) target.errors.clear! context(named_context).map do |validator| validator.execute?(target) ? validator.call(target) : true end.all? end |