Class: DataMapper::Validate::GenericValidator

  • Object
    • DataMapper::Validate::GenericValidator

All validators extend this base class. Validators must:

  • Implement the initialize method to capture its parameters, also calling super to have this parent class capture the optional, general :if and :unless parameters.
  • Implement the call method, returning true or false. The call method provides the validation logic.

Attributes

Instance Attributes

if_clause [RW] public

Sets the attribute if_clause.

unless_clause [RW] public

Sets the attribute unless_clause.

Constructor Summary

public initialize(field, opts = {})

Construct a validator. Capture the :if and :unless clauses when present.

Meta Tags

Parameters:

field<String,

Symbol> The property specified for validation

[View source]


30
31
32
33
# File 'dm-more/dm-validations/lib/dm-validations/generic_validator.rb', line 30

def initialize(field, opts = {})
  @if_clause = opts.has_key?(:if) ? opts[:if] : nil
  @unless_clause = opts.has_key?(:unless) ? opts[:unless] : nil
end

Public Visibility

Public Instance Method Summary

#add_error(target, message, field_name = :general)

Add an error message to a target resource.

#call(target)

Call the validator.

Returns:

#execute?(target)

Determines if this validator should be run against the target by evaluating the :if and :unless clauses optionally passed while specifying any validator.

Returns:

#field_name

Public Instance Methods Inherited from Object

validatable?

Public Instance Method Details

add_error

public add_error(target, message, field_name = :general)

Add an error message to a target resource. If the error corresponds to a specific field of the resource, add it to that field, otherwise add it as a :general message.

Meta Tags

Parameters:

<Object>

target the resource that has the error

<String>

message the message to add

<Symbol>

field_name the name of the field that caused the error

TODO - should the field_name for a general message be :default???

[View source]


45
46
47
# File 'dm-more/dm-validations/lib/dm-validations/generic_validator.rb', line 45

def add_error(target, message, field_name = :general)
  target.errors.add(field_name,message)
end

call

public call(target)

Call the validator. "call" is used so the operation is BoundMethod and Block compatible. This must be implemented in all concrete classes.

Meta Tags

Parameters:

<Object>

target the resource that the validator must be called against

Returns:

<Boolean> true if valid, otherwise false

[View source]


55
56
57
# File 'dm-more/dm-validations/lib/dm-validations/generic_validator.rb', line 55

def call(target)
  raise "DataMapper::Validate::GenericValidator::call must be overriden in #{self.class.to_s}"
end

execute?

public execute?(target)

Determines if this validator should be run against the target by evaluating the :if and :unless clauses optionally passed while specifying any validator.

Meta Tags

Parameters:

<Object>

target the resource that we check against

Returns:

<Boolean> true if should be run, otherwise false

[View source]


69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'dm-more/dm-validations/lib/dm-validations/generic_validator.rb', line 69

def execute?(target)
  return true if self.if_clause.nil? && self.unless_clause.nil?

  if self.unless_clause




    if self.unless_clause.is_a?(Symbol)




      return false if target.send(self.unless_clause)
    elsif self.unless_clause.respond_to?(:call)
      return false if self.unless_clause.call(target)
    end
  end

  if self.if_clause




    if self.if_clause.is_a?(Symbol)




      return target.send(self.if_clause)
    elsif self.if_clause.respond_to?(:call)
      return self.if_clause.call(target)
    end
  end
  return true
end

field_name

public field_name
[View source]


59
60
61
62
63
64
# File 'dm-more/dm-validations/lib/dm-validations/generic_validator.rb', line 59

def field_name



  @field_name
end