texttable 1.1.5 → 1.1.6

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/lib/texttable.rb +34 -5
  3. data/texttable.gemspec +1 -1
  4. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 3f9b989789bcfe00d1c8ead3a7079c9156ac5bc7227324a34b79f82baa915bd3
4
- data.tar.gz: 4de414dc3664abba7a8cb089fa93bf5e059c779dcb9665314feb8b4c0588bc9b
3
+ metadata.gz: 926d3d58ed1350f3e78c718e4064b1200fb214bedad5227ef477971248e2a358
4
+ data.tar.gz: dc52ecae113f0385042fbf1fa1aee9b4f7ba92a2ae1d9f1e07d7706b05cf9a45
5
5
  SHA512:
6
- metadata.gz: 28c2cb92bbe3354f26776666c55a56ee177ff2969eea1fcf7b0fd0ee42bfe16dd1732315ee7328f8792f3e0e893f71af03f8fad5211091b176c23dee441f4f93
7
- data.tar.gz: 6869f42df077bc4d038d679114ec0a647b92c51ebc5e3623247c82cf8eedfbcd60e36bdd8f9144a61c6f722bb233a12e6f09c90c7b9531d8e8bebe01c9cde0ba
6
+ metadata.gz: 7c618a01e4a46eca7bb4b2bec56921c0ada77d1ba6570510e1b968b576d8c914cf9d4322ea3ca2c1351a6521ced8b9868cd76a7db332b4e56b2a9d9cebdeb097
7
+ data.tar.gz: 1368fb476f90884563b4ac0abdd89b014bfdb3c32a4613b1801d32eb3eabd23643ebb92f574670ed7c1820235a097a8f65fb0da28efccf9af4f4cbfe78dc0b12
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
 
@@ -91,6 +111,10 @@ class TextTable
91
111
  @rows.each_with_index {|_, row| yield(row(row)) }
92
112
  end
93
113
 
114
+ def each_pair
115
+ @cols.each {|col, pos| yield col, @values[pos] }
116
+ end
117
+
94
118
  def [](field, val=nil)
95
119
  index = index(field)
96
120
  value = vals[index] if index
@@ -126,6 +150,8 @@ class TextTable
126
150
  self
127
151
  end
128
152
 
153
+ alias :<< :add
154
+
129
155
  def show(*)
130
156
  self
131
157
  end
@@ -177,7 +203,7 @@ class TextTable
177
203
  def tsv(**kw); csv("\t", **kw); end
178
204
  def psv(**kw); csv("|" , **kw); end
179
205
 
180
- def sql(table='table', quote: false, timestamps: false, verb: 'insert')
206
+ def sql(table='table', quote: false, timestamps: false, verb: 'insert', out: nil)
181
207
  q = quote ? '`' : ''
182
208
  flip = @cols.invert
183
209
  @rows.each do |vals|
@@ -187,9 +213,12 @@ class TextTable
187
213
  list
188
214
  end
189
215
  list.push('created_at=now(), updated_at=now()') if timestamps
190
- puts "#{verb} into #{q}#{table}#{q} set #{list * ', '};" if !list.empty?
216
+ if !list.empty?
217
+ line = "#{verb} into #{q}#{table}#{q} set #{list * ', '};"
218
+ out ? (out << line) : puts(line)
219
+ end
191
220
  end
192
- nil
221
+ out
193
222
  end
194
223
  end
195
224
 
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.6"
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.6
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-03-25 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