texttable 1.1.3 → 1.1.4

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 +21 -7
  3. data/texttable.gemspec +1 -1
  4. metadata +6 -6
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: daf8fb1088ce9570617aea39c1cbbfa3b4b5819af7013e26c2e4680d1ec2ceb1
4
- data.tar.gz: 343e38b5ff4ed2cb20076af22bcfaedb00fd70702dcc8654b4169736b87a4768
3
+ metadata.gz: 5c4dbf7e3ed3836d1f3e61665700ca6dc7aaf94c95a1a026b3a84cd613c89ee7
4
+ data.tar.gz: fc17d9a066c413f3c887889ad488ce703381470f6c53992e10f3ff54d47c7b51
5
5
  SHA512:
6
- metadata.gz: dfbd834c7999c0c666c39043f2529b31fa4077292aec331541744cb6fc22a841380914435b75c3462647896969d4b8eb6846e72ebdf6cb3878c761f0ac8005a1
7
- data.tar.gz: 8b401aab5b7df8d12aa883a7c0932cf5e4713889d5a3f63aa59416cfb3e3c15eb9029257819e9e46aa62e7f23a547239cd2e21e52a35ab91990240c950bdd279
6
+ metadata.gz: 11639822bf41842b1ee7e8817e53f724ce22cc7fa128391675700a8aa4048e975fe00679bf73d534c276991dcfab242205043d5d20f749b3de0439ab262de300
7
+ data.tar.gz: 42519909f0df28de90b93744fb985e99ae3662395cdc2815767d4a0510108154a632e791b295537ee12efd862a5010f3b2af5165ce351b264e512caacd8c528d
@@ -2,15 +2,20 @@ class TextTable
2
2
  attr_accessor :values, :rows
3
3
 
4
4
  class << self
5
- def csv(src, sep=',', encoding: nil)
5
+ def csv(src, sep=',', encoding: nil, **kw)
6
6
  require 'csv'
7
7
  new CSV.read(src || ARGF, {
8
8
  col_sep: sep,
9
9
  encoding: encoding ? encoding + ":UTF-8" : nil,
10
+ **kw
10
11
  }.compact)
11
12
  end
12
- def tsv(*args); csv(args.shift, "\t", *args); end
13
- def psv(*args); csv(args.shift, "|" , *args); end
13
+ def tsv(*args, **kw); csv(args.shift, "\t", *args, **kw); end
14
+ def psv(*args, **kw); csv(args.shift, "|" , *args, **kw); end
15
+
16
+ def add(*args)
17
+ new.add(*args)
18
+ end
14
19
  end
15
20
 
16
21
  def initialize(*args)
@@ -23,7 +28,7 @@ class TextTable
23
28
  @rows = rows
24
29
  @values = nil
25
30
  @row = 0
26
- cols.each {|col| index!(col) }
31
+ cols.each_with_index {|col, i| index!(col || i) }
27
32
  end
28
33
 
29
34
  def index(field, auto=false)
@@ -114,7 +119,7 @@ class TextTable
114
119
  obj = [obj, *args] if args.size > 0
115
120
  @values = @rows[@row = @rows.size] = []
116
121
  case obj
117
- when Hash then obj.each {|k, v| @values[@cols[k]] = v}
122
+ when Hash then obj.each {|k, v| @values[index(k.to_s, true)] = v }
118
123
  when Array then @values.replace(obj)
119
124
  else raise "unable to add #{obj.class} objects"
120
125
  end
@@ -149,11 +154,20 @@ class TextTable
149
154
  self
150
155
  end
151
156
 
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
163
+ end
164
+
152
165
  def csv(sep=',', encoding: nil)
153
166
  require 'csv'
154
167
  csv = {}
155
- csv[:encoding] = encoding + ":UTF-8" if encoding
156
- csv[:col_sep ] = sep
168
+ csv[:encoding ] = encoding + ":UTF-8" if encoding
169
+ csv[:col_sep ] = sep
170
+ csv[:quote_empty] = false #!# TODO: make this an option
157
171
  csv = CSV.new($stdout, csv)
158
172
  csv << @cols.keys
159
173
  @rows.each {|vals| csv << vals}
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = "texttable"
5
- s.version = "1.1.3"
5
+ s.version = "1.1.4"
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.3
4
+ version: 1.1.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Steve Shreeve
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-06-10 00:00:00.000000000 Z
11
+ date: 2020-10-06 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
@@ -26,7 +26,7 @@ homepage: https://github.com/shreeve/texttable
26
26
  licenses:
27
27
  - MIT
28
28
  metadata: {}
29
- post_install_message:
29
+ post_install_message:
30
30
  rdoc_options: []
31
31
  require_paths:
32
32
  - lib
@@ -41,8 +41,8 @@ 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.2
45
- signing_key:
44
+ rubygems_version: 3.1.4
45
+ signing_key:
46
46
  specification_version: 4
47
47
  summary: An easy way to work with rows and columns as simple tables
48
48
  test_files: []