table_print 1.1.4 → 1.1.5
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.
- data/.travis.yml +1 -0
- data/LICENSE.txt +1 -1
- data/README.rdoc +9 -6
- data/lib/table_print.rb +12 -2
- data/lib/table_print/fingerprinter.rb +3 -1
- data/lib/table_print/version.rb +1 -1
- data/spec/fingerprinter_spec.rb +18 -0
- data/spec/table_print_spec.rb +45 -13
- metadata +2 -2
data/.travis.yml
CHANGED
data/LICENSE.txt
CHANGED
data/README.rdoc
CHANGED
@@ -1,12 +1,14 @@
|
|
1
1
|
= table_print
|
2
2
|
|
3
|
-
{<img src="https://
|
3
|
+
{<img src="https://travis-ci.org/arches/table_print.png?branch=master" />}[http://travis-ci.org/arches/table_print]
|
4
|
+
{<img src="https://codeclimate.com/github/arches/table_print.png" />}[https://codeclimate.com/github/arches/table_print]
|
4
5
|
|
6
|
+
TablePrint shows objects in nicely formatted columns for easy reading. It even lets you nest other tables
|
7
|
+
of related objects, contextualizing data across tables. It's incredibly flexible, yet simple, making it easy to
|
8
|
+
see exactly the data you care about.
|
5
9
|
|
6
|
-
|
7
|
-
|
8
|
-
TablePrint formats an object or array of objects into columns for easy reading. To do this,
|
9
|
-
it assumes the objects in your array all respond to the same methods.
|
10
|
+
This {three minute screencast}[http://tableprintgem.com] will give you a quick overview of table_print's most
|
11
|
+
powerful features and how to use them.
|
10
12
|
|
11
13
|
== Installation
|
12
14
|
|
@@ -75,6 +77,7 @@ Pass options to individual columns through the options hash by using the display
|
|
75
77
|
a skinny email column, set the width explicitly:
|
76
78
|
|
77
79
|
tp User.all, :email => {:width => 12}
|
80
|
+
tp User.all, :id, {:email => {:width => 12}}, :age
|
78
81
|
|
79
82
|
Available column options:
|
80
83
|
|
@@ -161,5 +164,5 @@ You can also set global options:
|
|
161
164
|
|
162
165
|
== Copyright
|
163
166
|
|
164
|
-
Copyright (c)
|
167
|
+
Copyright (c) 2013 Chris Doyle. See LICENSE.txt for further details.
|
165
168
|
|
data/lib/table_print.rb
CHANGED
@@ -21,6 +21,7 @@ module TablePrint
|
|
21
21
|
@data = [data].flatten.compact
|
22
22
|
@options = options
|
23
23
|
@columns = nil
|
24
|
+
@start_time = Time.now
|
24
25
|
end
|
25
26
|
|
26
27
|
def table_print
|
@@ -41,6 +42,16 @@ module TablePrint
|
|
41
42
|
[group.header, group.horizontal_separator, group.format].join("\n")
|
42
43
|
end
|
43
44
|
|
45
|
+
def message
|
46
|
+
return "Printed with config" if configged?
|
47
|
+
Time.now - @start_time
|
48
|
+
end
|
49
|
+
|
50
|
+
private
|
51
|
+
def configged?
|
52
|
+
!!Config.for(@data.first.class)
|
53
|
+
end
|
54
|
+
|
44
55
|
def columns
|
45
56
|
return @columns if @columns
|
46
57
|
defaults = TablePrint::Printable.default_display_methods(@data.first)
|
@@ -51,8 +62,7 @@ module TablePrint
|
|
51
62
|
end
|
52
63
|
|
53
64
|
def tp(data=Class, *options)
|
54
|
-
start = Time.now
|
55
65
|
printer = TablePrint::Printer.new(data, options)
|
56
66
|
puts printer.table_print unless data.is_a? Class
|
57
|
-
TablePrint::Returnable.new(
|
67
|
+
TablePrint::Returnable.new(printer.message) # we have to return *something*, might as well be execution time.
|
58
68
|
end
|
@@ -36,8 +36,10 @@ module TablePrint
|
|
36
36
|
display_method = (prefix == "" ? method : "#{prefix}.#{method}")
|
37
37
|
if method.is_a? Proc
|
38
38
|
cell_value = method.call(target)
|
39
|
-
elsif target.is_a? Hash and target.keys.include? method
|
39
|
+
elsif target.is_a? Hash and target.keys.include? method.to_sym
|
40
40
|
cell_value = target[method.to_sym]
|
41
|
+
elsif target.is_a? Hash and target.keys.include? method
|
42
|
+
cell_value = target[method]
|
41
43
|
else
|
42
44
|
cell_value ||= target.send(method)
|
43
45
|
end
|
data/lib/table_print/version.rb
CHANGED
data/spec/fingerprinter_spec.rb
CHANGED
@@ -112,6 +112,24 @@ describe Fingerprinter do
|
|
112
112
|
row = f.populate_row("bar", {'title' => {}, 'author' => {}, 'publisher' => {'address' => {}}}, OpenStruct.new(:title => "foobar", :author => "bobby"))
|
113
113
|
row.cells.should == {'title' => "foobar", 'bar.author' => 'bobby'}
|
114
114
|
end
|
115
|
+
|
116
|
+
context 'using a hash as input_data' do
|
117
|
+
it "fills a row by calling methods on the target object" do
|
118
|
+
f = Fingerprinter.new
|
119
|
+
f.instance_variable_set('@column_names_by_display_method', {'title' => 'title', 'author' => 'author'})
|
120
|
+
input_data = {:title => 'foobar', :author => 'bobby'}
|
121
|
+
row = f.populate_row('', {'title' => {}, 'author' => {}, 'publisher' => {'address' => {}}}, input_data)
|
122
|
+
row.cells.should == {'title' => 'foobar', 'author' => 'bobby'}
|
123
|
+
end
|
124
|
+
|
125
|
+
it "fills a row by calling methods on the target object" do
|
126
|
+
f = Fingerprinter.new
|
127
|
+
f.instance_variable_set('@column_names_by_display_method', {'title' => 'title', 'author' => 'author'})
|
128
|
+
input_data = {'title' => 'foobar', 'author' => 'bobby'}
|
129
|
+
row = f.populate_row('', {'title' => {}, 'author' => {}, 'publisher' => {'address' => {}}}, input_data)
|
130
|
+
row.cells.should == {'title' => 'foobar', 'author' => 'bobby'}
|
131
|
+
end
|
132
|
+
end
|
115
133
|
end
|
116
134
|
|
117
135
|
describe "#create_child_group" do
|
data/spec/table_print_spec.rb
CHANGED
@@ -24,28 +24,60 @@ describe TablePrint::Printer do
|
|
24
24
|
end
|
25
25
|
end
|
26
26
|
|
27
|
+
describe "message" do
|
28
|
+
it "defaults to the time the print took" do
|
29
|
+
Printer.new([]).message.should be_a Numeric
|
30
|
+
end
|
31
|
+
|
32
|
+
it "shows a warning if the printed objects have config" do
|
33
|
+
Sandbox.add_class("User")
|
34
|
+
|
35
|
+
tp.set Sandbox::User, :id, :email
|
36
|
+
p = Printer.new(Sandbox::User.new)
|
37
|
+
p.message.should == "Printed with config"
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
27
41
|
describe "#columns" do
|
42
|
+
|
28
43
|
it "pulls the column names off the data object" do
|
29
44
|
Sandbox.add_class("Post")
|
30
45
|
Sandbox.add_attributes("Post", :title)
|
31
46
|
|
32
47
|
p = Printer.new(Sandbox::Post.new)
|
33
|
-
cols = p.columns
|
48
|
+
cols = p.send(:columns)
|
34
49
|
cols.length.should == 1
|
35
50
|
cols.first.name.should == 'title'
|
36
51
|
end
|
37
52
|
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
53
|
+
context 'when keys are symbols' do
|
54
|
+
it "pulls the column names off the array of hashes" do
|
55
|
+
data = [{:name => "User 1",
|
56
|
+
:surname => "Familyname 1"
|
57
|
+
},
|
58
|
+
{:name => "User 2",
|
59
|
+
:surname => "Familyname 2"}]
|
44
60
|
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
61
|
+
p = Printer.new(data)
|
62
|
+
cols = p.send(:columns)
|
63
|
+
cols.length.should == 2
|
64
|
+
cols.collect(&:name).sort.should == ['name', 'surname']
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
context 'when keys are strings' do
|
69
|
+
it "pulls the column names off the array of hashes" do
|
70
|
+
data = [{'name' => "User 1",
|
71
|
+
'surname' => "Familyname 1"
|
72
|
+
},
|
73
|
+
{'name' => "User 2",
|
74
|
+
'surname' => "Familyname 2"}]
|
75
|
+
|
76
|
+
p = Printer.new(data)
|
77
|
+
cols = p.send(:columns)
|
78
|
+
cols.length.should == 2
|
79
|
+
cols.collect(&:name).sort.should == ['name', 'surname']
|
80
|
+
end
|
49
81
|
end
|
50
82
|
|
51
83
|
it "pulls out excepted columns" do
|
@@ -53,7 +85,7 @@ describe TablePrint::Printer do
|
|
53
85
|
Sandbox.add_attributes("Post", :title, :author)
|
54
86
|
|
55
87
|
p = Printer.new(Sandbox::Post.new, :except => :title)
|
56
|
-
cols = p.columns
|
88
|
+
cols = p.send(:columns)
|
57
89
|
cols.length.should == 1
|
58
90
|
cols.first.name.should == 'author'
|
59
91
|
end
|
@@ -63,7 +95,7 @@ describe TablePrint::Printer do
|
|
63
95
|
Sandbox.add_attributes("Post", :title)
|
64
96
|
|
65
97
|
p = Printer.new(Sandbox::Post.new, :include => :author)
|
66
|
-
cols = p.columns
|
98
|
+
cols = p.send(:columns)
|
67
99
|
cols.length.should == 2
|
68
100
|
cols.first.name.should == 'title'
|
69
101
|
cols.last.name.should == 'author'
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: table_print
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.1.
|
4
|
+
version: 1.1.5
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-
|
12
|
+
date: 2013-08-19 00:00:00.000000000Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: cat
|