Class: TextTable

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cols = nil, rows = nil) ⇒ TextTable

Returns a new instance of TextTable.



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

def initialize(cols=nil, rows=nil)
  @cols = Hash.new {|h,k| h[k] = h.size}
  @rows = rows || []
  @values = nil
  @row = 0
  cols.each {|col| index!(col) } if cols.is_a?(Array)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(field, *args) ⇒ Object



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

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

Instance Attribute Details

#rows ⇒ Object

Returns the value of attribute rows.



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

def rows
  @rows
end

#values ⇒ Object

Returns the value of attribute values.



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

def values
  @values
end

Instance Method Details

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



62
63
64
65
66
# File 'lib/texttable.rb', line 62

def [](field, val=nil)
  index = index(field)
  value = vals[index] if index
  value.nil? ? val : value
end

#[]=(field, value) ⇒ Object



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

def []=(field, value)
  index = index!(field)
  vals[index] = value
end

#add(hash) ⇒ Object



86
87
88
89
# File 'lib/texttable.rb', line 86

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

#convert_key(key) ⇒ Object



28
29
30
31
32
# File 'lib/texttable.rb', line 28

def convert_key(key)
  key.to_s.gsub(/\W/, '_').gsub(/([^_])([A-Z])/, '\1_\2').downcase # allow CamelCase
  # key.to_s.gsub(/\W/, '_').downcase # allow non-word chars
  # key.to_s.downcase # force downcase
end

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



109
110
111
112
113
114
115
116
117
# File 'lib/texttable.rb', line 109

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



57
58
59
60
# File 'lib/texttable.rb', line 57

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

#fields ⇒ Object



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

def fields
  @cols.keys
end

#index(field, auto = false) ⇒ Object



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

def index(field, auto=false)
  case field
  when String, Symbol
    field = convert_key(field)
    index = @cols.key?(field) ? @cols[field] : auto ? @cols[field] : nil
  when Numeric
    field
  else
    raise "invalid field index #{field.inspect}"
  end
end

#index!(field) ⇒ Object



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

def index!(field)
  index(field, true)
end

#psv ⇒ Object



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

def psv; csv("|" ); end

#row(row = nil) ⇒ Object



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

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

#row=(row) ⇒ Object



48
49
50
51
# File 'lib/texttable.rb', line 48

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

#show ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/texttable.rb', line 91

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

#size ⇒ Object



34
35
36
# File 'lib/texttable.rb', line 34

def size
  @rows.size
end

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



121
122
123
124
125
126
127
128
129
130
131
# File 'lib/texttable.rb', line 121

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



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

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

#vals ⇒ Object



53
54
55
# File 'lib/texttable.rb', line 53

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