Sha256: 422e04a706bcb947faf17e90990d4b7e6af24ecc9ec2f8c23f8028611bd7f612

Contents?: true

Size: 957 Bytes

Versions: 2

Compression:

Stored size: 957 Bytes

Contents

# frozen_string_literal: true

require_relative 'template'
require_relative 'helpers'
require_relative 'content_shaper'
require_relative 'file_builders/base_file_builder'
require_relative 'errors/format_error'

class Tabled
  include BaseFileBuilder

  DEFAULT_OPTIONS = { framed: true, row_separator: '-', titles: [] }.freeze
  attr_accessor :data, :data_without_options, :columns_width, :content, :options

  def initialize(data, **options)
    @options = DEFAULT_OPTIONS.merge(options)
    @data = Tabled::Helpers.convert_to_required_structure(data)
    @columns_width = Tabled::Helpers.calculate_columns_width(data: data, options: @options)
    @content = Tabled::ContentShaper.new(data, @columns_width, @options).shape
  end

  def print_to_console
    print content.join("\n")
  end

  def self.from_csv(csv:, **options)
    raise Tabled::FormatError.new(received_format: csv.class) unless csv.is_a? CSV::Table

    new(csv.to_a, **options)
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
tabled-1.2.1 lib/tabled.rb
tabled-1.2.0 lib/tabled.rb