Class: TablePrint::Printer

Inherits:
Object
  • Object
show all
Defined in:
lib/table_print.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data, options = {}) ⇒ Printer

Returns a new instance of Printer.



19
20
21
22
23
24
# File 'lib/table_print.rb', line 19

def initialize(data, options={})
  @data = Array(data).compact
  @options = options
  @columns = nil
  @start_time = Time.now
end

Class Method Details

.table_print(data, options = {}) ⇒ Object



14
15
16
17
# File 'lib/table_print.rb', line 14

def self.table_print(data, options={})
  p = new(data, options)
  p.table_print
end

Instance Method Details

#messageObject



52
53
54
55
56
57
58
# File 'lib/table_print.rb', line 52

def message
  return "Printed with config" if configged?
  (Time.now - @start_time).to_s
  # the message is used to initiate Returnable
  # whose argument is regarded in ruby 2.7.1 as string
  # (ruby 2.7.1 calls ".includes?" method on this argument)
end

#table_printObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/table_print.rb', line 26

def table_print
  return "No data." if @data.empty?

  # it's groups all the way down
  # make a top-level group to hold everything we're about to do
  group = TablePrint::RowGroup.new

  # parse the config and attach it to the group
  columns.each do |c|
    group.set_column(c)
  end

  # copy data from original objects into the group
  group_data = (@data.first.is_a?(Hash) || @data.first.is_a?(Struct)) ? [@data] : @data
  group_data.each do |data|
    group.add_children(Fingerprinter.new.lift(columns, data))
  end

  # munge the tree of data we created, to condense the output
  group.collapse!
  return "No data." if group.columns.empty?

  # turn everything into a string for output
  [group.header, group.horizontal_separator, group.format].join("\n")
end