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
# File 'lib/tabular/table.rb', line 23

def initialize(rows = [])
  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.



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

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



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

def [](index)
  rows[index]
end

#columnsObject

Instance of Tabular::Columns



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

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



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

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



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

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

#delete_column(key) ⇒ Object



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

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



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

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



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

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

#metadataObject

Last-resort storage for client code data



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

def 
   ||= {}
end

#renderer=(value) ⇒ Object

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



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

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

#renderersObject

List of Renderers



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

def renderers
  columns.renderers
end

#rowsObject



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

def rows
  @rows ||= []
end

#rows=(source_rows = []) ⇒ Object

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



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

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.



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

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



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

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

#to_space_delimitedObject



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

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