Class: DataMapper::Validations::GenericValidator
- Defined in:
- lib/dm-validations/validators/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
AbsenceValidator, AcceptanceValidator, ConfirmationValidator, FormatValidator, LengthValidator, MethodValidator, NumericalityValidator, PresenceValidator, PrimitiveTypeValidator, UniquenessValidator, WithinValidator
Instance Attribute Summary collapse
- #field_name ⇒ Object readonly
- #humanized_field_name ⇒ Object readonly
- #if_clause ⇒ Object
- #options ⇒ Object readonly
- #unless_clause ⇒ Object
Instance Method Summary collapse
-
#==(other) ⇒ Object
Returns true if validators are equal.
-
#add_error(target, message, field_name = :general) ⇒ Object
Add an error message to a target resource.
-
#allow_blank?(value) ⇒ Boolean
Test if the value is blank and is allowed.
-
#allow_nil?(value) ⇒ Boolean
Test if the value is nil and is allowed.
-
#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.
-
#initialize(field_name, options = {}) ⇒ GenericValidator
constructor
Construct a validator.
- #inspect ⇒ Object (also: #to_s)
-
#optional?(value) ⇒ Boolean
Test the value to see if it is blank or nil, and if it is allowed.
-
#set_optional_by_default(default = true) ⇒ undefined
Set the default value for allow_nil and allow_blank.
Constructor Details
#initialize(field_name, options = {}) ⇒ 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
31 32 33 34 35 36 37 |
# File 'lib/dm-validations/validators/generic_validator.rb', line 31 def initialize(field_name, = {}) @field_name = field_name @options = .except(:if, :unless) @if_clause = [:if] @unless_clause = [:unless] @humanized_field_name = DataMapper::Inflector.humanize(@field_name) end |
Instance Attribute Details
#field_name ⇒ Object (readonly)
18 19 20 |
# File 'lib/dm-validations/validators/generic_validator.rb', line 18 def field_name @field_name end |
#humanized_field_name ⇒ Object (readonly)
18 19 20 |
# File 'lib/dm-validations/validators/generic_validator.rb', line 18 def humanized_field_name @humanized_field_name end |
#if_clause ⇒ Object
17 18 19 |
# File 'lib/dm-validations/validators/generic_validator.rb', line 17 def if_clause @if_clause end |
#options ⇒ Object (readonly)
18 19 20 |
# File 'lib/dm-validations/validators/generic_validator.rb', line 18 def @options end |
#unless_clause ⇒ Object
17 18 19 |
# File 'lib/dm-validations/validators/generic_validator.rb', line 17 def unless_clause @unless_clause end |
Instance Method Details
#==(other) ⇒ Object
Returns true if validators are equal
Note that this intentionally do validate options equality
even though it is hard to imagine a situation when multiple validations will be used on the same field with the same conditions but different options, it happens to be the case every once in a while with inferred validations for strings/text and explicitly given validations with different option (usually as Range vs. max limit for inferred validation)
134 135 136 137 138 139 140 |
# File 'lib/dm-validations/validators/generic_validator.rb', line 134 def ==(other) self.class == other.class && self.field_name == other.field_name && 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???
49 50 51 |
# File 'lib/dm-validations/validators/generic_validator.rb', line 49 def add_error(target, , field_name = :general) target.errors.add(field_name, ) end |
#allow_blank?(value) ⇒ Boolean
Test if the value is blank and is allowed
115 116 117 |
# File 'lib/dm-validations/validators/generic_validator.rb', line 115 def allow_blank?(value) @options[:allow_blank] if value.blank? end |
#allow_nil?(value) ⇒ Boolean
Test if the value is nil and is allowed
107 108 109 |
# File 'lib/dm-validations/validators/generic_validator.rb', line 107 def allow_nil?(value) @options[:allow_nil] if value.nil? 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.
59 60 61 |
# File 'lib/dm-validations/validators/generic_validator.rb', line 59 def call(target) raise NotImplementedError, "#{self.class}#call must be implemented" 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 |
# File 'lib/dm-validations/validators/generic_validator.rb', line 69 def execute?(target) if unless_clause = self.unless_clause return !target.__send__(unless_clause) if unless_clause.kind_of?(Symbol) return !unless_clause.call(target) if unless_clause.respond_to?(:call) end if if_clause = self.if_clause return target.__send__(if_clause) if if_clause.kind_of?(Symbol) return if_clause.call(target) if if_clause.respond_to?(:call) end true end |
#inspect ⇒ Object Also known as: to_s
142 143 144 |
# File 'lib/dm-validations/validators/generic_validator.rb', line 142 def inspect "<##{self.class.name} @field_name='#{@field_name}' @if_clause=#{@if_clause.inspect} @unless_clause=#{@unless_clause.inspect} @options=#{@options.inspect}>" end |
#optional?(value) ⇒ Boolean
Test the value to see if it is blank or nil, and if it is allowed
97 98 99 100 101 |
# File 'lib/dm-validations/validators/generic_validator.rb', line 97 def optional?(value) return allow_nil?(value) if value.nil? return allow_blank?(value) if value.blank? false end |
#set_optional_by_default(default = true) ⇒ undefined
Set the default value for allow_nil and allow_blank
87 88 89 90 91 |
# File 'lib/dm-validations/validators/generic_validator.rb', line 87 def set_optional_by_default(default = true) [ :allow_nil, :allow_blank ].each do |key| @options[key] = true unless .key?(key) end end |