simpletable 0.1.1 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +8 -8
- data/README.md +27 -28
- data/lib/simpletable.rb +31 -11
- data/lib/simpletable/version.rb +2 -2
- data/lib/test.rb +22 -7
- data/simpletable.gemspec +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
---
|
2
2
|
!binary "U0hBMQ==":
|
3
3
|
metadata.gz: !binary |-
|
4
|
-
|
4
|
+
ZTk0MTBlYzY0Y2JhNDJkMzk5YTJhYTI1OGNjYTIyYzE2ZmQ3YWJiYw==
|
5
5
|
data.tar.gz: !binary |-
|
6
|
-
|
6
|
+
ZTY2MzliYmNkOTJmYjQ2OTRjMDdmZGZiOWRjNTE1YzQ1MWM3MzcyNw==
|
7
7
|
!binary "U0hBNTEy":
|
8
8
|
metadata.gz: !binary |-
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
MTI5Y2NmMjQzODZjNmRhNTVhNjIwZWMxYWNjOTJlYTM0ZmNkMDBkY2IyZTM0
|
10
|
+
ZGUyNmE4MjI1MGQ5YmI1YjUyZWI3OTY4YTRmYjdmYmQ1OTZjZjkyMjQxZWE2
|
11
|
+
YmVlMjBhMmJkZGFhZGFiMjE5NGNkZWZlYzhhMjYzNDNkOGEzZmI=
|
12
12
|
data.tar.gz: !binary |-
|
13
|
-
|
14
|
-
|
15
|
-
|
13
|
+
OGU3MzQzMmQ0ZTIxMThjYjgyOGQ4ZDNlYjRmZWM4MzE2MTg5MWE2YTg3Zjg0
|
14
|
+
NzI2N2RjZDIwZTNhZDI2OGNlYTUwODNkMDc3ZTFiNTA5ODBiNDUzN2NkYjFk
|
15
|
+
MTY0NDhmOTZjZTdkMjJmNzAzNjVkZGE3ZjcwOWFjMjA0M2RjN2I=
|
data/README.md
CHANGED
@@ -1,29 +1,28 @@
|
|
1
|
-
|
1
|
+
simpletable
|
2
|
+
========
|
3
|
+
A Ruby library for easily formatting tables.
|
4
|
+
|
5
|
+
* simpletable on RubyGems: https://rubygems.org/gems/simpletable
|
6
|
+
|
7
|
+
Installation
|
8
|
+
----
|
9
|
+
```
|
10
|
+
gem install simpletable
|
11
|
+
```
|
12
|
+
|
13
|
+
Basic Usage
|
14
|
+
-----
|
15
|
+
See [lib/test.rb](lib/test.rb) for usage samples.
|
16
|
+
|
17
|
+
Development
|
18
|
+
-----
|
19
|
+
#### Build:
|
20
|
+
```
|
21
|
+
rake build
|
22
|
+
```
|
23
|
+
|
24
|
+
#### Install:
|
25
|
+
```
|
26
|
+
rake install
|
27
|
+
```
|
2
28
|
|
3
|
-
TODO: Write a gem description
|
4
|
-
|
5
|
-
## Installation
|
6
|
-
|
7
|
-
Add this line to your application's Gemfile:
|
8
|
-
|
9
|
-
gem 'simpletable'
|
10
|
-
|
11
|
-
And then execute:
|
12
|
-
|
13
|
-
$ bundle
|
14
|
-
|
15
|
-
Or install it yourself as:
|
16
|
-
|
17
|
-
$ gem install simpletable
|
18
|
-
|
19
|
-
## Usage
|
20
|
-
|
21
|
-
TODO: Write usage instructions here
|
22
|
-
|
23
|
-
## Contributing
|
24
|
-
|
25
|
-
1. Fork it
|
26
|
-
2. Create your feature branch (`git checkout -b my-new-feature`)
|
27
|
-
3. Commit your changes (`git commit -am 'Add some feature'`)
|
28
|
-
4. Push to the branch (`git push origin my-new-feature`)
|
29
|
-
5. Create new Pull Request
|
data/lib/simpletable.rb
CHANGED
@@ -3,30 +3,50 @@ require "simpletable/version"
|
|
3
3
|
DEFAULT_DIVIDER = "="
|
4
4
|
DEFAULT_PADDING = 2
|
5
5
|
|
6
|
-
|
7
|
-
def
|
6
|
+
class SimpleTable
|
7
|
+
def from_objects( objects, titles, methods, options = {} )
|
8
8
|
raise "Mismatched number of methods and column titles" if titles.length != methods.length
|
9
|
+
@objects = objects
|
10
|
+
@titles = titles
|
11
|
+
@methods = methods
|
12
|
+
@divider = options[:divider] || DEFAULT_DIVIDER
|
13
|
+
@padding = options[:padding] || DEFAULT_PADDING
|
14
|
+
self
|
15
|
+
end
|
9
16
|
|
10
|
-
|
11
|
-
padding = options[:padding] || DEFAULT_PADDING
|
12
|
-
|
17
|
+
def print_text
|
13
18
|
widths = []
|
14
19
|
# calculate column widths
|
15
|
-
titles.zip(methods).each do |title,method|
|
16
|
-
widths << objects.collect { |o| o.send(method).to_s }.push(title).group_by(&:size).max.first + padding
|
20
|
+
@titles.zip(@methods).each do |title,method|
|
21
|
+
widths << @objects.collect { |o| o.send(method).to_s }.push(title).group_by(&:size).max.first + @padding
|
17
22
|
end
|
18
23
|
|
19
24
|
# print table header
|
20
|
-
print_row(titles,widths)
|
21
|
-
puts divider * (widths.inject(:+) - padding) # sum of column widths - padding
|
25
|
+
print_row(@titles,widths)
|
26
|
+
puts @divider * (widths.inject(:+) - @padding) # sum of column widths - padding
|
22
27
|
|
23
28
|
# print table body
|
24
|
-
objects.each do |o|
|
25
|
-
data = methods.collect{ |m| o.send(m) } # collect row data
|
29
|
+
@objects.each do |o|
|
30
|
+
data = @methods.collect{ |m| o.send(m) } # collect row data
|
26
31
|
print_row(data,widths)
|
27
32
|
end
|
28
33
|
end
|
29
34
|
|
35
|
+
def print_csv(separator=",")
|
36
|
+
# quote strings w/ embedded separator characters
|
37
|
+
titles = []
|
38
|
+
@titles.each {|t| titles << (t.include?(separator) ? t.gsub(t,"\"#{t}\"") : t)}
|
39
|
+
|
40
|
+
# print table header
|
41
|
+
puts titles.join separator
|
42
|
+
|
43
|
+
# print table body
|
44
|
+
@objects.each do |o|
|
45
|
+
data = @methods.collect{ |m| o.send(m) } # collect row data
|
46
|
+
puts data.join separator
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
30
50
|
private
|
31
51
|
def print_row(data,widths)
|
32
52
|
data.zip(widths).each { |d,w| print d.to_s.ljust(w) }
|
data/lib/simpletable/version.rb
CHANGED
@@ -1,3 +1,3 @@
|
|
1
|
-
module
|
2
|
-
VERSION = "0.
|
1
|
+
module SimpleTableConst
|
2
|
+
VERSION = "0.2.0"
|
3
3
|
end
|
data/lib/test.rb
CHANGED
@@ -11,10 +11,8 @@ class TestObject
|
|
11
11
|
end
|
12
12
|
end
|
13
13
|
|
14
|
-
include SimpleTable
|
15
|
-
|
16
14
|
titles = [ "Title A", "Title B is very long, the longest", "Title C" ]
|
17
|
-
titles2 = [ "Title A", "Title B", "Title C is very long
|
15
|
+
titles2 = [ "Title A", "Title B", "Title C is very long; the longest" ]
|
18
16
|
methods = [ :p1, :p2, :p3 ]
|
19
17
|
|
20
18
|
objs = []
|
@@ -24,7 +22,24 @@ objs << TestObject.new("eleven","twelve","thirteen")
|
|
24
22
|
objs << TestObject.new("fourteen","fifteen","sixteen")
|
25
23
|
objs << TestObject.new("thisisthebiggestword","thisisthebiggestword","thisisthebiggestword")
|
26
24
|
|
27
|
-
SimpleTable.
|
28
|
-
|
29
|
-
|
30
|
-
|
25
|
+
t = SimpleTable.new
|
26
|
+
|
27
|
+
t.from_objects(objs,titles,methods).print_text
|
28
|
+
print "\n"
|
29
|
+
t.print_csv
|
30
|
+
print "\n"
|
31
|
+
|
32
|
+
t.from_objects(objs,titles,methods,{:divider=>"*"}).print_text
|
33
|
+
print "\n"
|
34
|
+
t.print_csv
|
35
|
+
print "\n"
|
36
|
+
|
37
|
+
t.from_objects(objs,titles,methods,{:divider=>"-",:padding=>5}).print_text
|
38
|
+
print "\n"
|
39
|
+
t.print_csv
|
40
|
+
print "\n"
|
41
|
+
|
42
|
+
t.from_objects(objs,titles2,methods,{:divider=>"-",:padding=>5}).print_text
|
43
|
+
print "\n"
|
44
|
+
t.print_csv(";")
|
45
|
+
print "\n"
|
data/simpletable.gemspec
CHANGED
@@ -5,7 +5,7 @@ require 'simpletable/version'
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |spec|
|
7
7
|
spec.name = "simpletable"
|
8
|
-
spec.version =
|
8
|
+
spec.version = SimpleTableConst::VERSION
|
9
9
|
spec.authors = ["Brady Turner"]
|
10
10
|
spec.email = ["bradyaturner@gmail.com"]
|
11
11
|
spec.description = %q{Create and format tables}
|