Class: Tablesmith::Table

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

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



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

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

Instance Method Details

#apply_column_aliases(column_names) ⇒ Object



120
121
122
123
124
125
126
# File 'lib/tablesmith/table.rb', line 120

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



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

def column_order
  []
end

#columnsObject



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

def columns
  @columns
end

#convert_item_to_hash_row(item) ⇒ Object

override in subclass or mixin



77
78
79
# File 'lib/tablesmith/table.rb', line 77

def convert_item_to_hash_row(item)
  item
end

#create_headers(rows) ⇒ Object



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

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

#grouped_headers(column_names) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/tablesmith/table.rb', line 100

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



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

def inspect
  pretty_inspect
end

#normalize_keys(rows) ⇒ Object

override in subclass or mixin



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

def normalize_keys(rows)
end

#pretty_inspectObject

Pry 0.9 calls this



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

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



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

def pretty_print(pp)
  pp.text pretty_inspect
end

#respond_to_missing?Boolean

Returns:

  • (Boolean)


15
16
17
# File 'lib/tablesmith/table.rb', line 15

def respond_to_missing?
  super
end

#row_values(row) ⇒ Object

override in subclass or mixin



68
69
70
# File 'lib/tablesmith/table.rb', line 68

def row_values(row)
  row
end

#sort_columns(rows) ⇒ Object

override in subclass or mixin



73
74
# File 'lib/tablesmith/table.rb', line 73

def sort_columns(rows)
end

#text_tableObject



34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/tablesmith/table.rb', line 34

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

  rows = self.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



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/tablesmith/table.rb', line 47

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



63
64
65
# File 'lib/tablesmith/table.rb', line 63

def to_html
  HtmlFormatter.new(self).to_html
end