Class: FixedWidthFileValidator::FileFormat

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(record_type, config_file = nil) ⇒ FileFormat

Returns a new instance of FileFormat.



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/fixed_width_file_validator/file_format.rb', line 19

def initialize(record_type, config_file = nil)
  @record_type = record_type.to_sym
  @fields = {}
  @unique_fields = []
  @parser_field_list = []
  @formatter_field_list = []
  @file_settings = {}
  @column = 1
  @config_file = config_file

  if @config_file.is_a?(String)
    File.open(@config_file) do |f|
      @raw_config = symbolize(YAML.safe_load(f, [], [], true))
    end
  else
    # config_file must be an IO object
    @raw_config = symbolize(YAML.safe_load(config_file, [], [], true))
  end

  load_config(@record_type)
  find_unique_fields
end

Instance Attribute Details

#fieldsObject (readonly)

Returns the value of attribute fields.



10
11
12
# File 'lib/fixed_width_file_validator/file_format.rb', line 10

def fields
  @fields
end

#file_settingsObject (readonly)

Returns the value of attribute file_settings.



10
11
12
# File 'lib/fixed_width_file_validator/file_format.rb', line 10

def file_settings
  @file_settings
end

#record_typeObject (readonly)

Returns the value of attribute record_type.



10
11
12
# File 'lib/fixed_width_file_validator/file_format.rb', line 10

def record_type
  @record_type
end

#unique_fieldsObject (readonly)

Returns the value of attribute unique_fields.



10
11
12
# File 'lib/fixed_width_file_validator/file_format.rb', line 10

def unique_fields
  @unique_fields
end

Class Method Details

.for(record_type, config_file = nil) ⇒ Object

not threadsafe



15
16
17
# File 'lib/fixed_width_file_validator/file_format.rb', line 15

def self.for(record_type, config_file = nil)
  @all_formats[record_type.to_sym] ||= FileFormat.new(record_type.to_sym, config_file)
end

Instance Method Details

#create_file_reader(data_file_path) ⇒ Object



46
47
48
# File 'lib/fixed_width_file_validator/file_format.rb', line 46

def create_file_reader(data_file_path)
  FixedWidthFileValidator::FileReader.new(data_file_path, record_parser, file_settings)
end

#create_record_validatorObject



50
51
52
# File 'lib/fixed_width_file_validator/file_format.rb', line 50

def create_record_validator
  FixedWidthFileValidator::RecordValidator.new fields
end

#create_record_validator_with_reader(data_file_path) ⇒ Object



54
55
56
57
58
59
60
61
# File 'lib/fixed_width_file_validator/file_format.rb', line 54

def create_record_validator_with_reader(data_file_path)
  # in this scenario the reader will read through the file to find unique values
  # so we need to create a new reader and close it when done
  reader = create_file_reader(data_file_path)
  FixedWidthFileValidator::RecordValidator.new fields, unique_fields, reader
ensure
  reader.close
end

#field_validations(field_name) ⇒ Object



42
43
44
# File 'lib/fixed_width_file_validator/file_format.rb', line 42

def field_validations(field_name)
  fields[field_name]&.fetch :validations
end

#record_formatterObject



63
64
65
# File 'lib/fixed_width_file_validator/file_format.rb', line 63

def record_formatter
  @record_formatter ||= RecordFormatter.new(@formatter_field_list, file_settings[:encoding])
end

#record_parserObject



67
68
69
# File 'lib/fixed_width_file_validator/file_format.rb', line 67

def record_parser
  @record_parser ||= RecordParser.new(@parser_field_list, file_settings[:encoding])
end