Class: CouchRest::Validation::LengthValidator
- Inherits:
-
GenericValidator
- Object
- GenericValidator
- CouchRest::Validation::LengthValidator
- Defined in:
- lib/couchrest/validation/validators/length_validator.rb
Overview
Instance Attribute Summary
Attributes inherited from GenericValidator
#field_name, #if_clause, #unless_clause
Instance Method Summary collapse
- #call(target) ⇒ Object
-
#initialize(field_name, options) ⇒ LengthValidator
constructor
A new instance of LengthValidator.
Methods inherited from GenericValidator
Constructor Details
#initialize(field_name, options) ⇒ LengthValidator
Returns a new instance of LengthValidator.
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/couchrest/validation/validators/length_validator.rb', line 33 def initialize(field_name, ) super @field_name = field_name @options = @min = [:minimum] || [:min] @max = [:maximum] || [:max] @equal = [:is] || [:equals] @range = [:within] || [:in] @validation_method ||= :range if @range @validation_method ||= :min if @min && @max.nil? @validation_method ||= :max if @max && @min.nil? @validation_method ||= :equals unless @equal.nil? end |
Instance Method Details
#call(target) ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/couchrest/validation/validators/length_validator.rb', line 49 def call(target) field_value = target.validation_property_value(field_name) return true if @options[:allow_nil] && field_value.nil? field_value = '' if field_value.nil? # XXX: HACK seems hacky to do this on every validation, probably should # do this elsewhere? field = Extlib::Inflection.humanize(field_name) min = @range ? @range.min : @min max = @range ? @range.max : @max equal = @equal case @validation_method when :range then unless valid = @range.include?(field_value.size) = ValidationErrors.(:length_between, field, min, max) end when :min then unless valid = field_value.size >= min = ValidationErrors.(:too_short, field, min) end when :max then unless valid = field_value.size <= max = ValidationErrors.(:too_long, field, max) end when :equals then unless valid = field_value.size == equal = ValidationErrors.(:wrong_length, field, equal) end end = @options[:message] || add_error(target, , field_name) unless valid return valid end |