Sha256: 6509484061ab7681244a90359126fd90a65807c8e50ce79a2ffe0ae8e89eeaaa

Contents?: true

Size: 1.51 KB

Versions: 14

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

14 entries across 14 versions & 1 rubygems

Version Path
rocketjob-5.1.1 lib/rocket_job/batch/tabular.rb
rocketjob-5.1.0 lib/rocket_job/batch/tabular.rb
rocketjob-5.0.0 lib/rocket_job/batch/tabular.rb
rocketjob-5.0.0.rc1 lib/rocket_job/batch/tabular.rb
rocketjob-5.0.0.beta4 lib/rocket_job/batch/tabular.rb
rocketjob-5.0.0.beta3 lib/rocket_job/batch/tabular.rb
rocketjob-5.0.0.beta2 lib/rocket_job/batch/tabular.rb
rocketjob-5.0.0.beta lib/rocket_job/batch/tabular.rb
rocketjob-4.3.0.beta2 lib/rocket_job/batch/tabular.rb
rocketjob-4.3.0.beta lib/rocket_job/batch/tabular.rb
rocketjob-4.2.0 lib/rocket_job/batch/tabular.rb
rocketjob-4.1.1 lib/rocket_job/batch/tabular.rb
rocketjob-4.1.0 lib/rocket_job/batch/tabular.rb
rocketjob-4.0.0 lib/rocket_job/batch/tabular.rb