Module: Tableparser

Defined in:
lib/tableparser.rb,
lib/tableparser/version.rb

Overview

Helpers for table formatted results

Constant Summary collapse

VERSION =
"1.0.1"

Class Method Summary collapse

Class Method Details

.parse(io) ⇒ Array<Array<String>> .parse(io) {|row| ... } ⇒ Object

Parses output from tables (often from SQL queries) to CSV-like array of rows of strings.

It expects the contents of io to look something like this, and will skip over rows that don’t match the format.

+---------+---------+
| col1    | col2    |
+---------+---------+
| val     | val     |
| val     | val     |
+---------+---------+

The block form streams from the input IO instead of reading all at once

Examples:

Tableparser.parse(STDIN) # =>
[['col1', 'col2'], ['val', 'val'], ['val', 'val']]
Tableparser.parse(STDIN) do |row|
  # ...
end

Overloads:

  • .parse(io) ⇒ Array<Array<String>>

    Returns rows.

    Parameters:

    • io (IO)

      io with source data to parse

    Returns:

    • (Array<Array<String>>)

      rows

  • .parse(io) {|row| ... } ⇒ Object

    Parameters:

    • io (IO)

      io with source data to parse

    Yield Parameters:

    • row (Array<String>)

      each row



34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/tableparser.rb', line 34

def self.parse(io)
  iter = io.each_line
  pattern = /\|/

  if block_given?
    iter.grep(pattern) do |line|
      yield parse_line(line)
    end
  else
    iter.grep(pattern).map { |line| parse_line(line) }
  end
end

.parse_line(line) ⇒ Array<String>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Examples:

parse a line

parse_line("| banklin_card_token           | state       |")
=> ['banklin_card_token', 'state']

Parameters:

  • line (String)

Returns:

  • (Array<String>)


89
90
91
# File 'lib/tableparser.rb', line 89

def self.parse_line(line)
  line.chomp.split('|').map(&:strip)[1..-1]
end

.parse_to_struct(io, struct) ⇒ Array<struct> .parse(io, struct) {|row| ... } ⇒ Object

Assumes struct was from Struct.new(:col1, :col2). The code sets values on the struct by name, not by index.

Note: the code will downcase column names when setting.

Since it builds on .parse, the block here form also streams from the input IO

Overloads:

  • .parse_to_struct(io, struct) ⇒ Array<struct>

    Returns an array of instances of the struct with fields set from the corresponding columns.

    Parameters:

    • io (IO)

      io with source data to parse

    • struct (Class)

      class that rows will be parsed as

    Returns:

    • (Array<struct>)

      an array of instances of the struct with fields set from the corresponding columns

  • .parse(io, struct) {|row| ... } ⇒ Object

    Parameters:

    • io (IO)

      io with source data to parse

    Yield Parameters:

    • row (struct)

      each row



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

def self.parse_to_struct(io, struct)
  cols = nil

  # grap the header row
  parse(io) do |values|
    cols = values
    break
  end

  if block_given?
    parse(io) do |values|
      yield to_struct(values, struct, cols)
    end
  else
    parse(io).map do |values|
      to_struct(values, struct, cols)
    end
  end
end

.to_struct(values, struct, cols) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



94
95
96
97
98
99
100
# File 'lib/tableparser.rb', line 94

def self.to_struct(values, struct, cols)
  struct.new.tap do |row|
    cols.zip(values).each do |name, val|
      row[name.downcase] = val
    end
  end
end