Class: DataMapper::Validate::WithinValidator
- Inherits:
-
GenericValidator
- Object
- GenericValidator
- DataMapper::Validate::WithinValidator
- Defined in:
- lib/gems/dm-validations-0.9.9/lib/dm-validations/within_validator.rb
Overview
Instance Attribute Summary
Attributes inherited from GenericValidator
Instance Method Summary collapse
- #call(target) ⇒ Object
-
#initialize(field_name, options = {}) ⇒ WithinValidator
constructor
A new instance of WithinValidator.
- #n ⇒ Object
Methods inherited from GenericValidator
#==, #add_error, #execute?, #field_name
Constructor Details
#initialize(field_name, options = {}) ⇒ WithinValidator
Returns a new instance of WithinValidator.
10 11 12 13 14 |
# File 'lib/gems/dm-validations-0.9.9/lib/dm-validations/within_validator.rb', line 10 def initialize(field_name, ={}) super @field_name, @options = field_name, @options[:set] = [] unless @options.has_key?(:set) end |
Instance Method Details
#call(target) ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/gems/dm-validations-0.9.9/lib/dm-validations/within_validator.rb', line 16 def call(target) includes = @options[:set].include?(target.send(field_name)) return true if includes field_name = Extlib::Inflection.humanize(@field_name) if @options[:set].is_a?(Range) if @options[:set].first != -n && @options[:set].last != n = @options[:message] || "%s must be between %s and %s".t(field_name, @options[:set].first, @options[:set].last) elsif @options[:set].first == -n = @options[:message] || "%s must be less than %s".t(field_name, @options[:set].last) elsif @options[:set].last == n = @options[:message] || "%s must be greater than %s".t(field_name, @options[:set].first) end else = "%s must be one of [%s]".t(field_name, @options[:set].join(', ')) end add_error(target, , @field_name) return false end |
#n ⇒ Object
37 38 39 |
# File 'lib/gems/dm-validations-0.9.9/lib/dm-validations/within_validator.rb', line 37 def n 1.0/0 end |