Class: Tabular::Table

Inherits:
Object
  • Object
show all
Includes:
Blank, Keys, Tabular::Tables::FileReading, Zero
Defined in:
lib/tabular/table.rb

Overview

Simple Enumerable list of Hashes. Use Table.read(file_path) to read file. Can also create a Table with Table.new. Either pass in data or set options and then call row=.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Zero

#is_zero?

Methods included from Tabular::Tables::FileReading

#format_from, #read, #read_csv, #read_spreadsheet, #read_txt, #to_file_path

Methods included from Keys

#key_to_sym

Methods included from Blank

#is_blank?

Constructor Details

#initialize(rows = []) ⇒ Table

Pass data in as rows. Expects rows to be an Enumerable of Enumerables. Maps rows to Hash-like Tabular::Rows.



23
24
25
26
# File 'lib/tabular/table.rb', line 23

def initialize(rows = [])
  @columns = nil
  self.rows = rows
end

Instance Attribute Details

#column_mapperObject

Returns the value of attribute column_mapper.



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

def column_mapper
  @column_mapper
end

#row_mapperObject

Returns the value of attribute row_mapper.



12
13
14
# File 'lib/tabular/table.rb', line 12

def row_mapper
  @row_mapper
end

Class Method Details

.read(file, options = {}) ⇒ Object



15
16
17
18
19
# File 'lib/tabular/table.rb', line 15

def self.read(file, options = {})
  table = Table.new
  table.read file, options[:as], options[:sheet]
  table
end

Instance Method Details

#<<(row) ⇒ Object

Add row to end of table. Create missing columns and link the source row to Row#source. To control how source data is added to the Table, use Table#mapper= to set a class that implements map(row) and returns a Hash.



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/tabular/table.rb', line 49

def <<(row)
  cells = if row_mapper
            row_mapper.map(row)
          else
            row
          end

  if @columns.nil? || @columns.empty?
    @columns = Tabular::Columns.new(self, cells, column_mapper)
    return columns unless cells.respond_to?(:keys)
  end

  new_row = Tabular::Row.new(self, cells, row)
  new_row.each_key do |key|
    columns << key
  end
  rows << new_row
  new_row
end

#[](index) ⇒ Object

Return Row at zero-based index, or nil if Row is out of bounds



42
43
44
# File 'lib/tabular/table.rb', line 42

def [](index)
  rows[index]
end

#columnsObject

Instance of Tabular::Columns



74
75
76
# File 'lib/tabular/table.rb', line 74

def columns
  @columns ||= Tabular::Columns.new(self, [], column_mapper)
end

#delete_blank_columns!(*options) ⇒ Object

Remove all columns that only contain a blank string, zero, or nil



79
80
81
82
83
84
85
86
87
# File 'lib/tabular/table.rb', line 79

def delete_blank_columns!(*options)
  exceptions = extract_exceptions(options)

  (columns.map(&:key) - exceptions).each do |key|
    if rows.all? { |row| is_blank?(row[key]) || is_zero?(row[key]) } # rubocop:disable Style/IfUnlessModifier
      delete_column key
    end
  end
end

#delete_blank_rows!Object



101
102
103
104
105
106
# File 'lib/tabular/table.rb', line 101

def delete_blank_rows!
  @rows = rows.reject(&:blank?)
  rows.each.with_index do |row, index|
    row.index = index
  end
end

#delete_column(key) ⇒ Object



123
124
125
126
127
128
# File 'lib/tabular/table.rb', line 123

def delete_column(key)
  rows.each do |row|
    row.delete key
  end
  columns.delete key
end

#delete_homogenous_columns!(*options) ⇒ Object

Remove all columns that contain the same value in all rows



90
91
92
93
94
95
96
97
98
99
# File 'lib/tabular/table.rb', line 90

def delete_homogenous_columns!(*options)
  return if rows.size < 2

  exceptions = extract_exceptions(options)

  (columns.map(&:key) - exceptions).each do |key|
    value = rows.first[key]
    delete_column key if rows.all? { |row| row[key] == value }
  end
end

#inspectObject



69
70
71
# File 'lib/tabular/table.rb', line 69

def inspect
  rows.map { |row| row.join(",") }.join("\n")
end

#metadataObject

Last-resort storage for client code data



146
147
148
# File 'lib/tabular/table.rb', line 146

def 
  @metadata ||= {}
end

#renderer=(value) ⇒ Object

Set default Renderer. If present, will be used for all cells and Column headers.



131
132
133
# File 'lib/tabular/table.rb', line 131

def renderer=(value)
  columns.renderer = value
end

#renderersObject

List of Renderers



136
137
138
# File 'lib/tabular/table.rb', line 136

def renderers
  columns.renderers
end

#rowsObject



28
29
30
# File 'lib/tabular/table.rb', line 28

def rows
  @rows ||= []
end

#rows=(source_rows = []) ⇒ Object

Set table rows. Calls row <<, which creates columns and links the source rows to Row#source.



33
34
35
36
37
38
39
# File 'lib/tabular/table.rb', line 33

def rows=(source_rows = [])
  return unless source_rows

  source_rows.each do |row|
    self.<< row
  end
end

#strip!Object

Remove preceding and trailing whitespace from all cells. By default, Table does not strip whitespace from cells.



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

def strip!
  rows.each do |row|
    columns.each do |column|
      value = row[column.key]
      if value.respond_to?(:strip)
        row[column.key] = value.strip
      elsif value.is_a?(Float)
        row[column.key] = strip_decimal(value)
      end
    end
  end
end

#to_sObject



154
155
156
# File 'lib/tabular/table.rb', line 154

def to_s
  "#<#{self.class} #{rows.size}>"
end

#to_space_delimitedObject



150
151
152
# File 'lib/tabular/table.rb', line 150

def to_space_delimited
  ([columns] + rows).map(&:to_space_delimited).join("\n") << "\n"
end