cldwalker-hirb 0.1.2 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG.rdoc CHANGED
@@ -1,3 +1,10 @@
1
+ == 0.2.0
2
+ * Major refactoring with bug fixes and better tests.
3
+ * Improved table algorithm to ensure that tables don't wrap.
4
+ * Added a pager which detects if output should be paged, Hirb::Pager.
5
+ * Added a selection menu, Hirb::Menu
6
+ * Following API changes: Hirb::Helpers::Table.max_width removed and config files don't use
7
+ the :view key anymore.
1
8
  == 0.1.2
2
9
  * Added tree views.
3
10
  * Added output_method option to Hirb::View.render_output.
data/README.rdoc CHANGED
@@ -1,11 +1,12 @@
1
1
  == Description
2
2
 
3
- Hirb currently provides a mini view framework for console applications, designed with irb in mind.
4
- Given the output of a console application, it renders a view if there is one configured, based on
5
- the output's class. The framework encourages reusing views by letting you package them in classes
6
- and associate them with any number of output classes. Hirb comes with tree views (see
3
+ Hirb currently provides a mini view framework for console applications, designed to improve irb's default output.
4
+ Hirb improves console output by providing a smart pager and auto-formatting output. The smart pager detects when an output exceeds
5
+ a screenful and thus only pages output as needed. Auto-formatting adds a view to an output's class. This is helpful in separating
6
+ views from content (MVC anyone?). The framework encourages reusing views by letting you
7
+ package them in classes and associate them with any number of output classes. Hirb comes with tree views (see
7
8
  Hirb::Helpers::Tree) and table views (see Hirb::Helpers::Table). By default Hirb displays Rails'
8
- model classes as tables.
9
+ model classes as tables. Hirb also sports a nice selection menu, Hirb::Menu.
9
10
 
10
11
  == Install
11
12
 
@@ -13,6 +14,10 @@ Install the gem with:
13
14
 
14
15
  sudo gem install cldwalker-hirb --source http://gems.github.com
15
16
 
17
+ == Create and Configure Views
18
+
19
+ If you'd like to learn how to create and configure views, {read the docs}[http://tagaholic.me/hirb/doc/classes/Hirb/Formatter.html].
20
+
16
21
  == Rails Example
17
22
 
18
23
  Let's load and enable the view framework:
@@ -20,12 +25,12 @@ Let's load and enable the view framework:
20
25
  Loading local environment (Rails 2.2.2)
21
26
  irb>> require 'hirb'
22
27
  => true
23
- irb>> Hirb::View.enable
28
+ irb>> Hirb.enable
24
29
  => nil
25
30
 
26
31
  The default configuration provides table views for ActiveRecord::Base descendants.
27
32
  If a class isn't configured, Hirb reverts to irb's default echo mode.
28
- irb>> Hirb::View.output_config
33
+ irb>> Hirb::View.formatter_config
29
34
  => {"ActiveRecord::Base"=>{:class=>"Hirb::Views::ActiveRecord_Base", :ancestor=>true}}
30
35
 
31
36
  # Tag is a model class and descendant of ActiveRecord::Base
@@ -60,7 +65,7 @@ you may appreciate it also detects configured output objects in an array:
60
65
  3 rows in set
61
66
 
62
67
  At any time you can disable Hirb if you really like irb's lovely echo mode:
63
- irb>> Hirb::View.disable
68
+ irb>> Hirb.disable
64
69
  => nil
65
70
  irb>> Tag.all :limit=>3, :order=>"id DESC"
66
71
  => [#<Tag id: 907, name: "gem:tags=yaml", description: nil, created_at: "2009-03-06 21:10:41",
@@ -74,7 +79,7 @@ While preconfigured tables are great for database records, sometimes you just wa
74
79
  tables/views for any output object:
75
80
 
76
81
  #These examples don't need to have Hirb::View enabled.
77
- irb>>Hirb::View.disable
82
+ irb>>Hirb.disable
78
83
  =>nil
79
84
 
80
85
  # Imports table() and view()
@@ -91,8 +96,8 @@ tables/views for any output object:
91
96
  +------------+--------+-----------+-------+--------------------------+
92
97
  2 rows in set
93
98
 
94
- # Same table as the previous method. However view() will be able to call any view created.
95
- irb>> view [Date.today, Date.today.next_month], :class=>Hirb::Helpers::ObjectTable,
99
+ # Same table as the previous method. However view() will be able to call any helper.
100
+ irb>> view [Date.today, Date.today.next_month], :class=>:object_table,
96
101
  :fields=>[:to_s, :ld, :ajd, :amjd, :asctime]
97
102
 
98
103
  If these console methods weren't convenient enough, try:
@@ -101,7 +106,7 @@ If these console methods weren't convenient enough, try:
101
106
  irb>> require 'hirb/import_object'
102
107
  =>true
103
108
  # Yields same table as above examples.
104
- irb>> [Date.today, Date.today.next_month].view :class=>Hirb::Helpers::ObjectTable,
109
+ irb>> [Date.today, Date.today.next_month].view :class=>:object_table,
105
110
  :fields=>[:to_s, :ld, :ajd, :amjd, :asctime]
106
111
 
107
112
  Although views by default are printed to STDOUT, they can be easily modified to write anywhere:
@@ -109,7 +114,7 @@ Although views by default are printed to STDOUT, they can be easily modified to
109
114
  irb>> Hirb::View.render_method = lambda {|output| File.open("console.log", 'w') {|f| f.write(output) } }
110
115
 
111
116
  # Writes to file with same table output as above example.
112
- irb>> view [Date.today, Date.today.next_month], :class=>Hirb::Helpers::ObjectTable,
117
+ irb>> view [Date.today, Date.today.next_month], :class=>:object_table,
113
118
  :fields=>[:to_s, :ld, :ajd, :amjd, :asctime]
114
119
 
115
120
  # Doesn't write to file because Symbol isn't configured to use Hirb::View and thus defaults to irb's echo mode.
@@ -119,110 +124,27 @@ Although views by default are printed to STDOUT, they can be easily modified to
119
124
  # Go back to printing Hirb views to STDOUT.
120
125
  irb>> Hirb::View.reset_render_method
121
126
 
122
- == Create and Configure Views
123
- Let's create a simple view and configure it in different ways to be Hash's default view:
124
-
125
- === Setup
126
- irb>> require 'hirb'
127
- =>true
128
- irb>> Hirb::View.enable
129
- =>nil
130
- irb>> require 'yaml'
131
- =>true
132
-
133
- === Configure As View Method
134
- A view method is the smallest reuseable view.
135
- # Create yaml view method
136
- irb>> def yaml(output); output.to_yaml; end
137
- =>nil
138
-
139
- # Configure view and reload it
140
- irb>>Hirb::View.output_config = {"Hash"=>{:method=>:yaml}}
141
- =>{"Hash"=>{:method=>:yaml}}
142
- irb>>Hash::View.reload_config
143
- =>true
144
-
145
- # Hashes now appear as yaml
146
- irb>>{:a=>1, :b=>{:c=>3}}
147
- ---
148
- :a : 1
149
- :b :
150
- :c : 3
151
- => true
152
-
153
- === Configure As View Class
154
- A view class is suited for more complex views. View classes can be under any namespace
155
- and are expected to provide a render method. However, if a class is under the Hirb::Views namespace,
156
- it will be automatically loaded with no configuration. Something to think about when
157
- sharing views with others.
158
-
159
- # Create yaml view class
160
- irb>> class Hirb::Views::Hash; def self.render(output, options={}); output.to_yaml; end ;end
161
- =>nil
162
- # Just reload since no configuration is necessary
163
- irb>>Hirb::View.reload_config
164
-
165
- # Hashes now appear as yaml ...
166
-
167
- Although the Hirb::Views namespace is great for quick classes that just plug and play, you
168
- often want view classes that can be reused with multiple outputs. For this case, it's recommended to
169
- use the Hirb::Helpers namespace.
170
-
171
- # Create yaml view class
172
- irb>> class Hirb::Helpers::Yaml; def self.render(output, options={}); output.to_yaml; end ;end
173
- =>nil
174
-
175
- # Configure view and reload it
176
- irb>>Hirb::View.output_config = {"Hash"=>{:class=>"Hirb::Helpers::Yaml"}}
177
- =>{"Hash"=>{:class=>"Hirb::Helpers::Yaml"}}
178
- irb>>Hirb::View.reload_config
179
-
180
- # Hashes now appear as yaml ...
181
-
182
- === Configure At Startup
183
- Once you know what views are associated with what output classes, you can configure
184
- them at startup by passing Hirb::View.enable a block:
185
- # In .irbrc
186
- require 'hirb'
187
- # View class needs to come before enable()
188
- class Hirb::Helpers::Yaml; def self.render(output, options={}); output.to_yaml; end ;end
189
- Hirb::View.enable {|conf| conf.output = {"Hash"=>{:class=>"Hirb::Helpers::Yaml"}} }
190
-
191
- Or by creating a config file at config/hirb.yml or ~/.hirb.yml:
192
- # The config file for the yaml example would look like:
193
- # ---
194
- # :view :
195
- # :output :
196
- # Hash :
197
- # :class : Hirb::Helpers::Yaml
198
-
199
- # In .irbrc
200
- require 'hirb'
201
- # View class needs to come before enable()
202
- class Hirb::Helpers::Yaml; def self.render(output, options={}); output.to_yaml; end ;end
203
- Hirb::View.enable
204
-
205
- == Contributing Views
206
- If you have views of your own you'd like to share, fork Hirb and put your views under
207
- the Hirb::Helpers namespace and the view files under lib/hirb/helpers/.
127
+ == Sharing Views
128
+ If you have tested views you'd like to share, fork Hirb and put your views under
129
+ the lib/hirb/views/ and/or helpers files under lib/hirb/helpers/. If not tested, feel free to share
130
+ them on the wiki.
208
131
 
209
132
  == Limitations
210
- Although Hirb preserves Wirble colorizing irb's default echo mode, it doesn't colorize its own views.
211
- This is mainly because colorizing caused table classes to render incorrectly. If you can get tables
212
- and colors to work nicely, please fork. To colorize your Hirb output:
213
- Hirb::View.render_method = lambda {|output| puts Wirble::Colorize.colorize(output) }
133
+ If using Wirble, you should call Hirb after it since they both override irb's default output.
214
134
 
215
135
  == Motivation
216
136
  Table code from http://gist.github.com/72234 and {my console
217
137
  app's needs}[http://github.com/cldwalker/tag-tree].
218
138
 
139
+ == Bugs/Issues
140
+ Please report them {on github}[http://github.com/cldwalker/hirb/issues].
141
+
219
142
  == Links
220
143
  * http://tagaholic.me/2009/03/13/hirb-irb-on-the-good-stuff.html
221
144
  * http://tagaholic.me/2009/03/18/ruby-class-trees-rails-plugin-trees-with-hirb.html
222
145
 
223
146
  == Todo
224
- * Configurable max height, which if exceeded activates a pager.
225
- * Possibly add non-view irb goodies ie command manager.
226
147
  * Consider applying multiple views/filters to output.
148
+ * Consider mapping a class' methods to their own views.
227
149
  * Provides helper methods to all view classes.
228
150
  * Consider adding a template system as needed.
data/Rakefile CHANGED
@@ -18,11 +18,12 @@ begin
18
18
  require 'jeweler'
19
19
  Jeweler::Tasks.new do |s|
20
20
  s.name = "hirb"
21
- s.description = "A mini view framework for console/irb that's easy to use, even while under its influence."
22
- s.summary = s.description
21
+ s.summary = "A mini view framework for console/irb that's easy to use, even while under its influence."
22
+ s.description = "Hirb currently provides a mini view framework for console applications, designed to improve irb's default output. Hirb improves console output by providing a smart pager and auto-formatting output. The smart pager detects when an output exceeds a screenful and thus only pages output as needed. Auto-formatting adds a view to an output's class. This is helpful in separating views from content (MVC anyone?). The framework encourages reusing views by letting you package them in classes and associate them with any number of output classes."
23
23
  s.email = "gabriel.horner@gmail.com"
24
24
  s.homepage = "http://github.com/cldwalker/hirb"
25
25
  s.authors = ["Gabriel Horner"]
26
+ s.rubyforge_project = 'tagaholic'
26
27
  s.has_rdoc = true
27
28
  s.extra_rdoc_files = ["README.rdoc", "LICENSE.txt"]
28
29
  s.files = FileList["[A-Z]*", "{bin,lib,test}/**/*"]
data/VERSION.yml CHANGED
@@ -1,4 +1,4 @@
1
1
  ---
2
+ :patch: 0
2
3
  :major: 0
3
- :minor: 1
4
- :patch: 2
4
+ :minor: 2
data/lib/hirb/console.rb CHANGED
@@ -1,17 +1,43 @@
1
1
  module Hirb
2
- # This class is meant to be extended to provide methods for use in a console/irb shell.
2
+ # This module is meant to be extended to provide methods for use in a console/irb shell.
3
3
  # For example:
4
4
  # irb>> extend Hirb::Console
5
5
  # irb>> view 'some string', :class=>Some::String::Formatter
6
6
  # irb>> table [[:row1], [:row2]]
7
7
  module Console
8
+ class<<self
9
+ # A console version of render_output() which takes its same options but allows for shorthand. All options are passed to
10
+ # the helper except for the formatter options. Formatter options are :class, :method and :output_method.
11
+ # Examples:
12
+ # render_output output, :class=>:tree :type=>:directory
13
+ # # is the same as:
14
+ # render_output output, :class=>:tree, :options=> {:type=>:directory}
15
+ #
16
+ def render_output(output, options={})
17
+ View.load_config unless View.config_loaded?
18
+ View.render_output(output, options.merge(:console=>true))
19
+ end
20
+
21
+ # Takes same arguments and options as render_output() but returns formatted output instead of rendering it.
22
+ def format_output(output, options={}, &block)
23
+ View.load_config unless View.config_loaded?
24
+ View.formatter.format_output(output, options.merge(:console=>true), &block)
25
+ end
26
+ end
27
+
8
28
  # Renders a table for the given object. Takes same options as Hirb::Helpers::Table.render.
9
29
  def table(output, options={})
10
- Hirb::View.console_render_output(output, options.merge(:class=>"Hirb::Helpers::AutoTable"))
30
+ Console.render_output(output, options.merge(:class=>"Hirb::Helpers::AutoTable"))
31
+ end
32
+
33
+ # Renders any specified view for the given object. Takes same options as Hirb::View.render_output.
34
+ def view(output, options={})
35
+ Console.render_output(*args)
11
36
  end
12
- # Renders any specified view for the given object. Takes same options as Hirb::View.console_render_output.
13
- def view(*args)
14
- Hirb::View.console_render_output(*args)
37
+
38
+ # Renders a menu given an array using Hirb::Menu.render.
39
+ def menu(output, options={}, &block)
40
+ Console.format_output(output, options.merge(:class=>"Hirb::Menu"), &block)
15
41
  end
16
42
  end
17
43
  end
@@ -0,0 +1,199 @@
1
+ module Hirb
2
+ =begin rdoc
3
+ This class is format an output into a string using Hirb::Helpers::*, Hirb::Views::* or any user-created views.
4
+ The formatter object looks for an output's class config in Hirb::Formatter.config and if found applies a helper to the output.
5
+
6
+ == Create and Configure Views
7
+ Let's create a simple view and configure it in different ways to be Hash's default view:
8
+
9
+ === Setup
10
+ irb>> require 'hirb'
11
+ =>true
12
+ irb>> Hirb.enable
13
+ =>nil
14
+ irb>> require 'yaml'
15
+ =>true
16
+
17
+ === Configure As View Method
18
+ A view method is the smallest reuseable view.
19
+ # Create yaml view method
20
+ irb>> def yaml(output); output.to_yaml; end
21
+ =>nil
22
+
23
+ # Configure view
24
+ irb>>Hirb::View.format_class Hash, :method=>:yaml
25
+ =>true
26
+
27
+ # Hashes now appear as yaml
28
+ irb>>{:a=>1, :b=>{:c=>3}}
29
+ ---
30
+ :a : 1
31
+ :b :
32
+ :c : 3
33
+ => true
34
+
35
+ === Configure As View Class
36
+ A view class is suited for more complex views. View classes can be under any namespace
37
+ and are expected to provide a render method. However, if a class is under the Hirb::Views namespace,
38
+ it will be automatically loaded with no configuration. Something to think about when
39
+ sharing views with others.
40
+
41
+ # Create yaml view class
42
+ irb>> class Hirb::Views::Hash; def self.render(output, options={}); output.to_yaml; end ;end
43
+ =>nil
44
+ # Just reload since no configuration is necessary
45
+ irb>>Hirb::View.formatter.reload
46
+
47
+ # Hashes now appear as yaml ...
48
+
49
+ Although the Hirb::Views namespace is great for quick classes that just plug and play, you
50
+ often want view classes that can be reused with multiple outputs. For this case, it's recommended to
51
+ use the Hirb::Helpers namespace.
52
+
53
+ # Create yaml view class
54
+ irb>> class Hirb::Helpers::Yaml; def self.render(output, options={}); output.to_yaml; end ;end
55
+ =>nil
56
+
57
+ # Configure view and reload it
58
+ irb>>Hirb::View.format_class Hash, :class=>"Hirb::Helpers::Yaml"
59
+ =>true
60
+
61
+ # Hashes now appear as yaml ...
62
+
63
+ == Configure At Startup
64
+ Once you know what views are associated with what output classes, you can configure
65
+ them at startup by passing Hirb.enable an options hash:
66
+ # In .irbrc
67
+ require 'hirb'
68
+ # View class needs to come before enable()
69
+ class Hirb::Helpers::Yaml; def self.render(output, options={}); output.to_yaml; end ;end
70
+ Hirb.enable :output=>{"Hash"=>{:class=>"Hirb::Helpers::Yaml"}}
71
+
72
+ Or by creating a config file at config/hirb.yml or ~/.hirb.yml:
73
+ # The config file for the yaml example would look like:
74
+ # ---
75
+ # :output :
76
+ # Hash :
77
+ # :class : Hirb::Helpers::Yaml
78
+
79
+ # In .irbrc
80
+ require 'hirb'
81
+ # View class needs to come before enable()
82
+ class Hirb::Helpers::Yaml; def self.render(output, options={}); output.to_yaml; end ;end
83
+ Hirb.enable
84
+ =end
85
+
86
+ class Formatter
87
+ def initialize(additional_config={})
88
+ @klass_config = {}
89
+ @config = Util.recursive_hash_merge default_config, additional_config || {}
90
+ end
91
+
92
+ # A hash of Ruby class strings mapped to helper config hashes. A helper config hash must have at least a :method, :output_method
93
+ # or :class option for a helper to be applied to an output. A helper config hash has the following keys:
94
+ # [:method] Specifies a global (Kernel) method to do the formatting.
95
+ # [:class] Specifies a class to do the formatting, using its render() class method. If a symbol it's converted to a corresponding
96
+ # Hirb::Helpers::* class if it exists.
97
+ # [:output_method] Specifies a method or proc to call on output before passing it to a helper. If the output is an array, it's applied
98
+ # to every element in the array.
99
+ # [:options] Options to pass the helper method or class.
100
+ # [:ancestor] Boolean which when true causes subclasses of the output class to inherit its config. This doesn't effect the current
101
+ # output class. Defaults to false. This is used by ActiveRecord classes.
102
+ #
103
+ # Examples:
104
+ # {'WWW::Delicious::Element'=>{:class=>'Hirb::Helpers::ObjectTable', :ancestor=>true, :options=>{:max_width=>180}}}
105
+ # {'Date'=>{:class=>:auto_table, :ancestor=>true}}
106
+ def config
107
+ @config
108
+ end
109
+
110
+ # Sets the helper config for the given output class.
111
+ def format_class(klass, helper_config)
112
+ @klass_config.delete(klass)
113
+ @config[klass.to_s] = helper_config
114
+ true
115
+ end
116
+
117
+ # Reloads autodetected Hirb::Views
118
+ def reload
119
+ @config = Util.recursive_hash_merge default_config, @config
120
+ end
121
+
122
+ # This is the main method of this class. The formatter looks for the first helper in its config for the given output class.
123
+ # If a helper is found, the output is converted by the helper into a string and returned. If not, nil is returned. The options
124
+ # this class takes are a helper config hash as described in config. These options will be merged with any existing helper config hash
125
+ # an output class has in config. Any block given is passed along to a helper class.
126
+ def format_output(output, options={}, &block)
127
+ output_class = determine_output_class(output)
128
+ options = parse_console_options(options) if options.delete(:console)
129
+ options = Util.recursive_hash_merge(klass_config(output_class), options)
130
+ output = options[:output_method] ? (output.is_a?(Array) ? output.map {|e| call_output_method(options[:output_method], e) } :
131
+ call_output_method(options[:output_method], output) ) : output
132
+ args = [output]
133
+ args << options[:options] if options[:options] && !options[:options].empty?
134
+ if options[:method]
135
+ new_output = send(options[:method],*args)
136
+ elsif options[:class] && (helper_class = determine_helper_class(options[:class]))
137
+ new_output = helper_class.render(*args, &block)
138
+ elsif options[:output_method]
139
+ new_output = output
140
+ end
141
+ new_output
142
+ end
143
+
144
+ #:stopdoc:
145
+ def parse_console_options(options) #:nodoc:
146
+ real_options = [:method, :class, :output_method].inject({}) do |h, e|
147
+ h[e] = options.delete(e) if options[e]; h
148
+ end
149
+ real_options.merge! :options=>options
150
+ real_options
151
+ end
152
+
153
+ def determine_helper_class(klass)
154
+ if klass.is_a?(Symbol) && (helper_class = Helpers.constants.find {|e| e == Util.camelize(klass.to_s)})
155
+ klass = "Hirb::Helpers::#{helper_class}"
156
+ end
157
+ Util.any_const_get(klass)
158
+ end
159
+
160
+ def determine_output_class(output)
161
+ if output.is_a?(Array)
162
+ output[0].class
163
+ else
164
+ output.class
165
+ end
166
+ end
167
+
168
+ def call_output_method(output_method, output)
169
+ output_method.is_a?(Proc) ? output_method.call(output) : output.send(output_method)
170
+ end
171
+
172
+ # Internal view options built from user-defined ones. Options are built by recursively merging options from oldest
173
+ # ancestors to the most recent ones.
174
+ def klass_config(output_class)
175
+ @klass_config[output_class] ||= begin
176
+ output_ancestors_with_config = output_class.ancestors.map {|e| e.to_s}.select {|e| @config.has_key?(e)}
177
+ @klass_config[output_class] = output_ancestors_with_config.reverse.inject({}) {|h, klass|
178
+ (klass == output_class.to_s || @config[klass][:ancestor]) ? h.update(@config[klass]) : h
179
+ }
180
+ end
181
+ end
182
+
183
+ def reset_klass_config
184
+ @klass_config = {}
185
+ end
186
+
187
+ def default_config
188
+ Views.constants.inject({}) {|h,e|
189
+ output_class = e.to_s.gsub("_", "::")
190
+ if (views_class = Views.const_get(e)) && views_class.respond_to?(:render)
191
+ default_options = views_class.respond_to?(:default_options) ? views_class.default_options : {}
192
+ h[output_class] = default_options.merge({:class=>"Hirb::Views::#{e}"})
193
+ end
194
+ h
195
+ }
196
+ end
197
+ #:startdoc:
198
+ end
199
+ end
@@ -8,10 +8,9 @@ class Hirb::Helpers::ActiveRecordTable < Hirb::Helpers::ObjectTable
8
8
  rows = [rows] unless rows.is_a?(Array)
9
9
  options[:fields] ||=
10
10
  begin
11
- fields = rows.first.attribute_names
12
- fields.unshift(fields.delete('id')) if fields.include?('id')
11
+ fields = rows.first.class.column_names
13
12
  fields.map {|e| e.to_sym }
14
13
  end
15
14
  super(rows, options)
16
15
  end
17
- end
16
+ end
@@ -1,10 +1,10 @@
1
- # Attempts to autodetect the table class the output represents and delegates rendering to it.
1
+ # Detects the table class the output should use and delegates rendering to it.
2
2
  class Hirb::Helpers::AutoTable
3
3
  # Same options as Hirb::Helpers::Table.render.
4
4
  def self.render(output, options={})
5
5
  klass = if ((output.is_a?(Array) && output[0].is_a?(ActiveRecord::Base)) or output.is_a?(ActiveRecord::Base) rescue false)
6
6
  Hirb::Helpers::ActiveRecordTable
7
- elsif options[:fields]
7
+ elsif (output.is_a?(Array) && !(output[0].is_a?(Hash) || output[0].is_a?(Array)))
8
8
  Hirb::Helpers::ObjectTable
9
9
  else
10
10
  Hirb::Helpers::Table
@@ -2,13 +2,13 @@ class Hirb::Helpers::ObjectTable < Hirb::Helpers::Table
2
2
  # Rows are any ruby objects. Takes same options as Hirb::Helpers::Table.render except as noted below.
3
3
  #
4
4
  # Options:
5
- # :fields- Methods of the object which are represented as columns in the table. Required option.
6
- # All method values are converted to strings via to_s.
5
+ # :fields- Methods of the object to represent as columns. Defaults to [:to_s].
7
6
  def self.render(rows, options ={})
8
- raise(ArgumentError, "Option 'fields' is required.") unless options[:fields]
7
+ options[:fields] ||= [:to_s]
8
+ options[:headers] = {:to_s=>'value'} if options[:fields] == [:to_s]
9
9
  rows = [rows] unless rows.is_a?(Array)
10
10
  item_hashes = rows.inject([]) {|t,item|
11
- t << options[:fields].inject({}) {|h,f| h[f] = item.send(f).to_s; h}
11
+ t << options[:fields].inject({}) {|h,f| h[f] = item.send(f); h}
12
12
  }
13
13
  super(item_hashes, options)
14
14
  end