module RocketJob::Batch::Categories

Public Instance Methods

input_category(category_name = :main) click to toggle source
# File lib/rocket_job/batch/categories.rb, line 74
def input_category(category_name = :main)
  return category_name if category_name.is_a?(Category::Input)
  raise(ArgumentError, "Cannot supply Output Category to input category") if category_name.is_a?(Category::Output)

  # Initialize categories when this method is called before initialization is complete
  rocketjob_categories_assign if input_categories.empty?

  category_name = category_name.to_sym
  # find does not work against this association
  input_categories.each { |category| return category if category.name == category_name }

  raise(
    ArgumentError,
    "Unknown Input Category: #{category_name.inspect}. Registered categories: #{input_categories.collect(&:name).join(',')}"
  )
end
input_category?(category_name) click to toggle source

Returns [true|false] whether the named category has already been defined

# File lib/rocket_job/batch/categories.rb, line 109
def input_category?(category_name)
  category_name = category_name.to_sym
  # .find does not work against this association
  input_categories.each { |catg| return true if catg.name == category_name }
  false
end
merge_input_categories(categories) click to toggle source
# File lib/rocket_job/batch/categories.rb, line 123
def merge_input_categories(categories)
  return if categories.blank?

  categories.each do |properties|
    category_name = (properties["name"] || properties[:name] || :main).to_sym
    category      = input_category(category_name)
    properties.each { |key, value| category.public_send("#{key}=".to_sym, value) }
  end
end
merge_output_categories(categories) click to toggle source
# File lib/rocket_job/batch/categories.rb, line 133
def merge_output_categories(categories)
  return if categories.blank?

  categories.each do |properties|
    category_name = (properties["name"] || properties[:name] || :main).to_sym
    category      = output_category(category_name)
    properties.each { |key, value| category.public_send("#{key}=".to_sym, value) }
  end
end
output_category(category_name = :main) click to toggle source
# File lib/rocket_job/batch/categories.rb, line 91
def output_category(category_name = :main)
  return category_name if category_name.is_a?(Category::Output)
  raise(ArgumentError, "Cannot supply Input Category to output category") if category_name.is_a?(Category::Input)

  # Initialize categories when this method is called before initialization is complete
  rocketjob_categories_assign if output_categories.empty? && self.class.defined_output_categories

  category_name = category_name.to_sym
  # .find does not work against this association
  output_categories.each { |category| return category if category.name == category_name }

  raise(
    ArgumentError,
    "Unknown Output Category: #{category_name.inspect}. Registered categories: #{output_categories.collect(&:name).join(',')}"
  )
end
output_category?(category_name) click to toggle source
# File lib/rocket_job/batch/categories.rb, line 116
def output_category?(category_name)
  category_name = category_name.to_sym
  # .find does not work against this association
  output_categories.each { |catg| return true if catg.name == category_name }
  false
end

Private Instance Methods

rocketjob_categories_assign() click to toggle source
# File lib/rocket_job/batch/categories.rb, line 145
def rocketjob_categories_assign
  # Input categories defaults to :main if none was set in the class
  if input_categories.empty?
    self.input_categories =
      if self.class.defined_input_categories
        self.class.defined_input_categories.deep_dup
      else
        [RocketJob::Category::Input.new]
      end
  end

  return if !output_categories.empty? || !self.class.defined_output_categories

  # Input categories defaults to nil if none was set in the class
  self.output_categories = self.class.defined_output_categories.deep_dup
end
rocketjob_categories_input_render() click to toggle source

Parse the input data before passing to the perform method

# File lib/rocket_job/batch/categories.rb, line 173
def rocketjob_categories_input_render
  return if @rocket_job_input.nil?

  @rocket_job_input = rocketjob_categories_input_render_row(@rocket_job_input)
end
rocketjob_categories_input_render_row(row) click to toggle source
# File lib/rocket_job/batch/categories.rb, line 179
def rocketjob_categories_input_render_row(row)
  return if row.nil?

  category = input_category
  return row if category.nil? || !category.tabular?
  return nil if row.blank?

  tabular = category.tabular

  # Return the row as-is if the required header has not yet been set.
  if tabular.header?
    raise(ArgumentError,
          "The tabular header columns _must_ be set before attempting to parse data that requires it.")
  end

  tabular.record_parse(row)
end
rocketjob_categories_migrate() click to toggle source

Migrate existing v5 batch jobs to v6

# File lib/rocket_job/batch/categories.rb, line 220
def rocketjob_categories_migrate
  return unless attribute_present?(:input_categories) && self[:input_categories]&.first.is_a?(Symbol)

  serializer = :none
  if attribute_present?(:compress)
    serializer = :compress if self[:compress]
    remove_attribute(:compress)
  end

  if attribute_present?(:encrypt)
    serializer = :encrypt if self[:encrypt]
    remove_attribute(:encrypt)
  end

  slice_size = 100
  if attribute_present?(:slice_size)
    slice_size = self[:slice_size].to_i
    remove_attribute(:slice_size)
  end

  main_input_format  = nil
  main_input_mode    = :line
  main_input_columns = nil
  # Only migrate tabular attributes if the job also removed the tabular plugin.
  unless respond_to?(:tabular_input_render)
    if attribute_present?(:tabular_input_format)
      main_input_format = self[:tabular_input_format]
      remove_attribute(:tabular_input_format)
    end

    if attribute_present?(:tabular_input_mode)
      main_input_mode = self[:tabular_input_mode]
      remove_attribute(:tabular_input_mode)
    end

    if attribute_present?(:tabular_input_header)
      main_input_columns = self[:tabular_input_header]
      remove_attribute(:tabular_input_header)
    end
  end

  file_name = nil
  if attribute_present?(:upload_file_name)
    file_name = self[:upload_file_name]
    remove_attribute(:upload_file_name)
  end

  existing                = self[:input_categories]
  self[:input_categories] = []
  self[:input_categories] = existing.collect do |category_name|
    RocketJob::Category::Input.new(
      name:       category_name,
      file_name:  file_name,
      serializer: serializer,
      slice_size: slice_size,
      format:     [:main, "main"].include?(category_name) ? main_input_format : nil,
      columns:    [:main, "main"].include?(category_name) ? main_input_columns : nil,
      mode:       [:main, "main"].include?(category_name) ? main_input_mode : nil
    ).as_document
  end

  collect_output = false
  if attribute_present?(:collect_output)
    collect_output = self[:collect_output]
    remove_attribute(:collect_output)
  end

  collect_nil_output = true
  if attribute_present?(:collect_nil_output)
    collect_nil_output = self[:collect_nil_output]
    remove_attribute(:collect_nil_output)
  end

  main_output_format  = nil
  main_output_columns = nil
  main_output_options = nil

  # Only migrate tabular attributes if the job also removed the tabular plugin.
  unless respond_to?(:tabular_output_render)
    if attribute_present?(:tabular_output_format)
      main_output_format = self[:tabular_output_format]
      remove_attribute(:tabular_output_format)
    end

    if attribute_present?(:tabular_output_header)
      main_output_columns = self[:tabular_output_header]
      remove_attribute(:tabular_output_header)
    end

    if attribute_present?(:tabular_output_options)
      main_output_options = self[:tabular_output_options]
      remove_attribute(:tabular_output_options)
    end
  end

  existing                 = self[:output_categories]
  self[:output_categories] = []
  if collect_output
    if existing.blank?
      self[:output_categories] = [
        RocketJob::Category::Output.new(
          nils:           collect_nil_output,
          format:         main_output_format,
          columns:        main_output_columns,
          format_options: main_output_options
        ).as_document
      ]
    elsif existing.first.is_a?(Symbol)
      self[:output_categories] = existing.collect do |category_name|
        RocketJob::Category::Output.new(
          name:           category_name,
          serializer:     serializer,
          nils:           collect_nil_output,
          format:         [:main, "main"].include?(category_name) ? main_output_format : nil,
          columns:        [:main, "main"].include?(category_name) ? main_output_columns : nil,
          format_options: [:main, "main"].include?(category_name) ? main_output_options : nil
        ).as_document
      end
    end
  end
end
rocketjob_categories_output_render() click to toggle source

Render the output from the perform.

# File lib/rocket_job/batch/categories.rb, line 163
def rocketjob_categories_output_render
  return if @rocket_job_output.nil?

  return unless output_categories
  return if output_categories.empty?

  @rocket_job_output = rocketjob_categories_output_render_row(@rocket_job_output)
end
rocketjob_categories_output_render_row(row) click to toggle source
# File lib/rocket_job/batch/categories.rb, line 197
def rocketjob_categories_output_render_row(row)
  return if row.nil?

  if row.is_a?(Batch::Result)
    category  = output_category(row.category)
    row.value = category.tabular.render(row.value) if category.tabular?
    return row
  end

  if row.is_a?(Batch::Results)
    results = Batch::Results.new
    row.each { |result| results << rocketjob_categories_output_render_row(result) }
    return results
  end

  category = output_category
  return row unless category.tabular?
  return nil if row.blank?

  category.tabular.render(row)
end