Class: TextTable

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ TextTable

Returns a new instance of TextTable.



4
5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/texttable.rb', line 4

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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(field, *args) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/texttable.rb', line 82

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



71
72
73
74
75
# File 'lib/texttable.rb', line 71

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

#[]=(field, value) ⇒ Object



77
78
79
80
# File 'lib/texttable.rb', line 77

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

#add(obj, *args) ⇒ Object



95
96
97
98
99
100
101
102
103
104
# File 'lib/texttable.rb', line 95

def add(obj, *args)
  obj = [obj, *args] if args.size > 0
  @values = @rows[@row = @rows.size] = []
  case obj
    when Hash  then obj.each {|k, v| @values[@cols[k]] = v}
    when Array then @values.replace(obj)
    else raise "unable to add #{obj.class} objects"
  end
  self
end

#convert_key(key) ⇒ Object



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

def convert_key(key)
  key.
    gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2').
    gsub(/([a-z\d])([A-Z])/, '\1_\2').
    gsub(/\W/, '_').
    downcase
end

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



124
125
126
127
128
129
130
131
132
133
# File 'lib/texttable.rb', line 124

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}
  nil
end

#each ⇒ Object



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

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

#fields ⇒ Object



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

def fields
  @cols.keys
end

#index(field, auto = false) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
# File 'lib/texttable.rb', line 19

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



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

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

#psv ⇒ Object



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

def psv; csv("|" ); end

#row(row = nil) ⇒ Object



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

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

#row=(row) ⇒ Object



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

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

#show ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/texttable.rb', line 106

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



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

def size
  @rows.size
end

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



137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/texttable.rb', line 137

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)|
      item = item.to_s #!# FIXME: force all to a string for now...
      list << "#{q}#{flip[i]}#{q}='#{item.gsub("'","''")}'" if item =~ /\S/
      list
    end
    puts "insert into #{q}#{table}#{q} set #{list * ', '};" if !list.empty?
  end
  nil
end

#tsv ⇒ Object



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

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

#vals ⇒ Object



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

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