Module: RocketJob::Batch::Tabular::Input

Extended by:
ActiveSupport::Concern
Defined in:
lib/rocket_job/batch/tabular/input.rb

Overview

For the simple case where all ‘input_categories` have the same format, If multiple input categories are used with different formats, then use IOStreams::Tabular directly instead of this plugin.

Instance Method Summary collapse

Instance Method Details

#upload(file_name_or_io = nil, **args, &block) ⇒ Object

Extract the header line during the upload.

Overrides: RocketJob::Batch::IO#upload

Notes:

  • When supplying a block the header must be set manually



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/rocket_job/batch/tabular/input.rb', line 52

def upload(file_name_or_io = nil, **args, &block)
  if tabular_input_type == :text
    args[:encoding]       = 'UTF-8'
    args[:encode_cleaner] = :printable
    args[:encode_replace] = ''
  end

  # If an input header is not required, then we don't extract it'
  return super(file_name_or_io, stream_mode: tabular_input_mode, **args, &block) unless tabular_input.header?

  # If the header is already set then it is not expected in the file
  if tabular_input_header.present?
    tabular_input_cleanse_header
    return super(file_name_or_io, stream_mode: tabular_input_mode, **args, &block)
  end

  case tabular_input_mode
  when :line
    parse_header = -> (line) do
      tabular_input.parse_header(line)
      tabular_input_cleanse_header
      self.tabular_input_header = tabular_input.header.columns
    end
    super(file_name_or_io, on_first: parse_header, stream_mode: tabular_input_mode, **args, &block)
  when :row
    set_header = -> (row) do
      tabular_input.header.columns = row
      tabular_input_cleanse_header
      self.tabular_input_header = tabular_input.header.columns
    end
    super(file_name_or_io, on_first: set_header, stream_mode: tabular_input_mode, **args, &block)
  when :record
    super(file_name_or_io, stream_mode: tabular_input_mode, **args, &block)
  else
    raise(ArgumentError, "Invalid tabular_input_mode: #{stream_mode.inspect}")
  end
end