Class: Tabled

Inherits:
Object
  • Object
show all
Includes:
BaseFileBuilder
Defined in:
lib/tabled.rb,
lib/helpers.rb,
lib/template.rb,
lib/content_shaper.rb,
lib/parsers/csv_parser.rb,
lib/parsers/json_parser.rb

Defined Under Namespace

Classes: CSVParser, ContentShaper, FormatError, Helpers, JSONParser, Template

Constant Summary collapse

DEFAULT_OPTIONS =
{ framed: true, row_separator: '-', titles: [] }.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from BaseFileBuilder

#export_to_file

Constructor Details

#initialize(data, **options) ⇒ Tabled

Returns a new instance of Tabled.



15
16
17
18
19
20
# File 'lib/tabled.rb', line 15

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

Instance Attribute Details

#columns_widthObject

Returns the value of attribute columns_width.



13
14
15
# File 'lib/tabled.rb', line 13

def columns_width
  @columns_width
end

#contentObject

Returns the value of attribute content.



13
14
15
# File 'lib/tabled.rb', line 13

def content
  @content
end

#dataObject

Returns the value of attribute data.



13
14
15
# File 'lib/tabled.rb', line 13

def data
  @data
end

#data_without_optionsObject

Returns the value of attribute data_without_options.



13
14
15
# File 'lib/tabled.rb', line 13

def data_without_options
  @data_without_options
end

#optionsObject

Returns the value of attribute options.



13
14
15
# File 'lib/tabled.rb', line 13

def options
  @options
end

Class Method Details

.from_csv(csv:, **options) ⇒ Object



26
27
28
29
30
# File 'lib/tabled.rb', line 26

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

Instance Method Details



22
23
24
# File 'lib/tabled.rb', line 22

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