Module | Hirb::View |
In: |
lib/hirb/view.rb
|
This class is responsible for managing all view-related functionality.
Let‘s create a simple view for Hash objects:
$ irb -rubygems >> require 'hirb' =>true >> Hirb.enable =>nil >> require 'yaml' =>true # A view method is the smallest view >> def yaml(output); output.to_yaml; end => nil # Add the view >> Hirb.add_view Hash, :method=>:yaml => true # Hashes now appear as yaml >> {:a=>1, :b=>{:c=>3}} --- :a : 1 :b : :c : 3 => true
Another way of creating a view is a Helper class:
# Create yaml view class >> class Hirb::Helpers::Yaml; def self.render(output, options={}); output.to_yaml; end ;end =>nil # Add the view >> Hirb.add_view Hash, :class=>Hirb::Helpers::Yaml =>true # Hashes appear as yaml like above ...
To configure the above Helper class as a view, either pass Hirb.enable a hash:
# In .irbrc require 'hirb' # View class needs to come before enable() class Hirb::Helpers::Yaml; def self.render(output, options={}); output.to_yaml; end ;end Hirb.enable :output=>{"Hash"=>{:class=>"Hirb::Helpers::Yaml"}}
Or create a config file at config/hirb.yml or ~/.hirb.yml:
# The config file for the yaml example would look like: # --- # :output : # Hash : # :class : Hirb::Helpers::Yaml # In .irbrc require 'hirb' # View class needs to come before enable() class Hirb::Helpers::Yaml; def self.render(output, options={}); output.to_yaml; end ;end Hirb.enable
For more about configuring Hirb, see the Config Files section in Hirb.
DEFAULT_WIDTH | = | 120 |
DEFAULT_HEIGHT | = | 40 |
config | [R] | |
render_method | [RW] |
Captures STDOUT and renders it using render_method(). The main use case is to conditionally page captured stdout.
This activates view functionality i.e. the formatter, pager and size detection. If irb exists, it overrides irb‘s output method with Hirb::View.view_output. When called multiple times, new configs are merged into the existing config. If using Wirble, you should call this after it. The view configuration can be specified in a hash via a config file, or as options to this method. In addition to the config keys mentioned in Hirb, options also take the following keys:
Examples:
Hirb.enable Hirb.enable :formatter=>false
A lambda or proc which handles the final formatted object. Although this pages/puts the object by default, it could be set to do other things i.e. write the formatted object to a file.
Resizes the console width and height for use with the table and pager i.e. after having resized the console window. *nix users should only have to call this method. Non-*nix users should call this method with explicit width and height. If you don‘t know your width and height, in irb play with "a"* width to find width and puts "a\n" * height to find height.
This is the main method of this class. When view is enabled, this method searches for a formatter it can use for the output and if successful renders it using render_method(). The options this method takes are helper config hashes as described in Hirb::Formatter.format_output(). Returns true if successful and false if no formatting is done or if not enabled.