Class: MinValidationRule

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



137
138
139
140
141
142
143
144
145
146
# File 'lib/validation_profiler/rules/rules.rb', line 137

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.
    min = attributes[:value]
    "#{field} must have a minimum value of #{min}"
  else
    attributes[:message]
  end
end

#validate(obj, field, attributes) ⇒ Object



148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/validation_profiler/rules/rules.rb', line 148

def validate(obj, field, attributes)

  min = attributes[:value]

  #verify the expected attributes have been specified.
  if min == 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 >= min
  else
    raise InvalidFieldType.new(MinValidationRule, field)
  end

end