Class: Tablesmith::Table

Inherits:
Array
  • Object
show all
Defined in:
lib/tablesmith/table.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Array

#to_table

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth_id, *args) ⇒ Object



8
9
10
11
12
13
14
15
# File 'lib/tablesmith/table.rb', line 8

def method_missing(meth_id, *args)
  count = 1
  map do |t|
    $stderr.print '.' if (count.divmod(100)[1]).zero?
    count += 1
    t.send(meth_id, *args)
  end
end

Instance Attribute Details

#columnsObject (readonly)

Returns the value of attribute columns.



95
96
97
# File 'lib/tablesmith/table.rb', line 95

def columns
  @columns
end

Instance Method Details

#apply_column_aliases(column_names) ⇒ Object



128
129
130
131
132
133
134
# File 'lib/tablesmith/table.rb', line 128

def apply_column_aliases(column_names)
  column_names.map do |name|
    instance = columns.detect { |ca| ca.name.to_s == name.to_s }
    value = instance ? instance.display_name : name
    { value: value, align: :center }
  end
end

#column_orderObject

override in subclass or mixin



91
92
93
# File 'lib/tablesmith/table.rb', line 91

def column_order
  []
end

#convert_item_to_hash_row(item) ⇒ Object

override in subclass or mixin



83
84
85
# File 'lib/tablesmith/table.rb', line 83

def convert_item_to_hash_row(item)
  item
end

#create_headers(rows) ⇒ Object



97
98
99
100
101
102
103
104
105
106
# File 'lib/tablesmith/table.rb', line 97

def create_headers(rows)
  first_element = rows.first
  if first_element.is_a?(Array)
    top_row = first_element
    column_names = top_row.first.is_a?(Array) ? top_row.map(&:first) : top_row
    grouped_headers(column_names) + [apply_column_aliases(column_names), :separator]
  else
    []
  end
end

#grouped_headers(column_names) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/tablesmith/table.rb', line 108

def grouped_headers(column_names)
  groups = Hash.new { |h, k| h[k] = 0 }
  column_names.map! do |name|
    group, col = name.to_s.split(/\./)
    col, group = [group, ''] if col.nil?
    groups[group] += 1
    col
  end
  if groups.keys.length == 1 # TODO: add option to show group header row when only one exists
    []
  else
    row = []
    # this relies on Ruby versions where hash retains add order
    groups.each_pair do |name, span|
      row << { value: name, align: :center, colspan: span }
    end
    [row, :separator]
  end
end

#inspectObject

irb



26
27
28
# File 'lib/tablesmith/table.rb', line 26

def inspect
  pretty_inspect
end

#normalize_keys(rows) ⇒ Object

override in subclass or mixin



88
# File 'lib/tablesmith/table.rb', line 88

def normalize_keys(rows); end

#pretty_inspectObject

Pry 0.9 calls this



31
32
33
# File 'lib/tablesmith/table.rb', line 31

def pretty_inspect
  text_table.to_s
end

#pretty_print(pp) ⇒ Object

Pry 0.10 eventually uses PP, and this is the PP way to provide custom output



36
37
38
# File 'lib/tablesmith/table.rb', line 36

def pretty_print(pp)
  pp.text pretty_inspect
end

#respond_to_missing?Boolean

Returns:

  • (Boolean)


17
18
19
# File 'lib/tablesmith/table.rb', line 17

def respond_to_missing?
  super
end

#row_values(row) ⇒ Object

override in subclass or mixin



75
76
77
# File 'lib/tablesmith/table.rb', line 75

def row_values(row)
  row
end

#sort_columns(rows) ⇒ Object

override in subclass or mixin



80
# File 'lib/tablesmith/table.rb', line 80

def sort_columns(rows); end

#text_tableObject



40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/tablesmith/table.rb', line 40

def text_table
  return ['(empty)'].to_text_table if empty?

  rows = map { |item| convert_item_to_hash_row(item) }.compact

  normalize_keys(rows)

  sort_columns(rows)

  rows = create_headers(rows) + (rows.map { |row| row_values(row) })
  rows.to_text_table
end

#to_csvObject



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/tablesmith/table.rb', line 53

def to_csv
  CSV.generate do |csv|
    text_table.rows.each do |row|
      next if row == :separator

      csv << row.map do |cell|
        case cell
        when Hash
          cell[:value]
        else
          cell
        end
      end
    end
  end
end

#to_htmlObject



70
71
72
# File 'lib/tablesmith/table.rb', line 70

def to_html
  HtmlFormatter.new(self).to_html
end

#to_sObject



21
22
23
# File 'lib/tablesmith/table.rb', line 21

def to_s
  text_table.to_s
end