hirb 0.2.7 → 0.2.8

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,3 +1,10 @@
1
+ == 0.2.8
2
+ * Added callbacks to Hirb::Helper::Table.
3
+ * Added :change_fields option to Hirb::Helpers::Table.
4
+ * Added terminal size detection for jruby.
5
+ * Bug fix for paging long outputs.
6
+ * Bug fix to make unexpected hirb rendering errors more clear.
7
+
1
8
  == 0.2.7
2
9
  * 2 ruby 1.9 bug fixes.
3
10
  * Bug fix in :fields of Hirb::Helpers::ObjectTable.
@@ -12,7 +12,7 @@ model classes as tables. Hirb also sports a nice selection menu, Hirb::Menu.
12
12
 
13
13
  Install the gem with:
14
14
 
15
- sudo gem install cldwalker-hirb --source http://gems.github.com
15
+ sudo gem install hirb
16
16
 
17
17
  == Pager
18
18
 
@@ -151,7 +151,7 @@ app's needs}[http://github.com/cldwalker/tag-tree].
151
151
 
152
152
  == Credits
153
153
  * Chrononaut for vertical table helper.
154
- * crafterm and spastorino for bug fixes.
154
+ * crafterm, spastorino and joshua for bug fixes.
155
155
 
156
156
  == Bugs/Issues
157
157
  Please report them {on github}[http://github.com/cldwalker/hirb/issues].
@@ -1,4 +1,4 @@
1
1
  ---
2
2
  :major: 0
3
3
  :minor: 2
4
- :patch: 7
4
+ :patch: 8
@@ -1,5 +1,5 @@
1
1
  class Hirb::Helpers::ActiveRecordTable < Hirb::Helpers::ObjectTable
2
- # Rows are Rails' ActiveRecord::Base objects.
2
+ # Rows are Rails' ActiveRecord::Base objects. These objects should all be from one class.
3
3
  # Takes same options as Hirb::Helpers::Table.render except as noted below.
4
4
  #
5
5
  # Options:
@@ -3,21 +3,22 @@ class Hirb::Helpers::ParentChildTree < Hirb::Helpers::Tree
3
3
  # Starting with the given node, this builds a tree by recursively calling a children method.
4
4
  # Takes same options as Hirb::Helper::Table.render with some additional ones below.
5
5
  # ==== Options:
6
- # [:value_method] Method to call to display as a node's value. If not given, uses :name if node
6
+ # [:value_method] Method or proc to call to display as a node's value. If not given, uses :name if node
7
7
  # responds to :name or defaults to :object_id.
8
8
  # [:children_method] Method or proc to call to obtain a node's children. Default is :children.
9
9
  def render(root_node, options={})
10
- @value_method = options[:value_method] || (root_node.respond_to?(:name) ? :name : :object_id)
11
- @children_method = options[:children_method] || :children
10
+ value_method = options[:value_method] || (root_node.respond_to?(:name) ? :name : :object_id)
11
+ @value_method = value_method.is_a?(Proc) ? value_method : lambda {|n| n.send(value_method) }
12
+ children_method = options[:children_method] || :children
13
+ @children_method = children_method.is_a?(Proc) ? children_method : lambda {|n| n.send(children_method)}
12
14
  @nodes = []
13
15
  build_node(root_node, 0)
14
16
  super(@nodes, options)
15
17
  end
16
18
 
17
19
  def build_node(node, level) #:nodoc:
18
- @nodes << {:value=>node.send(@value_method), :level=>level}
19
- children = @children_method.is_a?(Proc) ? @children_method.call(node) : node.send(@children_method)
20
- children.each {|e| build_node(e, level + 1)}
20
+ @nodes << {:value=>@value_method.call(node), :level=>level}
21
+ @children_method.call(node).each {|e| build_node(e, level + 1)}
21
22
  end
22
23
  end
23
24
  end
@@ -42,6 +42,8 @@ module Hirb
42
42
  # [:max_width] The maximum allowed width of all fields put together. This option is enforced except when the field_lengths option is set.
43
43
  # This doesn't count field borders as part of the total.
44
44
  # [: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 is useful when wanting to change auto-generated keys to
46
+ # more user-friendly names i.e. for array of arrays.
45
47
  # [: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
46
48
  # a given proc or an array containing a method and optional arguments to it.
47
49
  # [:vertical] When set to true, renders a vertical table using Hirb::Helpers::VerticalTable. Default is false.
@@ -64,7 +66,7 @@ module Hirb
64
66
 
65
67
  #:stopdoc:
66
68
  def initialize(rows, options={})
67
- @options = {:description=>true, :filters=>{}}.merge(options)
69
+ @options = {:description=>true, :filters=>{}, :change_fields=>{}}.merge(options)
68
70
  @fields = set_fields(rows)
69
71
  @rows = setup_rows(rows)
70
72
  @headers = @fields.inject({}) {|h,e| h[e] = e.to_s; h}
@@ -79,7 +81,7 @@ module Hirb
79
81
  end
80
82
 
81
83
  def set_fields(rows)
82
- if @options[:fields]
84
+ fields = if @options[:fields]
83
85
  @options[:fields].dup
84
86
  else
85
87
  if rows[0].is_a?(Hash)
@@ -89,18 +91,27 @@ module Hirb
89
91
  rows[0].is_a?(Array) ? (0..rows[0].length - 1).to_a : []
90
92
  end
91
93
  end
94
+ @options[:change_fields].each do |oldf, newf|
95
+ (index = fields.index(oldf)) ? fields[index] = newf : fields << newf
96
+ end
97
+ fields
92
98
  end
93
99
 
94
100
  def setup_rows(rows)
95
- rows ||= []
96
- rows = [rows] unless rows.is_a?(Array)
101
+ rows = Array(rows)
97
102
  if rows[0].is_a?(Array)
98
103
  rows = rows.inject([]) {|new_rows, row|
99
104
  new_rows << array_to_indices_hash(row)
100
105
  }
101
106
  end
107
+ @options[:change_fields].each do |oldf, newf|
108
+ rows.each {|e| e[newf] = e.delete(oldf) if e.key?(oldf) }
109
+ end
102
110
  rows = filter_values(rows)
103
111
  rows.each_with_index {|e,i| e[:hirb_number] = (i + 1).to_s} if @options[:number]
112
+ methods.grep(/_callback$/).sort.each do |meth|
113
+ rows = send(meth, rows, @options.dup)
114
+ end
104
115
  validate_values(rows)
105
116
  rows
106
117
  end
@@ -38,6 +38,7 @@ module Hirb
38
38
  save_stdout = STDOUT.clone
39
39
  STDOUT.reopen(pager)
40
40
  STDOUT.puts output
41
+ rescue Errno::EPIPE
41
42
  ensure
42
43
  STDOUT.reopen(save_stdout)
43
44
  save_stdout.close
@@ -59,8 +59,13 @@ module Hirb
59
59
  # Returns [width, height] of terminal when detected, nil if not detected.
60
60
  # Think of this as a simpler version of Highline's Highline::SystemExtensions.terminal_size()
61
61
  def detect_terminal_size
62
- (ENV['COLUMNS'] =~ /^\d+$/) && (ENV['LINES'] =~ /^\d+$/) ? [ENV['COLUMNS'].to_i, ENV['LINES'].to_i] :
63
- ( command_exists?('stty') ? `stty size`.scan(/\d+/).map { |s| s.to_i }.reverse : nil )
62
+ if (ENV['COLUMNS'] =~ /^\d+$/) && (ENV['LINES'] =~ /^\d+$/)
63
+ [ENV['COLUMNS'].to_i, ENV['LINES'].to_i]
64
+ elsif RUBY_PLATFORM =~ /java/ && command_exists?('tput')
65
+ [`tput cols`.to_i, `tput lines`.to_i]
66
+ else
67
+ command_exists?('stty') ? `stty size`.scan(/\d+/).map { |s| s.to_i }.reverse : nil
68
+ end
64
69
  rescue
65
70
  nil
66
71
  end
@@ -68,6 +68,10 @@ module Hirb
68
68
  # Hirb::Formatter.format_output(). Returns true if successful and false if no formatting is done or if not enabled.
69
69
  def view_output(output, options={})
70
70
  enabled? && config[:formatter] && render_output(output, options)
71
+ rescue Exception=>e
72
+ index = (obj = e.backtrace.find {|f| f =~ /^\(eval\)/}) ? e.backtrace.index(obj) : e.backtrace.length
73
+ $stderr.puts "Hirb Error: #{e.message}", e.backtrace.slice(0,index).map {|e| " " + e }
74
+ true
71
75
  end
72
76
 
73
77
  # Captures STDOUT and renders it using render_method(). The main use case is to conditionally page captured stdout.
@@ -318,8 +318,40 @@ class Hirb::Helpers::TableTest < Test::Unit::TestCase
318
318
  TABLE
319
319
  table([{:a=>1, :b=>2}, {:a=>3, :c=>4}], :all_fields=>true).should == expected_table
320
320
  end
321
+
322
+ test "change_fields option renders" do
323
+ expected_table = <<-TABLE.unindent
324
+ +------+-------+
325
+ | name | value |
326
+ +------+-------+
327
+ | 1 | 2 |
328
+ | 2 | 3 |
329
+ +------+-------+
330
+ 2 rows in set
331
+ TABLE
332
+ table([[1,2],[2,3]], :change_fields=>{0=>'name', 1=>'value'}).should == expected_table
333
+ end
321
334
  end
322
335
 
336
+ test "table can detect and run callbacks" do
337
+ Hirb::Helpers::Table.send(:define_method, :and_one_callback) do |obj, opt|
338
+ obj.each {|row| row.each {|k,v| row[k] += opt[:add] } }
339
+ obj
340
+ end
341
+
342
+ expected_table = <<-TABLE.unindent
343
+ +---+---+
344
+ | a | b |
345
+ +---+---+
346
+ | 2 | 3 |
347
+ | 4 | 5 |
348
+ +---+---+
349
+ 2 rows in set
350
+ TABLE
351
+ table([{'a'=>1, 'b'=>2}, {'a'=>3, 'b'=>4}], :add=>1).should == expected_table
352
+ Hirb::Helpers::Table.send(:remove_method, :and_one_callback)
353
+ end
354
+
323
355
  test "restrict_field_lengths ensures columns total doesn't exceed max width" do
324
356
  @table = Hirb::Helpers::Table.new([{:f1=>'f1', :f2=>'2', :f3=>'3', :f4=>'4'}])
325
357
  field_lengths = {:f1=>135, :f2=>45, :f3=>4, :f4=>55}
@@ -22,6 +22,13 @@ module Hirb
22
22
  View.page_output('blah').should be(false)
23
23
  end
24
24
 
25
+ test "view_output catches unexpected errors and prints them" do
26
+ Hirb.enable
27
+ View.expects(:render_output).raises('blah')
28
+ capture_stderr { View.view_output([1,2,3]) }.should =~ /Hirb Error: blah/
29
+ Hirb.disable
30
+ end
31
+
25
32
  context "enable" do
26
33
  before(:each) { reset_config }
27
34
  after(:each) { Hirb.disable }
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.7
4
+ version: 0.2.8
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-10-14 00:00:00 -04:00
12
+ date: 2009-11-07 00:00:00 -05:00
13
13
  default_executable:
14
14
  dependencies: []
15
15