texttable 0.8.1 → 0.9.0

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 +32 -24
  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: ca9b078035a8055ccdfca78776f872fd5b5d937c5298705c8c1119584950620f
4
- data.tar.gz: 80bca4dbf7c0e9c954845f04acc82e6fd7581262788690077e006a57fbbea246
3
+ metadata.gz: ca77eab48d17c39307bd1f4b58b31ba497d63c008628e765b026153870ee8397
4
+ data.tar.gz: 953d2d5b6cea2e116085d8b7df57b7542382cd3b24db31cc8e5d219d500da220
5
5
  SHA512:
6
- metadata.gz: 899aebab8ceffdc4b3f3e941c961298247a9da39e5a072b3b3cc82a5b236986cf9a10e613d3958dd07cf0a1e90be58955e568e7f8aa06f2ad6a140589e530f01
7
- data.tar.gz: ac90dcfd90a623859d6b7871dc4fc292538d6e79efd16154a8ae07a99f5c63158417bc49d550644a9012ae11d9fa7ac04a72948d530cdfdc8e41739c1d22b59a
6
+ metadata.gz: d70caf947e3da7c9e0fa397c640a3440f971a632626ae7b3af77023fa0c0ac75148483d1b07302fbb9d3d83656b56ff9f094cbb430ad2a800e4d1ff075375cba
7
+ data.tar.gz: 6452e896deb0406ab93e3284462f90fcd72abd0c294d5067b0880ebb457a54fe5bad0eed63dbc9f556a3a13ca805a9252dfdda059816db5f117c009f0ab0c097
data/lib/texttable.rb CHANGED
@@ -1,19 +1,19 @@
1
1
  class TextTable
2
- attr_accessor :cols, :rows
2
+ attr_accessor :values, :rows
3
3
 
4
- def initialize
5
- @cols = Hash.new{|h,k| h[k] = h.size}
4
+ def initialize(cols=nil)
5
+ @cols = Hash.new {|h,k| h[k] = h.size}
6
6
  @rows = []
7
- @vals = nil
7
+ @values = nil
8
8
  @row = 0
9
+ cols.each {|col| index!(col) } if cols.is_a?(Array)
9
10
  end
10
11
 
11
- def index(field)
12
+ def index(field, auto=false)
12
13
  case field
13
14
  when String, Symbol
14
15
  field = field.to_s
15
- index = @cols[field] || @cols[field.downcase.gsub(/\W/,'_')]
16
- index
16
+ index = @cols.key?(field) ? @cols[field] : auto ? @cols[field] : nil
17
17
  when Numeric
18
18
  field
19
19
  else
@@ -21,26 +21,27 @@ class TextTable
21
21
  end
22
22
  end
23
23
 
24
- def [](key, val=nil)
25
- @vals ||= @rows[@row]
26
- index = index(key)
27
- value = @vals[index] if index
28
- value.nil? ? val : value
24
+ def index!(field)
25
+ index(field, true)
26
+ end
27
+
28
+ def fields
29
+ @cols.keys
29
30
  end
30
31
 
31
32
  def row(row=nil)
32
33
  row or return @row
33
- @vals = @rows[@row = row]
34
+ @values = @rows[@row = row]
34
35
  self
35
36
  end
36
37
 
37
38
  def row=(row)
38
- @vals = @rows[@row = row]
39
+ @values = @rows[@row = row]
39
40
  @row
40
41
  end
41
42
 
42
43
  def vals
43
- @vals ||= @rows[@row]
44
+ @values ||= @rows[@row] ||= []
44
45
  end
45
46
 
46
47
  def each
@@ -48,26 +49,33 @@ class TextTable
48
49
  @rows.each_with_index {|_, row| yield(row(row)) }
49
50
  end
50
51
 
52
+ def [](field, val=nil)
53
+ index = index(field)
54
+ value = vals[index] if index
55
+ value.nil? ? val : value
56
+ end
57
+
58
+ def []=(field, value)
59
+ index = index!(field)
60
+ vals[index] = value
61
+ end
62
+
51
63
  def method_missing(field, *args)
52
64
  field = field.to_s
53
- equal = field.chomp!("=")
54
- index = index(field)
65
+ equal = field.chomp!('=')
66
+ index = index(field, equal)
55
67
  if equal
56
- index ||= @cols[field]
57
68
  value = vals[index] = args.first
58
69
  elsif index
59
70
  raise "variable lookup ignores arguments" unless args.empty?
60
- value = vals&.slice(index)
61
- # else
62
- # value = "" # failover to ""
71
+ value = vals[index]
63
72
  end
64
- # value == false ? value : (value || "") # failover to ""
65
73
  value
66
74
  end
67
75
 
68
76
  def add(hash)
69
- @rows << (vals = [])
70
- hash.each {|k, v| vals[@cols[k]] = v}
77
+ @values = @rows[@row = @rows.size] = []
78
+ hash.each {|k, v| @values[@cols[k]] = v}
71
79
  end
72
80
 
73
81
  def show
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 = "0.8.1"
5
+ s.version = "0.9.0"
6
6
  s.author = "Steve Shreeve"
7
7
  s.email = "steve.shreeve@gmail.com"
8
8
  s.summary = "An easy way to print 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: 0.8.1
4
+ version: 0.9.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Steve Shreeve
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-09-26 00:00:00.000000000 Z
11
+ date: 2019-10-08 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