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
Construct a validator. Capture the :if and :unless clauses when present.
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
Public Instance Method Details
add_error
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.
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
Call the validator. "call" is used so the operation is BoundMethod and Block compatible. This must be implemented in all concrete classes.
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?
Determines if this validator should be run against the target by evaluating the :if and :unless clauses optionally passed while specifying any validator.
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
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