tablecon 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/tablecon.rb +153 -0
- metadata +46 -0
data/lib/tablecon.rb
ADDED
@@ -0,0 +1,153 @@
|
|
1
|
+
|
2
|
+
module Tablecon
|
3
|
+
|
4
|
+
class Table
|
5
|
+
|
6
|
+
##
|
7
|
+
# Params:
|
8
|
+
# +list+:: a list of instances of the same class
|
9
|
+
# where every field will be treated as a
|
10
|
+
# separate column
|
11
|
+
def initialize list
|
12
|
+
lp = ListParser.new list
|
13
|
+
@printer = TablePrinter.new lp.rows
|
14
|
+
end
|
15
|
+
|
16
|
+
##
|
17
|
+
# prints @list as a table
|
18
|
+
def to_s
|
19
|
+
@printer.str
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
class TablePrinter
|
24
|
+
def initialize rows
|
25
|
+
@rows = rows
|
26
|
+
raise "Rows are empty!" if @rows.empty?
|
27
|
+
end
|
28
|
+
|
29
|
+
def str
|
30
|
+
ret = ""
|
31
|
+
ret << header
|
32
|
+
@rows.each do |row|
|
33
|
+
row.each do |cell|
|
34
|
+
ret << "|"
|
35
|
+
ret << cell.value.to_s.center(cell.width + 2)
|
36
|
+
end
|
37
|
+
ret << "|\n"
|
38
|
+
ret << delimiter
|
39
|
+
end
|
40
|
+
ret
|
41
|
+
end
|
42
|
+
|
43
|
+
private
|
44
|
+
def header
|
45
|
+
str = ""
|
46
|
+
# line 1
|
47
|
+
str << delimiter
|
48
|
+
# line 2
|
49
|
+
@rows.first.each do |cell|
|
50
|
+
str << "|"
|
51
|
+
str << cell.column.name.to_s.center(cell.width + 2)
|
52
|
+
end
|
53
|
+
str << "|\n"
|
54
|
+
# line 3
|
55
|
+
str << delimiter
|
56
|
+
end
|
57
|
+
|
58
|
+
def delimiter
|
59
|
+
str = ""
|
60
|
+
@rows.first.each do |cell|
|
61
|
+
str << "+"
|
62
|
+
str << "-".center(cell.width + 2, "-")
|
63
|
+
end
|
64
|
+
str << "+\n"
|
65
|
+
end
|
66
|
+
|
67
|
+
end
|
68
|
+
|
69
|
+
class ListParser
|
70
|
+
attr_accessor :columns, :rows
|
71
|
+
|
72
|
+
def initialize list
|
73
|
+
@columns = {}
|
74
|
+
@rows = []
|
75
|
+
parse list
|
76
|
+
end
|
77
|
+
|
78
|
+
private
|
79
|
+
|
80
|
+
##
|
81
|
+
# parse list and fill in rows and columns
|
82
|
+
def parse list
|
83
|
+
raise "list is empty!" if list.empty?
|
84
|
+
first = list[0]
|
85
|
+
init_columns first
|
86
|
+
|
87
|
+
list.each do |object|
|
88
|
+
vars = object.instance_variables
|
89
|
+
|
90
|
+
if vars != first.instance_variables
|
91
|
+
raise "list not consistent, expect to have the following columns: "
|
92
|
+
+ first.instance_variables + " but got " + vars
|
93
|
+
end
|
94
|
+
|
95
|
+
row = Row.new
|
96
|
+
@rows << row
|
97
|
+
vars.each do |var|
|
98
|
+
value = "#{object.instance_variable_get var}"
|
99
|
+
col = @columns[var]
|
100
|
+
raise "Cannot find column for #{col}" unless col
|
101
|
+
row << Cell.new(col, value)
|
102
|
+
col.length = value.length if value.length > col.length
|
103
|
+
end
|
104
|
+
end
|
105
|
+
end
|
106
|
+
|
107
|
+
##
|
108
|
+
# populate only the names
|
109
|
+
def init_columns object
|
110
|
+
object.instance_variables.each do |var|
|
111
|
+
name = var.to_s[1..-1].capitalize
|
112
|
+
@columns[var] = Column.new name, name.length
|
113
|
+
end
|
114
|
+
end
|
115
|
+
end
|
116
|
+
|
117
|
+
class Cell
|
118
|
+
attr_accessor :column, :value
|
119
|
+
def initialize column, value
|
120
|
+
@column = column
|
121
|
+
@value = value
|
122
|
+
end
|
123
|
+
def width
|
124
|
+
@column.length
|
125
|
+
end
|
126
|
+
end
|
127
|
+
|
128
|
+
class Column
|
129
|
+
attr_accessor :name, :length
|
130
|
+
def initialize name, length
|
131
|
+
@name = name
|
132
|
+
@length = length
|
133
|
+
end
|
134
|
+
def == col
|
135
|
+
@name == col.name and @length == col.length
|
136
|
+
end
|
137
|
+
def to_s
|
138
|
+
"Column[name=#{@name} length=#{@length}]"
|
139
|
+
end
|
140
|
+
end
|
141
|
+
|
142
|
+
class Row
|
143
|
+
def initialize
|
144
|
+
@cells = []
|
145
|
+
end
|
146
|
+
def << cell
|
147
|
+
@cells << cell
|
148
|
+
end
|
149
|
+
def each
|
150
|
+
@cells.each { |cell| yield cell }
|
151
|
+
end
|
152
|
+
end
|
153
|
+
end
|
metadata
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: tablecon
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- jdin
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-07-24 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: A simple gem to print an object as a table to console
|
15
|
+
email: jdinosaurus@gmail.com
|
16
|
+
executables: []
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- lib/tablecon.rb
|
21
|
+
homepage: http://rubygems.org/gems/tablecon
|
22
|
+
licenses: []
|
23
|
+
post_install_message:
|
24
|
+
rdoc_options: []
|
25
|
+
require_paths:
|
26
|
+
- lib
|
27
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ! '>='
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '0'
|
33
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
34
|
+
none: false
|
35
|
+
requirements:
|
36
|
+
- - ! '>='
|
37
|
+
- !ruby/object:Gem::Version
|
38
|
+
version: '0'
|
39
|
+
requirements: []
|
40
|
+
rubyforge_project:
|
41
|
+
rubygems_version: 1.8.24
|
42
|
+
signing_key:
|
43
|
+
specification_version: 3
|
44
|
+
summary: Print an object as a table
|
45
|
+
test_files: []
|
46
|
+
has_rdoc:
|