Class: DataMapper::Validate::GenericValidator
- Defined in:
- lib/gems/dm-validations-0.9.9/lib/dm-validations/generic_validator.rb
Overview
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.
Direct Known Subclasses
AbsentFieldValidator, AcceptanceValidator, ConfirmationValidator, CustomValidator, FormatValidator, LengthValidator, MethodValidator, NumericValidator, PrimitiveValidator, RequiredFieldValidator, UniquenessValidator, WithinValidator
Instance Attribute Summary collapse
Instance Method Summary collapse
- #==(other) ⇒ Object
-
#add_error(target, message, field_name = :general) ⇒ Object
Add an error message to a target resource.
-
#call(target) ⇒ Boolean
Call the validator.
-
#execute?(target) ⇒ Boolean
Determines if this validator should be run against the target by evaluating the :if and :unless clauses optionally passed while specifying any validator.
- #field_name ⇒ Object
-
#initialize(field, opts = {}) ⇒ GenericValidator
constructor
Construct a validator.
Constructor Details
#initialize(field, opts = {}) ⇒ GenericValidator
Construct a validator. Capture the :if and :unless clauses when present.
All additional key/value pairs are passed through to the validator that is sub-classing this GenericValidator
30 31 32 33 |
# File 'lib/gems/dm-validations-0.9.9/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 |
Instance Attribute Details
#if_clause ⇒ Object
16 17 18 |
# File 'lib/gems/dm-validations-0.9.9/lib/dm-validations/generic_validator.rb', line 16 def if_clause @if_clause end |
#unless_clause ⇒ Object
17 18 19 |
# File 'lib/gems/dm-validations-0.9.9/lib/dm-validations/generic_validator.rb', line 17 def unless_clause @unless_clause end |
Instance Method Details
#==(other) ⇒ Object
89 90 91 92 93 94 95 96 |
# File 'lib/gems/dm-validations-0.9.9/lib/dm-validations/generic_validator.rb', line 89 def ==(other) self.class == other.class && self.field_name == other.field_name && self.class == other.class && self.if_clause == other.if_clause && self.unless_clause == other.unless_clause && self.instance_variable_get(:@options) == other.instance_variable_get(:@options) end |
#add_error(target, message, field_name = :general) ⇒ Object
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.
TODO - should the field_name for a general message be :default???
45 46 47 |
# File 'lib/gems/dm-validations-0.9.9/lib/dm-validations/generic_validator.rb', line 45 def add_error(target, , field_name = :general) target.errors.add(field_name,) end |
#call(target) ⇒ Boolean
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 'lib/gems/dm-validations-0.9.9/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?(target) ⇒ Boolean
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 |
# File 'lib/gems/dm-validations-0.9.9/lib/dm-validations/generic_validator.rb', line 69 def execute?(target) if unless_clause = self.unless_clause if unless_clause.is_a?(Symbol) return false if target.send(unless_clause) elsif unless_clause.respond_to?(:call) return false if unless_clause.call(target) end end if if_clause = self.if_clause if if_clause.is_a?(Symbol) return target.send(if_clause) elsif if_clause.respond_to?(:call) return if_clause.call(target) end end true end |
#field_name ⇒ Object
59 60 61 |
# File 'lib/gems/dm-validations-0.9.9/lib/dm-validations/generic_validator.rb', line 59 def field_name @field_name end |