Class: Tabular::Table
- Inherits:
-
Object
- Object
- Tabular::Table
- 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
-
#column_mapper ⇒ Object
Returns the value of attribute column_mapper.
-
#row_mapper ⇒ Object
Returns the value of attribute row_mapper.
-
#rows ⇒ Object
Returns the value of attribute rows.
Class Method Summary collapse
Instance Method Summary collapse
-
#<<(row) ⇒ Object
Add row to end of table.
-
#[](index) ⇒ Object
Return Row at zero-based index, or nil if Row is out of bounds.
-
#columns ⇒ Object
Instance of Tabular::Columns.
-
#delete_blank_columns!(*_options) ⇒ Object
Remove all columns that only contain a blank string, zero, or nil.
- #delete_blank_rows! ⇒ Object
- #delete_column(key) ⇒ Object
-
#delete_homogenous_columns!(*_options) ⇒ Object
Remove all columns that contain the same value in all rows.
-
#initialize(rows = []) ⇒ Table
constructor
Pass data in as
rows
. - #inspect ⇒ Object
-
#metadata ⇒ Object
Last-resort storage for client code data.
-
#renderer=(value) ⇒ Object
Set default Renderer.
-
#renderers ⇒ Object
List of Renderers.
-
#strip! ⇒ Object
Remove preceding and trailing whitespace from all cells.
- #to_s ⇒ Object
- #to_space_delimited ⇒ Object
Methods included from Zero
Methods included from Tabular::Tables::FileReading
#format_from, #read, #read_csv, #read_spreadsheet, #read_txt, #to_file_path
Methods included from Keys
Methods included from 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.
22 23 24 |
# File 'lib/tabular/table.rb', line 22 def initialize(rows = []) self.rows = rows end |
Instance Attribute Details
#column_mapper ⇒ Object
Returns the value of attribute column_mapper.
10 11 12 |
# File 'lib/tabular/table.rb', line 10 def column_mapper @column_mapper end |
#row_mapper ⇒ Object
Returns the value of attribute row_mapper.
11 12 13 |
# File 'lib/tabular/table.rb', line 11 def row_mapper @row_mapper end |
#rows ⇒ Object
Returns the value of attribute rows.
12 13 14 |
# File 'lib/tabular/table.rb', line 12 def rows @rows end |
Class Method Details
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 68 69 |
# File 'lib/tabular/table.rb', line 49 def <<(row) if row_mapper cells = row_mapper.map(row) else cells = row end if @columns.nil? || @columns.size == 0 @columns = Tabular::Columns.new(self, cells, column_mapper) if !cells.respond_to?(:keys) return columns end end _row = Tabular::Row.new(self, cells, row) _row.keys.each do |key| columns << key end rows << _row _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 |
#columns ⇒ Object
Instance of Tabular::Columns
76 77 78 |
# File 'lib/tabular/table.rb', line 76 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
81 82 83 84 85 86 87 88 89 |
# File 'lib/tabular/table.rb', line 81 def delete_blank_columns!(*) exceptions = extract_exceptions() (columns.map(&:key) - exceptions).each do |key| if rows.all? { |row| is_blank?(row[key]) || is_zero?(row[key]) } delete_column key end end end |
#delete_blank_rows! ⇒ Object
105 106 107 108 109 110 |
# File 'lib/tabular/table.rb', line 105 def delete_blank_rows! @rows = rows.reject(&:blank?) rows.each.with_index do |row, index| row.index = index end end |
#delete_column(key) ⇒ Object
127 128 129 130 131 132 |
# File 'lib/tabular/table.rb', line 127 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
92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/tabular/table.rb', line 92 def delete_homogenous_columns!(*) return if rows.size < 2 exceptions = extract_exceptions() (columns.map(&:key) - exceptions).each do |key| value = rows.first[key] if rows.all? { |row| row[key] == value } delete_column key end end end |
#inspect ⇒ Object
71 72 73 |
# File 'lib/tabular/table.rb', line 71 def inspect rows.map { |row| row.join(",") }.join("\n") end |
#metadata ⇒ Object
Last-resort storage for client code data
152 153 154 |
# File 'lib/tabular/table.rb', line 152 def @metadata ||= {} end |
#renderer=(value) ⇒ Object
Set default Renderer. If present, will be used for all cells and Column headers.
135 136 137 |
# File 'lib/tabular/table.rb', line 135 def renderer=(value) columns.renderer = value end |
#renderers ⇒ Object
List of Renderers
140 141 142 |
# File 'lib/tabular/table.rb', line 140 def renderers columns.renderers end |
#strip! ⇒ Object
Remove preceding and trailing whitespace from all cells. By default, Table does not strip whitespace from cells.
114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/tabular/table.rb', line 114 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_s ⇒ Object
160 161 162 |
# File 'lib/tabular/table.rb', line 160 def to_s "#<#{self.class} #{rows.size}>" end |
#to_space_delimited ⇒ Object
156 157 158 |
# File 'lib/tabular/table.rb', line 156 def to_space_delimited ([ columns ] + rows).map(&:to_space_delimited).join("\n") << "\n" end |