table_print 1.1.4 → 1.1.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -3,6 +3,7 @@ rvm:
3
3
  - 1.8.7
4
4
  - 1.9.2
5
5
  - 1.9.3
6
+ - 2.0.0
6
7
  - jruby-18mode
7
8
  - jruby-19mode
8
9
  - rbx-18mode
@@ -1,4 +1,4 @@
1
- Copyright (c) 2012 Chris Doyle
1
+ Copyright (c) 2013 Chris Doyle
2
2
 
3
3
  Permission is hereby granted, free of charge, to any person obtaining
4
4
  a copy of this software and associated documentation files (the
@@ -1,12 +1,14 @@
1
1
  = table_print
2
2
 
3
- {<img src="https://secure.travis-ci.org/arches/table_print.png" />}[http://travis-ci.org/arches/table_print]
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
- Turn objects into nicely formatted columns for easy reading
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) 2012 Chris Doyle. See LICENSE.txt for further details.
167
+ Copyright (c) 2013 Chris Doyle. See LICENSE.txt for further details.
165
168
 
@@ -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(Time.now - start) # we have to return *something*, might as well be execution time.
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
@@ -1,4 +1,4 @@
1
1
  module TablePrint
2
- VERSION = "1.1.4"
2
+ VERSION = "1.1.5"
3
3
  end
4
4
 
@@ -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
@@ -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
- it "pulls the column names off the array of hashes" do
39
- data = [{:name => "User 1",
40
- :surname => "Familyname 1"
41
- },
42
- {:name => "User 2",
43
- :surname => "Familyname 2"}]
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
- p = Printer.new(data)
46
- cols = p.columns
47
- cols.length.should == 2
48
- cols.collect(&:name).sort.should == ['name', 'surname']
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
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-03-04 00:00:00.000000000Z
12
+ date: 2013-08-19 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: cat