Class: CF::ResultValidator

Inherits:
Object
  • Object
show all
Includes:
ValidatorHelpers
Defined in:
lib/cf/result_validator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeResultValidator

Returns a new instance of ResultValidator.



6
7
8
# File 'lib/cf/result_validator.rb', line 6

def initialize
  @errors = []
end

Instance Attribute Details

#errorsObject

Returns the value of attribute errors.



4
5
6
# File 'lib/cf/result_validator.rb', line 4

def errors
  @errors
end

Instance Method Details

#valid?(rules, result) ⇒ Boolean

Returns:

  • (Boolean)


10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/cf/result_validator.rb', line 10

def valid?(rules, result)
  csv_contents = FasterCSV.parse(result)
  csv_header = csv_contents[0].map{|a| a.strip unless a.nil? }

  rule_labels = Array.new
  rules.each do |rule|
    rule_labels << rule[:label]
  end
  
  bool = false
  case (rule_labels <=> csv_header)
  when 1
    # when input header is less than rule label
    @errors << "#{rule_labels - csv_header} is/are required"
  when -1
    # when input header is more than rule label
    @errors << "cannot process as the input data consists extra headers"
  when 0
    # if rule_labels == csv_header
    csv_contents[1..-1].each do |item|
      rules.each_with_index do |header, index|
        next unless item
        unless item.compact.empty?
          item[index].strip! if !item[index].nil?
          if required(header[:required], item[index]) and valid(header[:validation_format], item[index])
            bool = true
          else
            bool = false
            if header[:required] && item[index].blank?
              @errors << "#{header[:label]} is required"
            else
              @errors << "#{item[index]} is not valid"
            end
          end
        end
      end
    end
  end
  # finally return boolean for .valid?
  return bool
end