cldwalker-hirb 0.2.1 → 0.2.2
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/CHANGELOG.rdoc +6 -0
- data/README.rdoc +3 -0
- data/VERSION.yml +1 -1
- data/lib/hirb/helpers.rb +1 -1
- data/lib/hirb/helpers/active_record_table.rb +10 -0
- data/lib/hirb/helpers/auto_table.rb +1 -0
- data/lib/hirb/helpers/table.rb +17 -4
- data/lib/hirb/helpers/vertical_table.rb +31 -0
- data/test/active_record_table_test.rb +35 -0
- data/test/auto_table_test.rb +20 -0
- data/test/object_table_test.rb +49 -0
- data/test/table_test.rb +16 -65
- data/test/test_helper.rb +11 -0
- metadata +9 -2
data/CHANGELOG.rdoc
CHANGED
@@ -1,3 +1,9 @@
|
|
1
|
+
== 0.2.2
|
2
|
+
* Added a friendlier default (a vertical table) to incorrectly configured tables.
|
3
|
+
* Added vertical table helper thanks to chrononaut.
|
4
|
+
* Added detection of :select option from ActiveRecord queries in ActiveRecordTable helper.
|
5
|
+
* Added handling anything that responds to :to_a in AutoTable helper.
|
6
|
+
|
1
7
|
== 0.2.1
|
2
8
|
* Fixed typo in Hirb::Console.view
|
3
9
|
|
data/README.rdoc
CHANGED
@@ -149,6 +149,9 @@ If using Wirble, you should call Hirb after it since they both override irb's de
|
|
149
149
|
Table code from http://gist.github.com/72234 and {my console
|
150
150
|
app's needs}[http://github.com/cldwalker/tag-tree].
|
151
151
|
|
152
|
+
== Credits
|
153
|
+
Chrononaut for vertical table helper.
|
154
|
+
|
152
155
|
== Bugs/Issues
|
153
156
|
Please report them {on github}[http://github.com/cldwalker/hirb/issues].
|
154
157
|
|
data/VERSION.yml
CHANGED
data/lib/hirb/helpers.rb
CHANGED
@@ -2,6 +2,6 @@ module Hirb
|
|
2
2
|
module Helpers #:nodoc:
|
3
3
|
end
|
4
4
|
end
|
5
|
-
%w{table object_table active_record_table auto_table tree parent_child_tree}.each do |e|
|
5
|
+
%w{table object_table active_record_table auto_table tree parent_child_tree vertical_table}.each do |e|
|
6
6
|
require "hirb/helpers/#{e}"
|
7
7
|
end
|
@@ -11,6 +11,16 @@ class Hirb::Helpers::ActiveRecordTable < Hirb::Helpers::ObjectTable
|
|
11
11
|
fields = rows.first.class.column_names
|
12
12
|
fields.map {|e| e.to_sym }
|
13
13
|
end
|
14
|
+
if query_used_select?(rows)
|
15
|
+
selected_columns = rows.first.attributes.keys
|
16
|
+
sorted_columns = rows.first.class.column_names.dup.delete_if {|e| !selected_columns.include?(e) }
|
17
|
+
sorted_columns += (selected_columns - sorted_columns)
|
18
|
+
options[:fields] = sorted_columns.map {|e| e.to_sym}
|
19
|
+
end
|
14
20
|
super(rows, options)
|
15
21
|
end
|
22
|
+
|
23
|
+
def self.query_used_select?(rows) #:nodoc:
|
24
|
+
rows.first.attributes.keys.sort != rows.first.class.column_names.sort
|
25
|
+
end
|
16
26
|
end
|
@@ -2,6 +2,7 @@
|
|
2
2
|
class Hirb::Helpers::AutoTable
|
3
3
|
# Same options as Hirb::Helpers::Table.render.
|
4
4
|
def self.render(output, options={})
|
5
|
+
output = output.to_a if !output.is_a?(Array) && output.respond_to?(:to_a)
|
5
6
|
klass = if ((output.is_a?(Array) && output[0].is_a?(ActiveRecord::Base)) or output.is_a?(ActiveRecord::Base) rescue false)
|
6
7
|
Hirb::Helpers::ActiveRecordTable
|
7
8
|
elsif (output.is_a?(Array) && !(output[0].is_a?(Hash) || output[0].is_a?(Array)))
|
data/lib/hirb/helpers/table.rb
CHANGED
@@ -43,6 +43,7 @@ class Hirb::Helpers::Table
|
|
43
43
|
# [:number] When set to true, numbers rows by adding a :hirb_number column as the first column. Default is false.
|
44
44
|
# [:filters] A hash of fields and the filters that each row in the field must run through. The filter converts the cell's value by applying
|
45
45
|
# a given proc or an array containing a method and optional arguments to it.
|
46
|
+
# [:vertical] When set to true, renders a vertical table using Hirb::Helpers::VerticalTable. Default is false.
|
46
47
|
# Examples:
|
47
48
|
# Hirb::Helpers::Table.render [[1,2], [2,3]]
|
48
49
|
# Hirb::Helpers::Table.render [[1,2], [2,3]], :field_lengths=>{0=>10}
|
@@ -50,7 +51,11 @@ class Hirb::Helpers::Table
|
|
50
51
|
# Hirb::Helpers::Table.render [{:age=>10, :weight=>100}, {:age=>80, :weight=>500}], :headers=>{:weight=>"Weight(lbs)"}
|
51
52
|
# Hirb::Helpers::Table.render [{:age=>10, :weight=>100}, {:age=>80, :weight=>500}], :filters=>{:age=>[:to_f]}
|
52
53
|
def render(rows, options={})
|
53
|
-
new(rows,options).render
|
54
|
+
options.delete(:vertical) ? Hirb::Helpers::VerticalTable.render(rows, options) : new(rows, options).render
|
55
|
+
rescue TooManyFieldsForWidthError
|
56
|
+
$stderr.puts "", "** Error: Too many fields for the current width. Configure your width " +
|
57
|
+
"and/or fields to avoid this error. Defaulting to a vertical table. **"
|
58
|
+
Hirb::Helpers::VerticalTable.render(rows, options)
|
54
59
|
end
|
55
60
|
end
|
56
61
|
|
@@ -90,15 +95,23 @@ class Hirb::Helpers::Table
|
|
90
95
|
body = []
|
91
96
|
unless @rows.length == 0
|
92
97
|
setup_field_lengths
|
93
|
-
body +=
|
98
|
+
body += render_header
|
94
99
|
body += render_rows
|
95
|
-
body
|
100
|
+
body += render_footer
|
96
101
|
end
|
97
102
|
body << render_table_description
|
98
103
|
body.join("\n")
|
99
104
|
end
|
100
|
-
|
105
|
+
|
101
106
|
def render_header
|
107
|
+
@headers ? render_table_header : [render_border]
|
108
|
+
end
|
109
|
+
|
110
|
+
def render_footer
|
111
|
+
[render_border]
|
112
|
+
end
|
113
|
+
|
114
|
+
def render_table_header
|
102
115
|
title_row = '| ' + @fields.map {|f|
|
103
116
|
format_cell(@headers[f], @field_lengths[f])
|
104
117
|
}.join(' | ') + ' |'
|
@@ -0,0 +1,31 @@
|
|
1
|
+
class Hirb::Helpers::VerticalTable < Hirb::Helpers::Table
|
2
|
+
|
3
|
+
# Renders a vertical table using the same options as Hirb::Helpers::Table.render except for :field_lengths,
|
4
|
+
# :vertical and :max_width which aren't used.
|
5
|
+
def self.render(rows, options={})
|
6
|
+
new(rows, options).render
|
7
|
+
end
|
8
|
+
|
9
|
+
#:stopdoc:
|
10
|
+
def setup_field_lengths
|
11
|
+
@field_lengths = default_field_lengths
|
12
|
+
end
|
13
|
+
|
14
|
+
def render_header; []; end
|
15
|
+
def render_footer; []; end
|
16
|
+
|
17
|
+
def render_rows
|
18
|
+
i = 0
|
19
|
+
longest_header = @headers.values.sort_by {|e| e.length}.last.length
|
20
|
+
stars = "*" * [(longest_header + (longest_header / 2)), 3].max
|
21
|
+
@rows.map do |row|
|
22
|
+
row = "#{stars} #{i+1}. row #{stars}\n" +
|
23
|
+
@fields.map {|f|
|
24
|
+
"#{@headers[f].rjust(longest_header)}: #{row[f]}"
|
25
|
+
}.join("\n")
|
26
|
+
i+= 1
|
27
|
+
row
|
28
|
+
end
|
29
|
+
#:startdoc:
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), 'test_helper')
|
2
|
+
|
3
|
+
class Hirb::Helpers::ActiveRecordTableTest < Test::Unit::TestCase
|
4
|
+
context "activerecord table" do
|
5
|
+
test "with no select renders" do
|
6
|
+
expected_table = <<-TABLE.unindent
|
7
|
+
+-----+-------+
|
8
|
+
| age | name |
|
9
|
+
+-----+-------+
|
10
|
+
| 7 | rufus |
|
11
|
+
| 101 | alf |
|
12
|
+
+-----+-------+
|
13
|
+
2 rows in set
|
14
|
+
TABLE
|
15
|
+
@pets = [stub(:name=>'rufus', :age=>7, :attributes=>{"name"=>'rufus', 'age'=>7}, :class=>stub(:column_names=>%w{age name})),
|
16
|
+
stub(:name=>'alf', :age=>101)]
|
17
|
+
Hirb::Helpers::ActiveRecordTable.render(@pets).should == expected_table
|
18
|
+
end
|
19
|
+
|
20
|
+
test "with select renders" do
|
21
|
+
expected_table = <<-TABLE.unindent
|
22
|
+
+-------+
|
23
|
+
| name |
|
24
|
+
+-------+
|
25
|
+
| rufus |
|
26
|
+
| alf |
|
27
|
+
+-------+
|
28
|
+
2 rows in set
|
29
|
+
TABLE
|
30
|
+
@pets = [stub(:name=>'rufus', :age=>7, :attributes=>{'name'=>'rufus'}, :class=>stub(:column_names=>%w{age name})),
|
31
|
+
stub(:name=>'alf', :age=>101)]
|
32
|
+
Hirb::Helpers::ActiveRecordTable.render(@pets).should == expected_table
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), 'test_helper')
|
2
|
+
|
3
|
+
class Hirb::Helpers::AutoTableTest < Test::Unit::TestCase
|
4
|
+
context "auto table" do
|
5
|
+
test "converts nonarrays to arrays and renders" do
|
6
|
+
require 'set'
|
7
|
+
expected_table = <<-TABLE.unindent
|
8
|
+
+-------+
|
9
|
+
| value |
|
10
|
+
+-------+
|
11
|
+
| 1 |
|
12
|
+
| 2 |
|
13
|
+
| 3 |
|
14
|
+
+-------+
|
15
|
+
3 rows in set
|
16
|
+
TABLE
|
17
|
+
Hirb::Helpers::AutoTable.render(::Set.new([1,2,3])).should == expected_table
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,49 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), 'test_helper')
|
2
|
+
|
3
|
+
class Hirb::Helpers::ObjectTableTest < Test::Unit::TestCase
|
4
|
+
context "object table" do
|
5
|
+
before(:all) {
|
6
|
+
@pets = [stub(:name=>'rufus', :age=>7, :to_s=>'rufus'), stub(:name=>'alf', :age=>101, :to_s=>'alf')]
|
7
|
+
}
|
8
|
+
test "renders" do
|
9
|
+
expected_table = <<-TABLE.unindent
|
10
|
+
+-------+-----+
|
11
|
+
| name | age |
|
12
|
+
+-------+-----+
|
13
|
+
| rufus | 7 |
|
14
|
+
| alf | 101 |
|
15
|
+
+-------+-----+
|
16
|
+
2 rows in set
|
17
|
+
TABLE
|
18
|
+
Hirb::Helpers::ObjectTable.render(@pets, :fields=>[:name, :age]).should == expected_table
|
19
|
+
end
|
20
|
+
|
21
|
+
test "with no options defaults to to_s field" do
|
22
|
+
expected_table = <<-TABLE.unindent
|
23
|
+
+-------+
|
24
|
+
| value |
|
25
|
+
+-------+
|
26
|
+
| rufus |
|
27
|
+
| alf |
|
28
|
+
+-------+
|
29
|
+
2 rows in set
|
30
|
+
TABLE
|
31
|
+
Hirb::Helpers::ObjectTable.render(@pets).should == expected_table
|
32
|
+
end
|
33
|
+
|
34
|
+
test "renders simple arrays" do
|
35
|
+
expected_table = <<-TABLE.unindent
|
36
|
+
+-------+
|
37
|
+
| value |
|
38
|
+
+-------+
|
39
|
+
| 1 |
|
40
|
+
| 2 |
|
41
|
+
| 3 |
|
42
|
+
| 4 |
|
43
|
+
+-------+
|
44
|
+
4 rows in set
|
45
|
+
TABLE
|
46
|
+
Hirb::Helpers::ObjectTable.render([1,2,3,4]).should == expected_table
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
data/test/table_test.rb
CHANGED
@@ -42,7 +42,7 @@ class Hirb::Helpers::TableTest < Test::Unit::TestCase
|
|
42
42
|
TABLE
|
43
43
|
table([{'a'=>1, 'b'=>2}, {'a'=>3, 'b'=>4}]).should == expected_table
|
44
44
|
end
|
45
|
-
|
45
|
+
|
46
46
|
test "with array only rows renders" do
|
47
47
|
expected_table = <<-TABLE.unindent
|
48
48
|
+---+---+
|
@@ -55,11 +55,13 @@ class Hirb::Helpers::TableTest < Test::Unit::TestCase
|
|
55
55
|
TABLE
|
56
56
|
table([[1,2], [3,4]]).should == expected_table
|
57
57
|
end
|
58
|
-
|
59
|
-
test "with too many fields
|
60
|
-
|
58
|
+
|
59
|
+
test "with too many fields defaults to vertical table" do
|
60
|
+
rows = [Array.new(25, "A"* 10)]
|
61
|
+
Hirb::Helpers::VerticalTable.expects(:render).with(rows, anything)
|
62
|
+
capture_stderr { table(rows)}.should =~ /Error/
|
61
63
|
end
|
62
|
-
|
64
|
+
|
63
65
|
test "with no rows renders" do
|
64
66
|
table([]).should == "0 rows in set"
|
65
67
|
end
|
@@ -248,72 +250,21 @@ class Hirb::Helpers::TableTest < Test::Unit::TestCase
|
|
248
250
|
TABLE
|
249
251
|
table([['a','b'], ['c', 'd']], :number=>true).should == expected_table
|
250
252
|
end
|
251
|
-
end
|
252
|
-
|
253
|
-
context "object table" do
|
254
|
-
before(:all) {
|
255
|
-
@pets = [stub(:name=>'rufus', :age=>7, :to_s=>'rufus'), stub(:name=>'alf', :age=>101, :to_s=>'alf')]
|
256
|
-
}
|
257
|
-
test "renders" do
|
258
|
-
expected_table = <<-TABLE.unindent
|
259
|
-
+-------+-----+
|
260
|
-
| name | age |
|
261
|
-
+-------+-----+
|
262
|
-
| rufus | 7 |
|
263
|
-
| alf | 101 |
|
264
|
-
+-------+-----+
|
265
|
-
2 rows in set
|
266
|
-
TABLE
|
267
|
-
Hirb::Helpers::ObjectTable.render(@pets, :fields=>[:name, :age]).should == expected_table
|
268
|
-
end
|
269
|
-
|
270
|
-
test "with no options defaults to to_s field" do
|
271
|
-
expected_table = <<-TABLE.unindent
|
272
|
-
+-------+
|
273
|
-
| value |
|
274
|
-
+-------+
|
275
|
-
| rufus |
|
276
|
-
| alf |
|
277
|
-
+-------+
|
278
|
-
2 rows in set
|
279
|
-
TABLE
|
280
|
-
Hirb::Helpers::ObjectTable.render(@pets).should == expected_table
|
281
|
-
end
|
282
253
|
|
283
|
-
test "renders
|
254
|
+
test "vertical option renders vertical table" do
|
284
255
|
expected_table = <<-TABLE.unindent
|
285
|
-
|
286
|
-
|
287
|
-
|
288
|
-
|
289
|
-
|
290
|
-
|
291
|
-
| 4 |
|
292
|
-
+-------+
|
293
|
-
4 rows in set
|
294
|
-
TABLE
|
295
|
-
Hirb::Helpers::ObjectTable.render([1,2,3,4]).should == expected_table
|
296
|
-
end
|
297
|
-
end
|
298
|
-
|
299
|
-
context "activerecord table" do
|
300
|
-
before(:all) {
|
301
|
-
@pets = [stub(:name=>'rufus', :age=>7, :class=>mock(:column_names=>[:age, :name])), stub(:name=>'alf', :age=>101)]
|
302
|
-
}
|
303
|
-
test "renders" do
|
304
|
-
expected_table = <<-TABLE.unindent
|
305
|
-
+-----+-------+
|
306
|
-
| age | name |
|
307
|
-
+-----+-------+
|
308
|
-
| 7 | rufus |
|
309
|
-
| 101 | alf |
|
310
|
-
+-----+-------+
|
256
|
+
*** 1. row ***
|
257
|
+
a: 1
|
258
|
+
b: 2
|
259
|
+
*** 2. row ***
|
260
|
+
a: 3
|
261
|
+
b: 4
|
311
262
|
2 rows in set
|
312
263
|
TABLE
|
313
|
-
|
264
|
+
table([{:a=>1, :b=>2}, {:a=>3, :b=>4}], :vertical=>true).should == expected_table
|
314
265
|
end
|
315
266
|
end
|
316
|
-
|
267
|
+
|
317
268
|
test "restrict_field_lengths ensures columns total doesn't exceed max width" do
|
318
269
|
@table = Hirb::Helpers::Table.new([{:f1=>'f1', :f2=>'2', :f3=>'3', :f4=>'4'}])
|
319
270
|
field_lengths = {:f1=>135, :f2=>45, :f3=>4, :f4=>55}
|
data/test/test_helper.rb
CHANGED
@@ -24,6 +24,17 @@ class Test::Unit::TestCase
|
|
24
24
|
fake.string
|
25
25
|
end
|
26
26
|
|
27
|
+
def capture_stderr(&block)
|
28
|
+
original_stderr = $stderr
|
29
|
+
$stderr = fake = StringIO.new
|
30
|
+
begin
|
31
|
+
yield
|
32
|
+
ensure
|
33
|
+
$stderr = original_stderr
|
34
|
+
end
|
35
|
+
fake.string
|
36
|
+
end
|
37
|
+
|
27
38
|
def reset_config
|
28
39
|
Hirb::View.instance_eval "@config = nil"
|
29
40
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: cldwalker-hirb
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Gabriel Horner
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-06-
|
12
|
+
date: 2009-06-29 00:00:00 -07:00
|
13
13
|
default_executable:
|
14
14
|
dependencies: []
|
15
15
|
|
@@ -39,17 +39,21 @@ files:
|
|
39
39
|
- lib/hirb/helpers/parent_child_tree.rb
|
40
40
|
- lib/hirb/helpers/table.rb
|
41
41
|
- lib/hirb/helpers/tree.rb
|
42
|
+
- lib/hirb/helpers/vertical_table.rb
|
42
43
|
- lib/hirb/import_object.rb
|
43
44
|
- lib/hirb/menu.rb
|
44
45
|
- lib/hirb/pager.rb
|
45
46
|
- lib/hirb/util.rb
|
46
47
|
- lib/hirb/view.rb
|
47
48
|
- lib/hirb/views/activerecord_base.rb
|
49
|
+
- test/active_record_table_test.rb
|
50
|
+
- test/auto_table_test.rb
|
48
51
|
- test/console_test.rb
|
49
52
|
- test/formatter_test.rb
|
50
53
|
- test/hirb_test.rb
|
51
54
|
- test/import_test.rb
|
52
55
|
- test/menu_test.rb
|
56
|
+
- test/object_table_test.rb
|
53
57
|
- test/pager_test.rb
|
54
58
|
- test/table_test.rb
|
55
59
|
- test/test_helper.rb
|
@@ -83,11 +87,14 @@ signing_key:
|
|
83
87
|
specification_version: 3
|
84
88
|
summary: A mini view framework for console/irb that's easy to use, even while under its influence.
|
85
89
|
test_files:
|
90
|
+
- test/active_record_table_test.rb
|
91
|
+
- test/auto_table_test.rb
|
86
92
|
- test/console_test.rb
|
87
93
|
- test/formatter_test.rb
|
88
94
|
- test/hirb_test.rb
|
89
95
|
- test/import_test.rb
|
90
96
|
- test/menu_test.rb
|
97
|
+
- test/object_table_test.rb
|
91
98
|
- test/pager_test.rb
|
92
99
|
- test/table_test.rb
|
93
100
|
- test/test_helper.rb
|