class CouchRest::Validation::ContextualValidators

@author Guy van den Berg @since 0.9

Public Instance Methods

clear!() click to toggle source

Clear all named context validators off of the resource

# File lib/couchrest/validation/contextual_validators.rb, line 55
def clear!
  contexts.clear
end
context(name) click to toggle source

Return an array of validators for a named context

@return <Array> An array of validators

# File lib/couchrest/validation/contextual_validators.rb, line 49
def context(name)
  contexts[name] ||= []
end
contexts() click to toggle source

Get a hash of named context validators for the resource

@return <Hash> a hash of validators <GenericValidator>

# File lib/couchrest/validation/contextual_validators.rb, line 42
def contexts
  @contexts ||= {}
end
dump() click to toggle source
# File lib/couchrest/validation/contextual_validators.rb, line 33
def dump
  contexts.each_pair do |key, context|
    puts "Key=#{key} Context: #{context}"
  end
end
execute(named_context, target) click to toggle source

Execute all validators in the named context against the target

@param <Symbol> named_context the context we are validating against @param <Object> target the resource that we are validating @return <Boolean> true if all are valid, otherwise false

# File lib/couchrest/validation/contextual_validators.rb, line 64
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