Class: ValidationProfiler::Rules::ValidationRule

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

Instance Method Summary collapse

Instance Method Details

#get_field_value(obj, field) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/validation_profiler/rules/validation_rule.rb', line 5

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 ValidationProfiler::Exceptions::FieldNotFound.new(field)
    end

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

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

Returns:

  • (Boolean)


27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/validation_profiler/rules/validation_rule.rb', line 27

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 && required == false
    return false
  else
    return true
  end
end