Sha256: 3b377e118f6380560e8645dbfe13f394be75cc84f414681c9fc5f5ec73317720

Contents?: true

Size: 1.51 KB

Versions: 12

Compression:

Stored size: 1.51 KB

Contents

module RocketJob
  module Batch
    # Format output results.
    #
    # Takes Batch::Results, Batch::Result, Hash, Array, or String and renders it for output.
    #
    # Example:
    #
    # tabular = Tabular.new(
    #   main:       IOStreams::Tabular.new(columns: main_file_headers, format: tabular_output_format),
    #   exceptions: IOStreams::Tabular.new(columns: exception_file_headers, format: tabular_output_format)
    # )
    #
    # tabular.render(row)
    class Tabular
      autoload :Input, "rocket_job/batch/tabular/input"
      autoload :Output, "rocket_job/batch/tabular/output"

      def initialize(map)
        @map = map
      end

      def [](category = :main)
        @map[category] || raise("No tabular map defined for category: #{category.inspect}")
      end

      # Iterate over responses and format using Tabular
      def render(row, category = :main)
        if row.is_a?(Batch::Results)
          results = Batch::Results.new
          row.each { |result| results << render(result) }
          results
        elsif row.is_a?(Batch::Result)
          row.value = self[row.category].render(row.value)
          row
        elsif row.blank?
          nil
        else
          self[category].render(row)
        end
      end

      def render_header(category = :main)
        self[category].render_header
      end

      def requires_header?(category = :main)
        self[category].requires_header?
      end

      def header?(category = :main)
        self[category].header?
      end
    end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
rocketjob-5.4.1 lib/rocket_job/batch/tabular.rb
rocketjob-5.4.0 lib/rocket_job/batch/tabular.rb
rocketjob-5.4.0.beta2 lib/rocket_job/batch/tabular.rb
rocketjob-5.4.0.beta1 lib/rocket_job/batch/tabular.rb
rocketjob-5.3.3 lib/rocket_job/batch/tabular.rb
rocketjob-5.3.2 lib/rocket_job/batch/tabular.rb
rocketjob-5.3.1 lib/rocket_job/batch/tabular.rb
rocketjob-5.3.0 lib/rocket_job/batch/tabular.rb
rocketjob-5.2.0 lib/rocket_job/batch/tabular.rb
rocketjob-5.2.0.beta3 lib/rocket_job/batch/tabular.rb
rocketjob-5.2.0.beta2 lib/rocket_job/batch/tabular.rb
rocketjob-5.2.0.beta1 lib/rocket_job/batch/tabular.rb