Class: FixedWidthFileValidator::FieldValidator
- Inherits:
-
Object
- Object
- FixedWidthFileValidator::FieldValidator
- Defined in:
- lib/fixed_width_file_validator/validator.rb
Overview
rubocop:disable Style/ClassVars
Constant Summary collapse
- @@token_cache =
{}
Instance Attribute Summary collapse
-
#field_name ⇒ Object
Returns the value of attribute field_name.
-
#non_unique_values ⇒ Object
Returns the value of attribute non_unique_values.
-
#pos ⇒ Object
Returns the value of attribute pos.
-
#validations ⇒ Object
Returns the value of attribute validations.
-
#width ⇒ Object
Returns the value of attribute width.
Instance Method Summary collapse
-
#initialize(field_name, pos, width, validations = nil) ⇒ FieldValidator
constructor
A new instance of FieldValidator.
-
#validate(record, field_name, bindings = {}) ⇒ Object
return an array of error objects empty array if all validation passes.
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_name ⇒ Object
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_values ⇒ Object
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 |
#pos ⇒ Object
Returns the value of attribute pos.
21 22 23 |
# File 'lib/fixed_width_file_validator/validator.rb', line 21 def pos @pos end |
#validations ⇒ Object
Returns the value of attribute validations.
21 22 23 |
# File 'lib/fixed_width_file_validator/validator.rb', line 21 def validations @validations end |
#width ⇒ Object
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 |