texttable 1.1.5 → 1.1.10

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 3f9b989789bcfe00d1c8ead3a7079c9156ac5bc7227324a34b79f82baa915bd3
4
- data.tar.gz: 4de414dc3664abba7a8cb089fa93bf5e059c779dcb9665314feb8b4c0588bc9b
3
+ metadata.gz: c8055fd1e6304a359eb5fd7a2cad2edd6a6f908e56d530d905b303a7d67bd767
4
+ data.tar.gz: 47c438e04d322e05388e605123f7bcd477318634430d5ab5d8537f14997936a6
5
5
  SHA512:
6
- metadata.gz: 28c2cb92bbe3354f26776666c55a56ee177ff2969eea1fcf7b0fd0ee42bfe16dd1732315ee7328f8792f3e0e893f71af03f8fad5211091b176c23dee441f4f93
7
- data.tar.gz: 6869f42df077bc4d038d679114ec0a647b92c51ebc5e3623247c82cf8eedfbcd60e36bdd8f9144a61c6f722bb233a12e6f09c90c7b9531d8e8bebe01c9cde0ba
6
+ metadata.gz: 8f17047f82b879f67f4a0d89ccbd3ac18a92327ff2c8ae967625d382a0ee712e29fea7c2d8fc83b869e3eebe4f8a5159084383d224cf075000c6f9af081464fa
7
+ data.tar.gz: bdfa482758d242886d149e93587c86109d0556dbca721a1db82e0cbaea82132a158df3db02e0c1a8a0e189c73f11b36b291fc8fe7d2ba51835407c556a6fcd1c
data/.ruby-version CHANGED
@@ -1 +1 @@
1
- 2.5
1
+ 2.6.5
data/lib/texttable.rb CHANGED
@@ -16,6 +16,27 @@ class TextTable
16
16
  def add(*args)
17
17
  new.add(*args)
18
18
  end
19
+
20
+ def load(data, delim="\t", headers=true)
21
+ case data
22
+ when String
23
+ if data.include?(delim) # string
24
+ text = data
25
+ elsif File.exist?(data) # filename
26
+ text = File.read(data)
27
+ end
28
+ when File, ARGF
29
+ text = data.read
30
+ end
31
+
32
+ text or raise "unable to load #{data.inspect}"
33
+ rows = text.split(/\r?\n/).map {|line| line.split(delim).map {|part| part.strip}}
34
+ info = new
35
+ rows.shift.each_with_index {|col, i| info.index!(col || i) } if headers
36
+ info.rows = rows
37
+ info.row(0)
38
+ info
39
+ end
19
40
  end
20
41
 
21
42
  def initialize(*args)
@@ -26,8 +47,7 @@ class TextTable
26
47
  rows = [] if !rows || !rows[0].is_a?(Array) || rows[0].empty?
27
48
  @cols = Hash.new {|h,k| h[k] = h.size}
28
49
  @rows = rows
29
- @values = nil
30
- @row = 0
50
+ row(0)
31
51
  cols.each_with_index {|col, i| index!(col || i) }
32
52
  end
33
53
 
@@ -47,6 +67,14 @@ class TextTable
47
67
  index(field, true)
48
68
  end
49
69
 
70
+ def lookup!(field)
71
+ @rows or raise "no rows defined"
72
+ index = index(field)
73
+ lookup = {}
74
+ @rows.each_with_index {|cols, i| lookup[cols[index]] = i}
75
+ lookup
76
+ end
77
+
50
78
  def convert_key(key)
51
79
  key.
52
80
  gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2').
@@ -91,6 +119,10 @@ class TextTable
91
119
  @rows.each_with_index {|_, row| yield(row(row)) }
92
120
  end
93
121
 
122
+ def each_pair
123
+ @cols.each {|col, pos| yield col, @values[pos] }
124
+ end
125
+
94
126
  def [](field, val=nil)
95
127
  index = index(field)
96
128
  value = vals[index] if index
@@ -115,9 +147,8 @@ class TextTable
115
147
  value
116
148
  end
117
149
 
118
- def add(obj, *args)
150
+ def update(obj, *args)
119
151
  obj = [obj, *args] if args.size > 0
120
- @values = @rows[@row = @rows.size] = []
121
152
  case obj
122
153
  when Hash then obj.each {|k, v| @values[index(k.to_s, true)] = v }
123
154
  when Array then @values.replace(obj)
@@ -126,6 +157,13 @@ class TextTable
126
157
  self
127
158
  end
128
159
 
160
+ def add(obj, *args)
161
+ @values = @rows[@row = @rows.size] = []
162
+ update(obj, *args)
163
+ end
164
+
165
+ alias :<< :add
166
+
129
167
  def show(*)
130
168
  self
131
169
  end
@@ -154,17 +192,14 @@ class TextTable
154
192
  self
155
193
  end
156
194
 
157
- def lookup!(field)
158
- @rows or raise "no rows defined"
159
- index = index(field)
160
- lookup = {}
161
- @rows.each_with_index {|cols, i| lookup[cols[index]] = i}
162
- lookup
195
+ def as_json(obj = defined?(ConfigHash) ? +{} : {})
196
+ (@rows || []).map {|r| r.each_with_index.inject(obj) {|h, (v, c)| h[fields[c]] = v; h }}
163
197
  end
164
198
 
165
199
  def csv(sep=',', encoding: nil, **kw)
166
200
  require 'csv'
167
- csv = CSV.new($stdout, {
201
+ out = kw.key?(:out) ? (kw.delete(:out) || "").dup : nil
202
+ csv = CSV.new(out || $stdout, {
168
203
  col_sep: sep,
169
204
  encoding: encoding ? encoding + ":UTF-8" : nil,
170
205
  quote_empty: false, #!# TODO: make this an option
@@ -172,12 +207,12 @@ class TextTable
172
207
  })
173
208
  csv << @cols.keys
174
209
  @rows.each {|vals| csv << vals}
175
- nil
210
+ out
176
211
  end
177
212
  def tsv(**kw); csv("\t", **kw); end
178
213
  def psv(**kw); csv("|" , **kw); end
179
214
 
180
- def sql(table='table', quote: false, timestamps: false, verb: 'insert')
215
+ def sql(table='table', quote: false, timestamps: false, verb: 'insert', out: nil)
181
216
  q = quote ? '`' : ''
182
217
  flip = @cols.invert
183
218
  @rows.each do |vals|
@@ -187,9 +222,12 @@ class TextTable
187
222
  list
188
223
  end
189
224
  list.push('created_at=now(), updated_at=now()') if timestamps
190
- puts "#{verb} into #{q}#{table}#{q} set #{list * ', '};" if !list.empty?
225
+ if !list.empty?
226
+ line = "#{verb} into #{q}#{table}#{q} set #{list * ', '};"
227
+ out ? (out << line) : puts(line)
228
+ end
191
229
  end
192
- nil
230
+ out
193
231
  end
194
232
  end
195
233
 
data/texttable.gemspec CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = "texttable"
5
- s.version = "1.1.5"
5
+ s.version = "1.1.10"
6
6
  s.author = "Steve Shreeve"
7
7
  s.email = "steve.shreeve@gmail.com"
8
8
  s.summary = "An easy way to work with rows and columns as simple tables"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: texttable
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.5
4
+ version: 1.1.10
5
5
  platform: ruby
6
6
  authors:
7
7
  - Steve Shreeve
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-10-29 00:00:00.000000000 Z
11
+ date: 2021-11-27 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: This gem will auto-size based on column widths.
14
14
  email: steve.shreeve@gmail.com
@@ -41,7 +41,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
41
41
  - !ruby/object:Gem::Version
42
42
  version: '0'
43
43
  requirements: []
44
- rubygems_version: 3.1.4
44
+ rubygems_version: 3.2.16
45
45
  signing_key:
46
46
  specification_version: 4
47
47
  summary: An easy way to work with rows and columns as simple tables