hirb 0.2.8 → 0.2.9
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG.rdoc +6 -1
- data/VERSION.yml +1 -1
- data/lib/hirb/helpers/auto_table.rb +5 -4
- data/lib/hirb/helpers/table.rb +44 -3
- data/lib/hirb/helpers/vertical_table.rb +1 -1
- data/test/auto_table_test.rb +27 -0
- data/test/table_test.rb +27 -0
- metadata +2 -2
data/CHANGELOG.rdoc
CHANGED
@@ -1,5 +1,10 @@
|
|
1
|
+
== 0.2.9
|
2
|
+
* Added newline filtering and :no_newlines option for table helper.
|
3
|
+
* Added default filters for hashes that have hash values.
|
4
|
+
* Bug fix for deprecated to_a call.
|
5
|
+
|
1
6
|
== 0.2.8
|
2
|
-
* Added callbacks to Hirb::
|
7
|
+
* Added callbacks to Hirb::Helpers::Table.
|
3
8
|
* Added :change_fields option to Hirb::Helpers::Table.
|
4
9
|
* Added terminal size detection for jruby.
|
5
10
|
* Bug fix for paging long outputs.
|
data/VERSION.yml
CHANGED
@@ -2,14 +2,15 @@
|
|
2
2
|
class Hirb::Helpers::AutoTable
|
3
3
|
# Same options as Hirb::Helpers::Table.render.
|
4
4
|
def self.render(output, options={})
|
5
|
-
|
6
|
-
|
5
|
+
options[:_original_class] = output.class
|
6
|
+
output = Array(output)
|
7
|
+
klass = if (output[0].is_a?(ActiveRecord::Base) rescue false)
|
7
8
|
Hirb::Helpers::ActiveRecordTable
|
8
|
-
elsif
|
9
|
+
elsif !(output[0].is_a?(Hash) || output[0].is_a?(Array))
|
9
10
|
Hirb::Helpers::ObjectTable
|
10
11
|
else
|
11
12
|
Hirb::Helpers::Table
|
12
13
|
end
|
13
14
|
klass.render(output, options)
|
14
15
|
end
|
15
|
-
end
|
16
|
+
end
|
data/lib/hirb/helpers/table.rb
CHANGED
@@ -23,6 +23,35 @@ module Hirb
|
|
23
23
|
# +-----+--------+
|
24
24
|
#
|
25
25
|
# By default, the fields/columns are the keys of the first hash.
|
26
|
+
#
|
27
|
+
# === Custom Callbacks
|
28
|
+
# Callback methods can be defined to add your own options that modify rows right before they are rendered.
|
29
|
+
# Here's an example that allows for searching with a :query option:
|
30
|
+
# module Query
|
31
|
+
# # Searches fields given a query hash
|
32
|
+
# def query_callback(rows, options)
|
33
|
+
# return rows unless options[:query]
|
34
|
+
# options[:query].map {|field,query|
|
35
|
+
# rows.select {|e| e[field].to_s =~ /#{query}/i }
|
36
|
+
# }.flatten.uniq
|
37
|
+
# end
|
38
|
+
# end
|
39
|
+
# Hirb::Helpers::Table.send :include, Query
|
40
|
+
#
|
41
|
+
# >> puts Hirb::Helpers::Table.render [{:name=>'batman'}, {:name=>'robin'}], :query=>{:name=>'rob'}
|
42
|
+
# +-------+
|
43
|
+
# | name |
|
44
|
+
# +-------+
|
45
|
+
# | robin |
|
46
|
+
# +-------+
|
47
|
+
# 1 row in set
|
48
|
+
#
|
49
|
+
# Callback methods:
|
50
|
+
# * must be defined in Helpers::Table and end in '_callback'.
|
51
|
+
# * should expect rows and a hash of render options. Rows will be an array of hashes.
|
52
|
+
# * are expected to return an array of hashes.
|
53
|
+
# * are invoked in alphabetical order.
|
54
|
+
# For a thorough example, see {Boson::Pipe}[http://github.com/cldwalker/boson/blob/master/lib/boson/pipe.rb].
|
26
55
|
#--
|
27
56
|
# derived from http://gist.github.com/72234
|
28
57
|
class Helpers::Table
|
@@ -42,16 +71,19 @@ module Hirb
|
|
42
71
|
# [:max_width] The maximum allowed width of all fields put together. This option is enforced except when the field_lengths option is set.
|
43
72
|
# This doesn't count field borders as part of the total.
|
44
73
|
# [:number] When set to true, numbers rows by adding a :hirb_number column as the first column. Default is false.
|
45
|
-
# [:change_fields] A hash to change old field names to new field names. This
|
46
|
-
# more user-friendly names i.e. for array
|
74
|
+
# [:change_fields] A hash to change old field names to new field names. This can also be an array of new names but only for array rows.
|
75
|
+
# This is useful when wanting to change auto-generated keys to more user-friendly names i.e. for array rows.
|
47
76
|
# [: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
|
48
77
|
# a given proc or an array containing a method and optional arguments to it.
|
49
78
|
# [:vertical] When set to true, renders a vertical table using Hirb::Helpers::VerticalTable. Default is false.
|
50
79
|
# [:all_fields] When set to true, renders fields in all rows. Valid only in rows that are hashes. Default is false.
|
51
80
|
# [:description] When set to true, renders row count description at bottom. Default is true.
|
81
|
+
# [:no_newlines] When set to true, stringifies newlines so they don't disrupt tables. Default is false for vertical tables
|
82
|
+
# and true for anything else.
|
52
83
|
# Examples:
|
53
84
|
# Hirb::Helpers::Table.render [[1,2], [2,3]]
|
54
85
|
# Hirb::Helpers::Table.render [[1,2], [2,3]], :field_lengths=>{0=>10}
|
86
|
+
# Hirb::Helpers::Table.render [['a',1], ['b',2]], :change_fields=>%w{letters numbers}
|
55
87
|
# Hirb::Helpers::Table.render [{:age=>10, :weight=>100}, {:age=>80, :weight=>500}]
|
56
88
|
# Hirb::Helpers::Table.render [{:age=>10, :weight=>100}, {:age=>80, :weight=>500}], :headers=>{:weight=>"Weight(lbs)"}
|
57
89
|
# Hirb::Helpers::Table.render [{:age=>10, :weight=>100}, {:age=>80, :weight=>500}], :filters=>{:age=>[:to_f]}
|
@@ -66,7 +98,8 @@ module Hirb
|
|
66
98
|
|
67
99
|
#:stopdoc:
|
68
100
|
def initialize(rows, options={})
|
69
|
-
@options = {:description=>true, :filters=>{}, :change_fields=>{}}.merge(options)
|
101
|
+
@options = {:description=>true, :filters=>{}, :change_fields=>{}, :no_newlines=>true}.merge(options)
|
102
|
+
@options[:change_fields] = array_to_indices_hash(@options[:change_fields]) if @options[:change_fields].is_a?(Array)
|
70
103
|
@fields = set_fields(rows)
|
71
104
|
@rows = setup_rows(rows)
|
72
105
|
@headers = @fields.inject({}) {|h,e| h[e] = e.to_s; h}
|
@@ -235,7 +268,14 @@ module Hirb
|
|
235
268
|
field_lengths
|
236
269
|
end
|
237
270
|
|
271
|
+
def set_filter_defaults(rows)
|
272
|
+
if @options[:_original_class] == Hash && @fields[1] && rows.any? {|e| e[@fields[1]].is_a?(Hash) }
|
273
|
+
(@options[:filters] ||= {})[@fields[1]] ||= :inspect
|
274
|
+
end
|
275
|
+
end
|
276
|
+
|
238
277
|
def filter_values(rows)
|
278
|
+
set_filter_defaults(rows)
|
239
279
|
rows.map {|row|
|
240
280
|
new_row = {}
|
241
281
|
@fields.each {|f|
|
@@ -254,6 +294,7 @@ module Hirb
|
|
254
294
|
rows.each {|row|
|
255
295
|
@fields.each {|f|
|
256
296
|
row[f] = row[f].to_s || ''
|
297
|
+
row[f].gsub!("\n", '\n') if @options[:no_newlines]
|
257
298
|
}
|
258
299
|
}
|
259
300
|
end
|
@@ -3,7 +3,7 @@ class Hirb::Helpers::VerticalTable < Hirb::Helpers::Table
|
|
3
3
|
# Renders a vertical table using the same options as Hirb::Helpers::Table.render except for :field_lengths,
|
4
4
|
# :vertical and :max_width which aren't used.
|
5
5
|
def self.render(rows, options={})
|
6
|
-
new(rows, options).render
|
6
|
+
new(rows, {:no_newlines=>false}.merge(options)).render
|
7
7
|
end
|
8
8
|
|
9
9
|
#:stopdoc:
|
data/test/auto_table_test.rb
CHANGED
@@ -16,5 +16,32 @@ class Hirb::Helpers::AutoTableTest < Test::Unit::TestCase
|
|
16
16
|
TABLE
|
17
17
|
Hirb::Helpers::AutoTable.render(::Set.new([1,2,3])).should == expected_table
|
18
18
|
end
|
19
|
+
|
20
|
+
test "converts hash with any value hashes to inspected values" do
|
21
|
+
expected_table = <<-TABLE.unindent
|
22
|
+
+---+---------+
|
23
|
+
| 0 | 1 |
|
24
|
+
+---+---------+
|
25
|
+
| c | "ok" |
|
26
|
+
| a | {:b=>1} |
|
27
|
+
+---+---------+
|
28
|
+
2 rows in set
|
29
|
+
TABLE
|
30
|
+
Hirb::Helpers::AutoTable.render({:a=>{:b=>1}, :c=>'ok'}).should == expected_table
|
31
|
+
end
|
32
|
+
|
33
|
+
test "doesn't convert hash with value hashes if filter exists for value" do
|
34
|
+
expected_table = <<-TABLE.unindent
|
35
|
+
+------+-------+
|
36
|
+
| name | value |
|
37
|
+
+------+-------+
|
38
|
+
| c | ok |
|
39
|
+
| a | b1 |
|
40
|
+
+------+-------+
|
41
|
+
2 rows in set
|
42
|
+
TABLE
|
43
|
+
Hirb::Helpers::AutoTable.render({:a=>{:b=>1}, :c=>'ok'}, :change_fields=>['name', 'value'],
|
44
|
+
:filters=>{'value'=>:to_s}).should == expected_table
|
45
|
+
end
|
19
46
|
end
|
20
47
|
end
|
data/test/table_test.rb
CHANGED
@@ -80,6 +80,19 @@ class Hirb::Helpers::TableTest < Test::Unit::TestCase
|
|
80
80
|
TABLE
|
81
81
|
table([{:name=>"アイウエオカキ"}, {:name=>"クケコサシスセソタチツテ"}, {:name=>"Tata l'asticote"}, {:name=>"toto létoile PAOLI"}]).should == expected_table
|
82
82
|
end
|
83
|
+
|
84
|
+
test "with newlines renders with newlines stringified" do
|
85
|
+
expected_table = <<-TABLE.unindent
|
86
|
+
+-----+---+
|
87
|
+
| a | b |
|
88
|
+
+-----+---+
|
89
|
+
| 1#{'\n'} | 2 |
|
90
|
+
| 3 | 4 |
|
91
|
+
+-----+---+
|
92
|
+
2 rows in set
|
93
|
+
TABLE
|
94
|
+
table([{'a'=>"1\n", 'b'=>2}, {'a'=>3, 'b'=>4}]).should == expected_table
|
95
|
+
end
|
83
96
|
end
|
84
97
|
|
85
98
|
context "table with" do
|
@@ -291,6 +304,20 @@ class Hirb::Helpers::TableTest < Test::Unit::TestCase
|
|
291
304
|
table([{:a=>1, :b=>2}, {:a=>3, :b=>4}], :vertical=>true).should == expected_table
|
292
305
|
end
|
293
306
|
|
307
|
+
test "vertical option renders vertical table with newlines" do
|
308
|
+
expected_table = <<-TABLE.unindent
|
309
|
+
*** 1. row ***
|
310
|
+
a: 1
|
311
|
+
b: 2
|
312
|
+
*** 2. row ***
|
313
|
+
a: 3
|
314
|
+
b: 4
|
315
|
+
and one
|
316
|
+
2 rows in set
|
317
|
+
TABLE
|
318
|
+
table([{:a=>1, :b=>2}, {:a=>3, :b=>"4\nand one"}], :vertical=>true).should == expected_table
|
319
|
+
end
|
320
|
+
|
294
321
|
test "vertical option renders vertical table successively" do
|
295
322
|
expected_table = <<-TABLE.unindent
|
296
323
|
*** 1. row ***
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: hirb
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.9
|
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-11-
|
12
|
+
date: 2009-11-19 00:00:00 -05:00
|
13
13
|
default_executable:
|
14
14
|
dependencies: []
|
15
15
|
|