This module generates views for a command by handing it to Hirb. Since Hirb can be customized to generate any view, commands can have any views associated with them!
Views with Render Options
To pass rendering options to a Hirb helper as command options, a command has to define the options with the render_options method attribute:
# @render_options :fields=>[:a,:b] def list(options={}) [{:a=>1, :b=>2}, {:a=>10,:b=>11}] end # To see that the render_options method attribute actually passes the :fields option by default: >> list '-p' # or list '--pretend' Arguments: [] Global options: {:pretend=>true, :fields=>[:a, :b]} >> list +----+----+ | a | b | +----+----+ | 1 | 2 | | 10 | 11 | +----+----+ 2 rows in set # To create a vertical table, we can pass --vertical, one of the default global render options. >> list '-V' # or list '--vertical' *** 1. row *** a: 1 b: 2 ... # To get the original return value use the global option --render >> list '-r' # or list '--render' => [{:a=>1, :b=>2}, {:a=>10,:b=>11}]
Boson and Hirb
Since Boson uses Hirb's auto table helper by default, you may want to read up on its many options. To use any of them in commands, define them locally with render_options or globally by adding them under the :render_options key of the main config. What if you want to use your own helper class? No problem. Simply pass it with the global :class option.
When using the default helper, one of the most important options to define is :fields. Aside from controlling what fields are displayed, it’s used to set :values option attributes for related options i.e. :sort and :query. This provides handy option value aliasing via OptionParser. If you don’t set :fields, Boson will try to set its :values with field-related options i.e. :change_fields, :filters and :headers.
Public instance methods
Enables hirb and reads a config file from the main repo’s config/hirb.yml.
# File lib/boson/view.rb, line 53 def enable unless @enabled Hirb::View.enable(:config_file=>File.join(Boson.repo.config_dir, 'hirb.yml')) Hirb::Helpers::Table.filter_any = true end @enabled = true end
Renders any object via Hirb. Options are passed directly to Hirb::Console.render_output.
# File lib/boson/view.rb, line 63 def render(object, options={}, return_obj=false) if options[:inspect] puts(object.inspect) else render_object(object, options, return_obj) unless silent_object?(object) end end