Class: DataMapper::Validate::ContextualValidators

Inherits:
Object
  • Object
show all
Defined in:
lib/dm-validations/contextual_validators.rb

Overview

Author:

  • Guy van den Berg

Since:

  • 0.9

Instance Method Summary collapse

Instance Method Details

#clear!Object

Clear all named context validators off of the resource

Since:

  • 0.9


32
33
34
# File 'lib/dm-validations/contextual_validators.rb', line 32

def clear!
  contexts.clear
end

#context(name) ⇒ Array

Return an array of validators for a named context

Returns:

  • (Array)

    An array of validators

Since:

  • 0.9


26
27
28
# File 'lib/dm-validations/contextual_validators.rb', line 26

def context(name)
  contexts[name] ||= []
end

#contextsHash

Get a hash of named context validators for the resource

Returns:

  • (Hash)

    a hash of validators <GenericValidator>

Since:

  • 0.9


19
20
21
# File 'lib/dm-validations/contextual_validators.rb', line 19

def contexts
  @contexts ||= {}
end

#dumpObject

Since:

  • 0.9


10
11
12
13
14
# File '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

Parameters:

  • named_context (Symbol)

    the context we are validating against

  • target (Object)

    the resource that we are validating

Returns:

  • (Boolean)

    true if all are valid, otherwise false

Raises:

  • (ArgumentError)

Since:

  • 0.9


41
42
43
44
45
46
47
48
49
50
51
# File 'lib/dm-validations/contextual_validators.rb', line 41

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