texttable 0.8.1 → 0.9.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/texttable.rb +32 -24
- data/texttable.gemspec +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ca77eab48d17c39307bd1f4b58b31ba497d63c008628e765b026153870ee8397
|
4
|
+
data.tar.gz: 953d2d5b6cea2e116085d8b7df57b7542382cd3b24db31cc8e5d219d500da220
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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 :
|
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
|
-
@
|
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]
|
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
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
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
|
-
@
|
34
|
+
@values = @rows[@row = row]
|
34
35
|
self
|
35
36
|
end
|
36
37
|
|
37
38
|
def row=(row)
|
38
|
-
@
|
39
|
+
@values = @rows[@row = row]
|
39
40
|
@row
|
40
41
|
end
|
41
42
|
|
42
43
|
def vals
|
43
|
-
@
|
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
|
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
|
70
|
-
hash.each {|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
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.
|
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-
|
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
|