Class: FixedWidthFileValidator::FieldValidator

Inherits:
Object
  • Object
show all
Defined in:
lib/fixed_width_file_validator/validator.rb

Overview

rubocop:disable Style/ClassVars

Constant Summary collapse

@@token_cache =
{}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(field_name, pos, width, validations = nil) ⇒ FieldValidator

Returns a new instance of FieldValidator.



25
26
27
28
29
30
31
# File 'lib/fixed_width_file_validator/validator.rb', line 25

def initialize(field_name, pos, width, validations = nil)
  self.field_name = field_name
  self.non_unique_values = []
  self.validations = validations
  self.pos = pos
  self.width = width
end

Instance Attribute Details

#field_nameObject

Returns the value of attribute field_name.



21
22
23
# File 'lib/fixed_width_file_validator/validator.rb', line 21

def field_name
  @field_name
end

#non_unique_valuesObject

Returns the value of attribute non_unique_values.



21
22
23
# File 'lib/fixed_width_file_validator/validator.rb', line 21

def non_unique_values
  @non_unique_values
end

#posObject

Returns the value of attribute pos.



21
22
23
# File 'lib/fixed_width_file_validator/validator.rb', line 21

def pos
  @pos
end

#validationsObject

Returns the value of attribute validations.



21
22
23
# File 'lib/fixed_width_file_validator/validator.rb', line 21

def validations
  @validations
end

#widthObject

Returns the value of attribute width.



21
22
23
# File 'lib/fixed_width_file_validator/validator.rb', line 21

def width
  @width
end

Instance Method Details

#validate(record, field_name, bindings = {}) ⇒ Object

return an array of error objects empty array if all validation passes



35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/fixed_width_file_validator/validator.rb', line 35

def validate(record, field_name, bindings = {})
  if validations
    validations.collect do |validation|
      unless valid_value?(validation, record, field_name, bindings)
        FieldValidationError.new(validation, record, field_name, pos, width)
      end
    end.compact
  elsif record && record[field_name]
    # when no validation rules exist for the field, just check if the field exists in the record
    []
  else
    raise "found field value nil in #{record} field #{field_name}, shouldn't be possible?"
  end
end