Class: DataMapper::Validations::ContextualValidators

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Enumerable
Defined in:
lib/dm-validations/contextual_validators.rb

Overview

Author:

  • Guy van den Berg

Since:

  • 0.9

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeContextualValidators

Returns a new instance of ContextualValidators.

Since:

  • 0.9



22
23
24
# File 'lib/dm-validations/contextual_validators.rb', line 22

def initialize
  @contexts = {}
end

Instance Attribute Details

#contextsObject (readonly)

Since:

  • 0.9



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

Since:

  • 0.9



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

Parameters:

  • Context (String)

    name for which return validators

Returns:

Since:

  • 0.9



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

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

Since:

  • 0.9



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