Class: TextTable

Inherits:
Object
  • Object
show all
Defined in:
lib/texttable.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize ⇒ TextTable

Returns a new instance of TextTable.



4
5
6
7
8
9
# File 'lib/texttable.rb', line 4

def initialize
  @cols = Hash.new{|h,k| h[k] = h.size}
  @rows = []
  @vals = nil
  @row = 0
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(field, *args) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/texttable.rb', line 51

def method_missing(field, *args)
  field = field.to_s
  equal = field.chomp!("=")
  index = index(field)
  if equal
    index ||= @cols[field]
    value = vals[index] = args.first
  elsif index
    raise "variable lookup ignores arguments" unless args.empty?
    value = vals[index]
  else
    value = ""
  end
  value == false ? value : (value || "")
end

Instance Attribute Details

#cols ⇒ Object

Returns the value of attribute cols.



2
3
4
# File 'lib/texttable.rb', line 2

def cols
  @cols
end

#rows ⇒ Object

Returns the value of attribute rows.



2
3
4
# File 'lib/texttable.rb', line 2

def rows
  @rows
end

Instance Method Details

#[](key, val = nil) ⇒ Object



24
25
26
27
28
29
# File 'lib/texttable.rb', line 24

def [](key, val=nil)
  @vals ||= @rows[@row]
  index = index(key)
  value = @vals[index] if index
  value.nil? ? val : value
end

#add(hash) ⇒ Object



67
68
69
70
# File 'lib/texttable.rb', line 67

def add(hash)
  @rows << (vals = [])
  hash.each {|k, v| vals[@cols[k]] = v}
end

#csv(sep = ',', encoding: nil) ⇒ Object



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

def csv(sep=',', encoding: nil)
  require 'csv'
  csv = {}
  csv[:encoding] = encoding + ":UTF-8" if encoding
  csv[:col_sep ] = sep
  csv = CSV.new($stdout, csv)
  csv << cols.keys
  rows.each {|vals| csv << vals}
end

#each ⇒ Object



46
47
48
49
# File 'lib/texttable.rb', line 46

def each
  @rows or raise "no rows defined"
  @rows.each_with_index {|_, row| yield(row(row)) }
end

#index(field) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/texttable.rb', line 11

def index(field)
  case field
  when String, Symbol
    field = field.to_s
    index = @cols[field] || @cols[field.downcase.gsub(/\W/,'_')]
    index
  when Numeric
    field
  else
    raise "invalid field index #{field.inspect}"
  end
end

#psv ⇒ Object



100
# File 'lib/texttable.rb', line 100

def psv; csv("|" ); end

#row(row = nil) ⇒ Object



31
32
33
34
35
# File 'lib/texttable.rb', line 31

def row(row=nil)
  row or return @row
  @vals = @rows[@row = row]
  self
end

#row=(row) ⇒ Object



37
38
39
40
# File 'lib/texttable.rb', line 37

def row=(row)
  @vals = @rows[@row = row]
  @row
end

#show ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/texttable.rb', line 72

def show
  join = " | "
  both = [cols.keys] + rows
  flip = both.transpose
  wide = flip.map {|row| row.map {|col| col.to_s.size }.max }
  pict = wide.map {|len| "%-#{len}.#{len}s" }.join(join)
  pict = [join, pict, join].join.strip
  line = (pict % ([""] * cols.size)).tr("| ", "+-")
  seen = -1
  puts "", line
  both.each do |vals|
    puts pict % vals
    puts line if (seen += 1) == 0
  end
  puts line, "#{seen} rows displayed", ""
  self
end

#sql(table = 'table', quote: false) ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
# File 'lib/texttable.rb', line 102

def sql(table='table', quote: false)
  q = quote ? '`' : ''
  flip = cols.invert
  rows.each do |vals|
    list = vals.each_with_index.inject([]) do |list, (item, i)|
      list << "#{q}#{flip[i]}#{q}='#{item.gsub("'","''")}'" if item =~ /\S/
      list
    end
    puts "insert into #{q}#{table}#{q} set #{list * ', '};" if !list.empty?
  end
end

#tsv ⇒ Object



99
# File 'lib/texttable.rb', line 99

def tsv; csv("\t"); end

#vals ⇒ Object



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

def vals
  @vals ||= @rows[@row]
end