Class: Tablesmith::Table

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(array = []) ⇒ Table

Returns a new instance of Table.



8
9
10
11
# File 'lib/tablesmith/table.rb', line 8

def initialize(array = [])
  super(array)
  @array = array
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth_id, *args) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/tablesmith/table.rb', line 13

def method_missing(meth_id, *args)
  # In order to support `Kernel::puts` of a `Table`, we need to ignore
  # `to_ary` calls here as well. See comments on `delegated_array_class`.
  #
  # While `DelegatorClass(Array)` proactively defines methods on `Table`
  # that come from `Array`, it _also_ will pass calls through method_missing
  # to the target object if it says it will respond to it.
  #
  # It seems a little redundant, but it is what it is, and so we must also
  # cut off calls to `to_ary` in both places.
  return nil if meth_id.to_sym == :to_ary

  super
end

Instance Attribute Details

#columnsObject (readonly)

Returns the value of attribute columns.



108
109
110
# File 'lib/tablesmith/table.rb', line 108

def columns
  @columns
end

Instance Method Details

#apply_column_aliases(column_names) ⇒ Object



139
140
141
142
143
144
145
# File 'lib/tablesmith/table.rb', line 139

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



104
105
106
# File 'lib/tablesmith/table.rb', line 104

def column_order
  []
end

#convert_item_to_hash_row(item) ⇒ Object

override in subclass or mixin



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

def convert_item_to_hash_row(item)
  item
end

#create_headers(rows) ⇒ Object



110
111
112
113
114
115
116
117
118
119
# File 'lib/tablesmith/table.rb', line 110

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



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/tablesmith/table.rb', line 121

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 { |name, span| row << {value: name, align: :center, colspan: span} }
    [row, :separator]
  end
end

#inspectObject

irb



39
40
41
# File 'lib/tablesmith/table.rb', line 39

def inspect
  pretty_inspect
end

#normalize_keys(rows) ⇒ Object

override in subclass or mixin



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

def normalize_keys(rows); end

#pretty_inspectObject

Pry 0.9 calls this



44
45
46
# File 'lib/tablesmith/table.rb', line 44

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



49
50
51
# File 'lib/tablesmith/table.rb', line 49

def pretty_print(pp)
  pp.text pretty_inspect
end

#respond_to_missing?(meth_id, _include_all) ⇒ Boolean

Returns:

  • (Boolean)


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

def respond_to_missing?(meth_id, _include_all)
  return false if meth_id.to_sym == :to_ary

  super
end

#row_values(row) ⇒ Object

override in subclass or mixin



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

def row_values(row)
  row
end

#sort_columns(rows) ⇒ Object

override in subclass or mixin



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

def sort_columns(rows); end

#text_tableObject



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

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



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/tablesmith/table.rb', line 66

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



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

def to_html
  HtmlFormatter.new(self).to_html
end

#to_sObject



34
35
36
# File 'lib/tablesmith/table.rb', line 34

def to_s
  text_table.to_s
end