Module: FatTable

Defined in:
lib/fat_table.rb,
lib/fat_table/table.rb,
lib/fat_table/column.rb,
lib/fat_table/errors.rb,
lib/fat_table/footer.rb,
lib/fat_table/convert.rb,
lib/fat_table/version.rb,
lib/fat_table/db_handle.rb,
lib/fat_table/evaluator.rb,
lib/fat_table/formatters/formatter.rb,
lib/fat_table/formatters/aoa_formatter.rb,
lib/fat_table/formatters/aoh_formatter.rb,
lib/fat_table/formatters/org_formatter.rb,
lib/fat_table/formatters/term_formatter.rb,
lib/fat_table/formatters/text_formatter.rb,
lib/fat_table/formatters/latex_formatter.rb

Overview

Set and access a database by module-level methods.

Defined Under Namespace

Modules: Convert Classes: AoaFormatter, AohFormatter, Column, Evaluator, Footer, Formatter, IncompatibleTypeError, LaTeXFormatter, LogicError, NoTable, OrgFormatter, Table, TermFormatter, TextFormatter, TransientError, UserError

Constant Summary collapse

FORMATS =

Valid output formats as symbols.

i[psv aoa aoh latex org term text].freeze
VERSION =

The current version of FatTable

'1.0.0'

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.currency_symbolObject

Default value to use to indicate currency in a Numeric column. By default this is set to '$'.



79
80
81
# File 'lib/fat_table.rb', line 79

def currency_symbol
  @currency_symbol
end

.formatObject

Set a default output format to use when FatTable.to_format is invoked. Valid formats are +:psv+, +:aoa+, +:aoh+, +:latex+, +:org+, +:term+, and +:text+, or their string equivalents. By default, +FatTable.format+ is +:text+.



75
76
77
# File 'lib/fat_table.rb', line 75

def format
  @format
end

.handleObject

The +Sequel+ database handle to use in calls to +FatTable.from_sql+.



7
8
9
# File 'lib/fat_table/db_handle.rb', line 7

def handle
  @handle
end

Class Method Details

.connect(args) ⇒ Object

This method must be called before calling +FatTable.from_sql+ or +FatTable::Table.from_sql+ in order to specify the database to use.

You can pass in a +Sequel+ connection with +db+, or have fat_table construct a uri from given components. In the latter case, all of the keyword parameters have a default except +database:+, which must contain the name of the database to query.

+db+:: Inject a Sequel connection constructed +Sequel.connect+ or one of Sequel's adapter-specific connection methods. http://sequel.jeremyevans.net/rdoc/files/doc/opening_databases_rdoc.html

+adapter+:: One of 'pg' (for Postgresql), 'mysql' or 'mysql2' (for Mysql), or 'sqlite' (for SQLite3) (or any other adapter supported by the +Sequel+ gem) to specify the driver to use. You may have to install the appropriate driver to make this work.

+database+:: The name of the database to access. There is no default for this.

+user+:: The user name to use for accessing the database. It defaults to nil, which may be interpreted as a default user by the Sequel driver being used.

+password+:: The password to use for accessing the database. It defaults to nil, which may be interpreted as a default password by the Sequel driver being used.

+host+:: The name of the host on which to look for the database connection, defaulting to 'localhost'.

+port+:: The port number as a string or integer on which to access the database on the given host. Defaults to '5432'. Only used if host is not 'localhost'.

+socket+:: The socket to use to access the database if the host is 'localhost'. Defaults to the standard socket for the Pg driver, '/tmp/.s.PGSQL.5432'.

If successful the database handle for Sequel is return. Once called successfully, this establishes the database handle to use for all subsequent calls to FatTable.from_sql or FatTable::Table.from_sql. You can then access the handle if needed with FatTable.db.



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/fat_table/db_handle.rb', line 57

def self.connect(args)
  # Set the dsn for Sequel
  begin
    self.handle = Sequel.connect(args)
  rescue Sequel::AdapterNotFound => ex
    case ex.to_s
    when /pg/
      raise TransientError, 'You need to install the postgres adapter pg'
    when /mysql/
      raise TransientError, 'You need to install the mysql adapter'
    when /sqlite/
      raise TransientError, 'You need to install the sqlite adapter'
    else
      raise ex
    end
  end
  handle
end

.dbObject

Return the +Sequel+ database handle.



77
78
79
# File 'lib/fat_table/db_handle.rb', line 77

def self.db
  handle
end

.db=(db) ⇒ Object

Directly set the db handle to a Sequel connection formed without FatTable.connect.



83
84
85
# File 'lib/fat_table/db_handle.rb', line 83

def self.db=(db)
  self.handle = db
end

.from_aoa(aoa, hlines: false, **types) ⇒ Object

Construct a FatTable::Table from the array of arrays +aoa+. By default, with +hlines+ false, do not look for nil separators, just treat the first row as headers. With +hlines+ true, expect separators to mark the header row and any boundaries. If the second element of the array is a nil, interpret the first element of the array as a row of headers. Otherwise, synthesize headers of the form +:col_1+, +:col_2+, ... and so forth. The remaining elements are taken as the body of the table, except that if an element of the outer array is a nil, mark the preceding row as a boundary. In Emacs org-mode code blocks, by default (+:hlines no+) all hlines are stripped from the table, otherwise (+:hlines yes+) they are indicated with nil elements in the outer array.



137
138
139
# File 'lib/fat_table.rb', line 137

def self.from_aoa(aoa, hlines: false, **types)
  Table.from_aoa(aoa, hlines: hlines, **types)
end

.from_aoh(aoh, hlines: false, **types) ⇒ Object

Construct a FatTable::Table from the array of hashes +aoh+, which can be an array of any objects that respond to the #to_h method. With +hlines+ true, interpret nil separators as marking boundaries in the new Table. All hashes must have the same keys, which, converted to symbols, become the headers for the new Table.



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

def self.from_aoh(aoh, hlines: false, **types)
  Table.from_aoh(aoh, hlines: hlines, **types)
end

.from_csv_file(fname, **types) ⇒ Object

Construct a FatTable::Table from the contents of a CSV file given by the file name +fname+. Headers will be taken from the first row and converted to symbols.



97
98
99
# File 'lib/fat_table.rb', line 97

def self.from_csv_file(fname, **types)
  Table.from_csv_file(fname, **types)
end

.from_csv_string(str, **types) ⇒ Object

Construct a FatTable::Table from the string +str+, treated in the same manner as if read the input from a CSV file. Headers will be taken from the first row and converted to symbols.



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

def self.from_csv_string(str, **types)
  Table.from_csv_string(str, **types)
end

.from_org_file(fname, **types) ⇒ Object

Construct a FatTable::Table from the first table found in the Emacs org-mode file names +fname+. Headers are taken from the first row if the second row is an hline. Otherwise, synthetic headers of the form +:col_1+, +:col_2+, etc. are created. Any other hlines will be treated as marking a boundary in the table.



113
114
115
# File 'lib/fat_table.rb', line 113

def self.from_org_file(fname, **types)
  Table.from_org_file(fname, **types)
end

.from_org_string(str, **types) ⇒ Object

Construct a FatTable::Table from the first table found in the string +str+, treated in the same manner as if read from an Emacs org-mode file. Headers are taken from the first row if the second row is an hrule. Otherwise, synthetic headers of the form :col_1, :col_2, etc. are created. Any other hlines will be treated as marking a boundary in the table.



122
123
124
# File 'lib/fat_table.rb', line 122

def self.from_org_string(str, **types)
  Table.from_org_string(str, **types)
end

.from_sql(query, **types) ⇒ Object

Construct a Table by running a SQL query against the database set up with FatTable.connect. Return the Table with the query results as rows and the headers from the query, converted to symbols, as headers.



159
160
161
# File 'lib/fat_table.rb', line 159

def self.from_sql(query, **types)
  Table.from_sql(query, **types)
end

.from_table(table) ⇒ Object

Construct a FatTable::Table from another FatTable::Table. Inherit any group boundaries from the input table.



152
153
154
# File 'lib/fat_table.rb', line 152

def self.from_table(table)
  Table.from_table(table)
end

.new(*args, **types) ⇒ Object

Return an empty FatTable::Table object. You can use FatTable::Table#add_row or FatTable::Table#add_column to populate the table with data.



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

def self.new(*args, **types)
  Table.new(*args, **types)
end

.to_any(fmt, table, **options) ⇒ Object

Return a string or ruby object according to the format given in the +fmt+ argument. Valid formats are :psv, :aoa, :aoh, :latex, :org, :term, :text, or their string equivalents. If a block is given, it will yield a +FatTable::Formatter+ of the appropriate type on which formatting and footer methods can be called. If no block is given, the default format for the +fmt+ type will be used.

Raises:



187
188
189
190
191
192
193
194
195
196
197
# File 'lib/fat_table.rb', line 187

def self.to_any(fmt, table, **options)
  fmt = fmt.as_sym
  raise UserError, "unknown format '#{fmt}'" unless FORMATS.include?(fmt)

  method = "to_#{fmt}"
  if block_given?
    send(method, table, **options, &Proc.new)
  else
    send(method, table, **options)
  end
end

.to_aoa(table, **options) {|fmt| ... } ⇒ Object

Return the table as an Array of Array of strings. If no block is given, default formatting is applies to the table's cells. If a block is given, it yields an AoaFormatter to the block to which formatting instructions and footers can be added by calling methods on it.

Yields:

  • (fmt)


214
215
216
217
218
# File 'lib/fat_table.rb', line 214

def self.to_aoa(table, **options)
  fmt = AoaFormatter.new(table, **options)
  yield fmt if block_given?
  fmt.output
end

.to_aoh(table, **options) {|fmt| ... } ⇒ Object

Return the table as an Array of Hashes. Each inner hash uses the Table's columns as keys and it values are strings representing the cells of the table. If no block is given, default formatting is applies to the table's cells. If a block is given, it yields an AohFormatter to the block to which formatting instructions and footers can be added by calling methods on it.

Yields:

  • (fmt)


225
226
227
228
229
# File 'lib/fat_table.rb', line 225

def self.to_aoh(table, **options)
  fmt = AohFormatter.new(table, **options)
  yield fmt if block_given?
  fmt.output
end

.to_format(table, **options) ⇒ Object

Return a string or ruby object formatting +table+ according to the format specified in +FatTable.format+. If a block is given, it will yield a +FatTable::Formatter+ of the appropriate type on which formatting and footer methods can be called. If no block is given, the default format for the type will be used. The +options+ are passed along to the FatTable::Formatter created to process the output.



173
174
175
176
177
178
179
# File 'lib/fat_table.rb', line 173

def self.to_format(table, **options) # :yields: formatter
  if block_given?
    to_any(format, table, **options, &Proc.new)
  else
    to_any(format, table, **options)
  end
end

.to_latex(table, **options) {|fmt| ... } ⇒ Object

Return the table as a string containing a LaTeX table. If no block is given, default formatting applies to the table's cells. If a block is given, it yields a LaTeXFormatter to the block to which formatting instructions and footers can be added by calling methods on it.

Yields:

  • (fmt)


235
236
237
238
239
# File 'lib/fat_table.rb', line 235

def self.to_latex(table, **options)
  fmt = LaTeXFormatter.new(table, **options)
  yield fmt if block_given?
  fmt.output
end

.to_org(table, **options) {|fmt| ... } ⇒ Object

Return the table as a string containing an Emacs org-mode table. If no block is given, default formatting applies to the table's cells. If a block is given, it yields a OrgFormatter to the block to which formatting instructions and footers can be added by calling methods on it.

Yields:

  • (fmt)


245
246
247
248
249
# File 'lib/fat_table.rb', line 245

def self.to_org(table, **options)
  fmt = OrgFormatter.new(table, **options)
  yield fmt if block_given?
  fmt.output
end

.to_psv(table, **options) {|fmt| ... } ⇒ Object

Return the +table+ as a string formatted as pipe-separated values, passing the +options+ to a new +FatTable::Formatter+ object. If no block is given, default formatting is applied to the +table+'s cells. If a block is given, it yields the +FatTable::Formatter+ to the block on which formatting and footer methods can be called.

Yields:

  • (fmt)


204
205
206
207
208
# File 'lib/fat_table.rb', line 204

def self.to_psv(table, **options)
  fmt = Formatter.new(table, options)
  yield fmt if block_given?
  fmt.output
end

.to_term(table, **options) {|fmt| ... } ⇒ Object

Return the table as a string containing ANSI terminal text representing table. If no block is given, default formatting applies to the table's cells. If a block is given, it yields a TermFormatter to the block to which formatting instructions and footers can be added by calling methods on it.

Yields:

  • (fmt)


255
256
257
258
259
# File 'lib/fat_table.rb', line 255

def self.to_term(table, **options)
  fmt = TermFormatter.new(table, **options)
  yield fmt if block_given?
  fmt.output
end

.to_text(table, **options) {|fmt| ... } ⇒ Object

Return the table as a string containing ordinary text representing table. If no block is given, default formatting applies to the table's cells. If a block is given, it yields a TextFormatter to the block to which formatting instructions and footers can be added by calling methods on it.

Yields:

  • (fmt)


265
266
267
268
269
# File 'lib/fat_table.rb', line 265

def self.to_text(table, **options)
  fmt = TextFormatter.new(table, **options)
  yield fmt if block_given?
  fmt.output
end