Class: ValidationRule

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

Instance Method Summary collapse

Instance Method Details

#get_field_value(obj, field) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/validation_profiler/rules/rules.rb', line 47

def get_field_value(obj, field)
  #attempt to get the field value from the object
  field_value = nil
  #check if the object is a hash
  if obj.is_a?(Hash)
    if obj.has_key?(field)
      #get the field value
      field_value = obj[field]
    elsif obj.has_key?(field.to_s)
      field_value = obj[field.to_s]
    end
  else
    #if the object does not contain the specified field raise an exception
    if !obj.respond_to?(field)
      raise FieldNotFound.new(field)
    end

    #get the field value
    field_value = obj.send(field)
  end
end

#is_required?(field_value, attributes = {}) ⇒ Boolean

Returns:

  • (Boolean)


69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/validation_profiler/rules/rules.rb', line 69

def is_required?(field_value, attributes = {})
  required = attributes[:required]
  if required == nil
    required = true
  end

  #check if the field is required
  if (field_value == nil || ((field_value.is_a?(String) || field_value.is_a?(Array)) && field_value.empty?)) && required == false
    return false
  else
    return true
  end
end