simpletable 0.3.0 → 0.3.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +8 -8
- data/lib/simpletable.rb +19 -7
- data/lib/simpletable/version.rb +1 -1
- data/lib/test.rb +7 -6
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
---
|
2
2
|
!binary "U0hBMQ==":
|
3
3
|
metadata.gz: !binary |-
|
4
|
-
|
4
|
+
MmU3Y2YxODA5ODIxZDVjMzcwMGUzZGVlYmU5ZTIxMDZmZjY5ODA2OQ==
|
5
5
|
data.tar.gz: !binary |-
|
6
|
-
|
6
|
+
N2RmNzQyYjI4NDBiN2FhOGJiYWNmNWM4ZDc3ZjQ4ZGU5NDJkYzllMA==
|
7
7
|
!binary "U0hBNTEy":
|
8
8
|
metadata.gz: !binary |-
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
N2NmNjc0N2ViNjMyZDY1YmQ1NGJiZWUwNGY0MzZlNTA5ODcyNWFlYjYxNDA3
|
10
|
+
NzllNmQ4MjgxNmI5MTgyZDljOGQxYmNhOGFmMTI2ODI3YjE4ZTNiNmU5MjA1
|
11
|
+
Nzg0OTNmNzEwODBlOWUzZDE5YTdjMGUyOTg4ZGExOWY2NTMxNjA=
|
12
12
|
data.tar.gz: !binary |-
|
13
|
-
|
14
|
-
|
15
|
-
|
13
|
+
NzIwNDc0OWFmN2FlMTNlY2YxNzJjNWQ1YjIxMjNhY2IxMjIyNmFkMDA5ZDQ2
|
14
|
+
ZGJkNmZkN2I4MzJlZGJkZDBlMDkwNDI1MWZlYmU3ZDRmMzZmOGRiNzA2YjQw
|
15
|
+
MWU2NmNhMjBkN2RiYjljZDQ0M2MxNjM5M2EyNDQ2MzhhNmMzZmQ=
|
data/lib/simpletable.rb
CHANGED
@@ -2,8 +2,10 @@ require "simpletable/version"
|
|
2
2
|
|
3
3
|
DEFAULT_DIVIDER = "="
|
4
4
|
DEFAULT_PADDING = 2
|
5
|
+
DEFAULT_PLACEHOLDER = "-"
|
5
6
|
|
6
7
|
class SimpleTable
|
8
|
+
attr_accessor :divider, :padding, :placeholder
|
7
9
|
def from_objects( objects, titles, methods, options = {} )
|
8
10
|
raise "Mismatched number of methods and column titles" if titles.length != methods.length
|
9
11
|
@objects = objects
|
@@ -11,6 +13,7 @@ class SimpleTable
|
|
11
13
|
@methods = methods
|
12
14
|
@divider = options[:divider] || DEFAULT_DIVIDER
|
13
15
|
@padding = options[:padding] || DEFAULT_PADDING
|
16
|
+
@placeholder = options[:placeholder] || DEFAULT_PLACEHOLDER
|
14
17
|
self
|
15
18
|
end
|
16
19
|
|
@@ -18,7 +21,7 @@ class SimpleTable
|
|
18
21
|
widths = []
|
19
22
|
# calculate column widths
|
20
23
|
@titles.zip(@methods).each do |title,method|
|
21
|
-
widths << @objects.collect { |o| o
|
24
|
+
widths << @objects.collect { |o| call_method(o,method) }.push(title).group_by(&:size).max.first + @padding
|
22
25
|
end
|
23
26
|
|
24
27
|
# print table header
|
@@ -27,24 +30,25 @@ class SimpleTable
|
|
27
30
|
|
28
31
|
# print table body
|
29
32
|
@objects.each do |o|
|
30
|
-
data = @methods.collect{ |m| o
|
33
|
+
data = @methods.collect{ |m| call_method(o,m) } # collect row data
|
31
34
|
text << row(data,widths)
|
32
35
|
end
|
33
36
|
text
|
34
37
|
end
|
35
38
|
|
36
|
-
def csv(separator
|
39
|
+
def csv(separator=@separator)
|
40
|
+
@separator = separator if @separator != separator
|
37
41
|
# quote strings w/ embedded separator characters
|
38
42
|
titles = []
|
39
|
-
@titles.each {|t| titles << (t.include?(separator) ? t.gsub(t,"\"#{t}\"") : t)}
|
43
|
+
@titles.each {|t| titles << (t.include?(@separator) ? t.gsub(t,"\"#{t}\"") : t)}
|
40
44
|
|
41
45
|
# print table header
|
42
|
-
text = titles.join(separator) << "\n"
|
46
|
+
text = titles.join(@separator) << "\n"
|
43
47
|
|
44
48
|
# print table body
|
45
49
|
@objects.each do |o|
|
46
|
-
data = @methods.collect{ |m| o
|
47
|
-
text << data.join(separator) << "\n"
|
50
|
+
data = @methods.collect{ |m| call_method(o,m) } # collect row data
|
51
|
+
text << data.join(@separator) << "\n"
|
48
52
|
end
|
49
53
|
text
|
50
54
|
end
|
@@ -55,4 +59,12 @@ private
|
|
55
59
|
data.zip(widths).each { |d,w| row << d.to_s.ljust(w) }
|
56
60
|
row << "\n"
|
57
61
|
end
|
62
|
+
|
63
|
+
def call_method(obj,method)
|
64
|
+
begin
|
65
|
+
obj.send(method).to_s
|
66
|
+
rescue NoMethodError
|
67
|
+
@placeholder == @separator ? "\"#{@placeholder}\"" : @placeholder
|
68
|
+
end
|
69
|
+
end
|
58
70
|
end
|
data/lib/simpletable/version.rb
CHANGED
data/lib/test.rb
CHANGED
@@ -11,9 +11,9 @@ class TestObject
|
|
11
11
|
end
|
12
12
|
end
|
13
13
|
|
14
|
-
titles = [ "Title A", "Title B is very long, the longest", "Title C" ]
|
15
|
-
titles2 = [ "Title A", "Title B", "Title C is very long; the longest" ]
|
16
|
-
methods = [ :p1, :p2, :p3 ]
|
14
|
+
titles = [ "Title A", "Title B is very long, the longest", "Title C", "Title D" ]
|
15
|
+
titles2 = [ "Title A", "Title B", "Title C", "Title D is very long; the longest" ]
|
16
|
+
methods = [ :p1, :p2, :p4, :p3 ]
|
17
17
|
|
18
18
|
objs = []
|
19
19
|
objs << TestObject.new("thisisaword","anotherword","short")
|
@@ -25,10 +25,11 @@ objs << TestObject.new("thisisthebiggestword","thisisthebiggestword","thisistheb
|
|
25
25
|
t = SimpleTable.new
|
26
26
|
|
27
27
|
puts t.from_objects(objs,titles,methods).text << "\n"
|
28
|
-
|
28
|
+
t.placeholder = "+"
|
29
|
+
puts t.csv("+") << "\n"
|
29
30
|
|
30
|
-
puts t.from_objects(objs,titles,methods,{:divider=>"*"}).text << "\n"
|
31
|
-
puts t.csv << "\n"
|
31
|
+
puts t.from_objects(objs,titles,methods,{:divider=>"*",:placeholder=>"-"}).text << "\n"
|
32
|
+
puts t.csv(",") << "\n"
|
32
33
|
|
33
34
|
puts t.from_objects(objs,titles,methods,{:divider=>"-",:padding=>5}).text << "\n"
|
34
35
|
puts t.csv << "\n"
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: simpletable
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.3.
|
4
|
+
version: 0.3.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Brady Turner
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2015-01-01 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|