Class: DataMapper::Validate::ContextualValidators
- Defined in:
- lib/gems/dm-validations-0.9.7/lib/dm-validations/contextual_validators.rb
Overview
Instance Method Summary collapse
-
#clear! ⇒ Object
Clear all named context validators off of the resource.
-
#context(name) ⇒ Array
Return an array of validators for a named context.
-
#contexts ⇒ Hash
Get a hash of named context validators for the resource.
- #dump ⇒ Object
-
#execute(named_context, target) ⇒ Boolean
Execute all validators in the named context against the target.
Instance Method Details
#clear! ⇒ Object
Clear all named context validators off of the resource
33 34 35 |
# File 'lib/gems/dm-validations-0.9.7/lib/dm-validations/contextual_validators.rb', line 33 def clear! contexts.clear end |
#context(name) ⇒ Array
Return an array of validators for a named context
26 27 28 29 |
# File 'lib/gems/dm-validations-0.9.7/lib/dm-validations/contextual_validators.rb', line 26 def context(name) contexts[name] = [] unless contexts.has_key?(name) contexts[name] end |
#contexts ⇒ Hash
Get a hash of named context validators for the resource
19 20 21 |
# File 'lib/gems/dm-validations-0.9.7/lib/dm-validations/contextual_validators.rb', line 19 def contexts @contexts ||= @contexts = {} end |
#dump ⇒ Object
10 11 12 13 14 |
# File 'lib/gems/dm-validations-0.9.7/lib/dm-validations/contextual_validators.rb', line 10 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
42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/gems/dm-validations-0.9.7/lib/dm-validations/contextual_validators.rb', line 42 def execute(named_context, target) raise(ArgumentError, 'invalid context specified') if !named_context || (contexts.length > 0 && !contexts[named_context]) target.errors.clear! result = true context(named_context).each do |validator| next unless validator.execute?(target) result = false unless validator.call(target) end result end |