Class: DataMapper::Validate::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::Validate::GenericValidator>
Return an array of validators for a named context.
-
#dump ⇒ Object
(also: #inspect)
API.
-
#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
49 50 51 |
# File 'lib/dm-validations/contextual_validators.rb', line 49 def clear! contexts.clear end |
#context(name) ⇒ Array<DataMapper::Validate::GenericValidator>
Return an array of validators for a named context
43 44 45 |
# File 'lib/dm-validations/contextual_validators.rb', line 43 def context(name) contexts[name] ||= [] end |
#dump ⇒ Object Also known as: inspect
API
30 31 32 33 34 |
# File 'lib/dm-validations/contextual_validators.rb', line 30 def dump contexts.each_pair do |key, context| puts "Key=#{key} Context: #{context}" end end |
#execute(named_context, target) ⇒ Boolean
Execute all validators in the named context against the target
61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/dm-validations/contextual_validators.rb', line 61 def execute(named_context, target) raise(ArgumentError, "validation context #{named_context} doesn't seem to be defined. Known contexts are #{contexts.keys.inspect}") if !named_context || (contexts.length > 0 && !contexts[named_context]) target.errors.clear! result = true # note that all? and any? stop iteration on first negative or positive result, # so we really have to use each here to make sure all validators are # executed context(named_context).select { |validator| validator.execute?(target) }.each do |validator| result = false unless validator.call(target) end result end |