Class: DataMapper::Validations::WithinValidator
- Inherits:
-
GenericValidator
- Object
- GenericValidator
- DataMapper::Validations::WithinValidator
- Defined in:
- lib/dm-validations/validators/within_validator.rb
Overview
Instance Attribute Summary
Attributes inherited from GenericValidator
#field_name, #humanized_field_name, #if_clause, #options, #unless_clause
Instance Method Summary collapse
- #call(target) ⇒ Object
-
#initialize(field_name, options = {}) ⇒ WithinValidator
constructor
A new instance of WithinValidator.
Methods inherited from GenericValidator
#==, #add_error, #evaluate_conditional_clause, #execute?, #inspect, #optional?, #set_optional_by_default
Constructor Details
#initialize(field_name, options = {}) ⇒ WithinValidator
Returns a new instance of WithinValidator.
7 8 9 10 11 |
# File 'lib/dm-validations/validators/within_validator.rb', line 7 def initialize(field_name, ={}) super @options[:set] = [] unless @options.has_key?(:set) end |
Instance Method Details
#call(target) ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/dm-validations/validators/within_validator.rb', line 13 def call(target) value = target.validation_property_value(field_name) return true if optional?(value) return true if @options[:set].include?(value) n = 1.0/0 set = @options[:set] msg = @options[:message] if set.is_a?(Range) if set.first != -n && set.last != n = msg || ValidationErrors.(:value_between, field_name, set.first, set.last) elsif set.first == -n = msg || ValidationErrors.(:less_than_or_equal_to, field_name, set.last) elsif set.last == n = msg || ValidationErrors.(:greater_than_or_equal_to, field_name, set.first) end else = msg || ValidationErrors.(:inclusion, field_name, set.to_a.join(', ')) end add_error(target, , field_name) false end |