Class: MaxValidationRule

Inherits:
ValidationRule show all
Defined in:
lib/validation_profiler/rules/rules.rb

Instance Method Summary collapse

Methods inherited from ValidationRule

#get_field_value, #is_required?

Instance Method Details

#error_message(field, attributes) ⇒ Object



184
185
186
187
188
189
190
191
192
193
# File 'lib/validation_profiler/rules/rules.rb', line 184

def error_message(field, attributes)
  #check if a custom error message has been specified in the attributes
  if attributes[:message] == nil
    #no custom error message has been specified so create the default message.
    max = attributes[:value]
    "#{field} must not have a value greater than #{max}"
  else
    attributes[:message]
  end
end

#validate(obj, field, attributes) ⇒ Object



195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
# File 'lib/validation_profiler/rules/rules.rb', line 195

def validate(obj, field, attributes)

  max = attributes[:value]

  #verify the expected attributes have been specified.
  if max == nil
    raise InvalidRuleAttributes.new(MinValidationRule, field)
  end

  #attempt to get the field value from the object
  field_value = get_field_value(obj, field)

  if !is_required?(field_value, attributes)
    return true
  end

  if field_value.is_a?(DateTime) || field_value.is_a?(Numeric)
    field_value <= max
  else
    raise InvalidFieldType.new(MaxValidationRule, field)
  end

end