Sha256: 8f0d7ea45d55063a77f2f09c011e3dca94423d160851ff161adc07fddd5ea51c

Contents?: true

Size: 1.2 KB

Versions: 1

Compression:

Stored size: 1.2 KB

Contents

# frozen_string_literal: true

require 'eac_ruby_utils/core_ext'
require 'eac_ruby_utils/acts_as_abstract'

module EacCli
  module RunnerWith
    module OutputList
      class BaseFormatter
        acts_as_abstract :to_output
        common_constructor :columns, :rows

        # @return [String]
        def build_column(column)
          column.to_s
        end

        # @return [Array<String>]
        def build_columns
          columns.map(&:to_s)
        end

        # @param row [Object]
        # @return [Object]
        def build_row(_row)
          raise_abstract_method __method__
        end

        # @return [Array<Hash<String, String>>]
        def build_rows
          rows.map { |row| build_row(row) }
        end

        # @param row [Object]
        # @param column [String]
        # @return [Object]
        def build_value(row, column)
          return row.send(column) if row.respond_to?(column)
          return row.with_indifferent_access.fetch(column) if row.is_a?(::Hash)

          raise ::ArgumentError, "Row \"#{row}\" do not respond to column \"#{column}\" neither " \
                                 "is a Hash (Row: #{row}, Column: #{column})"
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
eac_cli-0.42.0 lib/eac_cli/runner_with/output_list/base_formatter.rb