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



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

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
    raise ValidationProfiler::Exceptions::FieldNotFound, field unless obj.respond_to?(field)

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

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

Returns:

  • (Boolean)


25
26
27
28
29
30
31
32
# File 'lib/validation_profiler/rules/validation_rule.rb', line 25

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

  # check if the field is required
  return false if required_field?(field_value, required)
  true
end