texttable 0.7.0 → 0.8.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 +58 -0
  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: 05a63a519ac14bb17ae4fc2e01e343842b69964389701fca2dabfa7f6188f862
4
- data.tar.gz: b0fc4bc9ba189bb8261a8f35dee4cff9bb0433c7b954db3c44a99e32007ad118
3
+ metadata.gz: 4fd483aa1f36230c2afe01d2a2dd26459506bc730e66187686ed83e6f43bfd20
4
+ data.tar.gz: c175539e6e2bc57f15f06f37a204079d2a13a70b5d29eeacc09b3fed30f16836
5
5
  SHA512:
6
- metadata.gz: '04752868c5a7e74f19355135b8f58ec14e77920e6b466a6e9f5f08d47a70cb9f19531a74b5b0d06fb9321340d865a5ff372960382b2b2dba6d9d753699635248'
7
- data.tar.gz: d642c5505cd6787b8f86ed51bddb58ff2adafae05ab4e0b094f377ae29fb74148fc8d960313dcefcfef9bbd2ef6eba4e88dd597fd3a0fb1bf2fe4c503c647437
6
+ metadata.gz: eea9d1c329bed3b186cc2141bb7cc9e8196e6cf9ce76077c39462e92f5cd8fc3f57b577d33030beafad51c239c47688215b59a788b44a7c2635f31a7991e80db
7
+ data.tar.gz: 5e17657bc436e6b7906364d0b7dcefbf4d6400976b5da98cdda360fe4888e9ddadc8c104da8d59078c77d966d876cb695faf2ff4aeb5ba9be3b6fa7256b71508
@@ -4,6 +4,64 @@ class TextTable
4
4
  def initialize
5
5
  @cols = Hash.new{|h,k| h[k] = h.size}
6
6
  @rows = []
7
+ @vals = nil
8
+ @row = 0
9
+ end
10
+
11
+ def index(field)
12
+ case field
13
+ when String, Symbol
14
+ field = field.to_s
15
+ index = @cols[field] || @cols[field.downcase.gsub(/\W/,'_')]
16
+ index
17
+ when Numeric
18
+ field
19
+ else
20
+ raise "invalid field index #{field.inspect}"
21
+ end
22
+ end
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
29
+ end
30
+
31
+ def row(row=nil)
32
+ row or return @row
33
+ @vals = @rows[@row = row]
34
+ self
35
+ end
36
+
37
+ def row=(row)
38
+ @vals = @rows[@row = row]
39
+ @row
40
+ end
41
+
42
+ def vals
43
+ @vals ||= @rows[@row]
44
+ end
45
+
46
+ def each
47
+ @rows or raise "no rows defined"
48
+ @rows.each_with_index {|_, row| yield(row(row)) }
49
+ end
50
+
51
+ def method_missing(field, *args)
52
+ field = field.to_s
53
+ equal = field.chomp!("=")
54
+ index = index(field)
55
+ if equal
56
+ index ||= @cols[field]
57
+ value = vals[index] = args.first
58
+ elsif index
59
+ raise "variable lookup ignores arguments" unless args.empty?
60
+ value = vals[index]
61
+ else
62
+ value = ""
63
+ end
64
+ value == false ? value : (value || "")
7
65
  end
8
66
 
9
67
  def add(hash)
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = "texttable"
5
- s.version = "0.7.0"
5
+ s.version = "0.8.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.7.0
4
+ version: 0.8.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-08-22 00:00:00.000000000 Z
11
+ date: 2019-09-26 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