simpletable 0.0.2 → 0.1.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 CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- NzUwNTRjNWY2NThhYWY2ODE3NjRiMTk2MDhkMjczODQwZmE5YTMxZg==
4
+ NTI5Njg4MTBiNjJjNzcyYWY3OTk0ZDAzMDk2MGVhZDRmZGY0MTRjZA==
5
5
  data.tar.gz: !binary |-
6
- ZWZiMDdmZjQyMmRkMzk2MjEyMzI4Y2ExMWNiMjJhYzNmNWE1ODdkNQ==
6
+ NDNjNDE0YWE1NmU1ZmQwNGNhNmU4NzVjNDNmNmRlZjY5MDJjZGYxZQ==
7
7
  !binary "U0hBNTEy":
8
8
  metadata.gz: !binary |-
9
- YjEzOTc5MzI2M2I1OTQ0NTIwNzg0MTUxNzA2MTRlMWE2MmVmZjUxYmNmYjQ3
10
- YzBiYzJmODgzM2E2ZjRlNzExYzdkNDQwYTk3Nzk0MmFhNjJiZGM3OGY3NjYw
11
- ZTAxODY2ZjY4NGEzODE1ZmY5MjFhZmY3ZGFhODZiNGQ3N2E4OGQ=
9
+ ZTIxZDU2MTllYjEwOWFkMjhhMTUyOTA3MjA1OGY2MjBhN2VhYzVlZmQ0NDcx
10
+ NjQxZGY3YzczNWU3M2RjZjEzYzVlZjRlNWZjYjYxMTFkMjc5ZmU5YmZiN2M0
11
+ OTQ3YTNiY2I4ZTA3NDc4NWM1OGRjZmRiNzAzMGU3MjkwZWIxYTI=
12
12
  data.tar.gz: !binary |-
13
- M2Y1YjRmNDIwYzhhODI0YzFhMDNkOWY0NzlmZjM0MGU2ZDdkMDU0NDY3NTI4
14
- YWQ4YzM1MmJkYTdiY2QyNjUwMGY1MWUzNDIyNjI3NDU3MGRjNTQxMjc3NWU1
15
- MzBjODY4Zjk1NmYyMTFmODdjNGI2NDIyNGE4N2Q1NWFmMjI4ZDY=
13
+ MWI3NjhiNDY4ZjJiODNmNDQyZjVkYmNkZTE0OTBhNzQ2MmUyY2ZlYzMyYWY4
14
+ YmFjNDI3Yzg0ZmRjNWU5N2FlOTg3MDg5Y2I4NGNjODRjNTJmMTE4ZGFjYmY4
15
+ ZmFhYzNlZjBlNTYwYTk4NDAzMzUyMGQwOGQ1MjIxYjcyZTMyZDE=
data/lib/simpletable.rb CHANGED
@@ -1,5 +1,35 @@
1
1
  require "simpletable/version"
2
2
 
3
+ DEFAULT_DIVIDER = "="
4
+ DEFAULT_PADDING = 2
5
+
3
6
  module SimpleTable
4
- # Your code goes here...
7
+ def create( objects, titles, methods, options = {} )
8
+ raise "Mismatched number of methods and column titles" if titles.length != methods.length
9
+
10
+ divider = options[:divider] || DEFAULT_DIVIDER
11
+ padding = options[:padding] || DEFAULT_PADDING
12
+
13
+ widths = []
14
+ # 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
17
+ end
18
+
19
+ # print table header
20
+ print_row(titles,widths)
21
+ puts divider * (widths.inject(:+) - padding) # sum of column widths - padding
22
+
23
+ # print table body
24
+ objects.each do |o|
25
+ data = methods.collect{ |m| o.send(m) } # collect row data
26
+ print_row(data,widths)
27
+ end
28
+ end
29
+
30
+ private
31
+ def print_row(data,widths)
32
+ data.zip(widths).each { |d,w| print d.ljust(w) }
33
+ print "\n"
34
+ end
5
35
  end
@@ -1,3 +1,3 @@
1
1
  module SimpleTable
2
- VERSION = "0.0.2"
2
+ VERSION = "0.1.0"
3
3
  end
data/lib/test.rb ADDED
@@ -0,0 +1,30 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require './simpletable'
4
+
5
+ class TestObject
6
+ attr_reader :p1, :p2, :p3
7
+ def initialize(p1,p2,p3)
8
+ @p1 = p1
9
+ @p2 = p2
10
+ @p3 = p3
11
+ end
12
+ end
13
+
14
+ include SimpleTable
15
+
16
+ titles = [ "Title A", "Title B is very long, the longest", "Title C" ]
17
+ titles2 = [ "Title A", "Title B", "Title C is very long, the longest" ]
18
+ methods = [ :p1, :p2, :p3 ]
19
+
20
+ objs = []
21
+ objs << TestObject.new("thisisaword","anotherword","short")
22
+ objs << TestObject.new("also","kindof","longerword")
23
+ objs << TestObject.new("eleven","twelve","thirteen")
24
+ objs << TestObject.new("fourteen","fifteen","sixteen")
25
+ objs << TestObject.new("thisisthebiggestword","thisisthebiggestword","thisisthebiggestword")
26
+
27
+ SimpleTable.create(objs,titles,methods)
28
+ SimpleTable.create(objs,titles,methods,{:divider=>"*"})
29
+ SimpleTable.create(objs,titles,methods,{:divider=>"-",:padding=>5})
30
+ SimpleTable.create(objs,titles2,methods,{:divider=>"-",:padding=>5})
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: simpletable
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Brady Turner
@@ -53,6 +53,7 @@ files:
53
53
  - Rakefile
54
54
  - lib/simpletable.rb
55
55
  - lib/simpletable/version.rb
56
+ - lib/test.rb
56
57
  - simpletable.gemspec
57
58
  homepage: ''
58
59
  licenses: