Module: Hirb::View

### Home Classes Methods

### In Files

- lib/hirb/view.rb

### Methods

- ::add
- ::capture_and_render
- ::disable
- ::enable
- ::enabled?
- ::formatter_config
- ::height
- ::reset_render_method
- ::resize
- ::toggle_formatter
- ::toggle_pager
- ::view_output
- ::width

### Files

- LICENSE.txt
- README.rdoc

### Class/Module Index

Quicksearch

- Hirb
- Hirb::Console
- Hirb::DynamicView
- Hirb::Formatter
- Hirb::Helpers
- Hirb::Helpers::AutoTable
- Hirb::Helpers::ObjectTable
- Hirb::Helpers::ParentChildTree
- Hirb::Helpers::Table
- Hirb::Helpers::Table::Filters
- Hirb::Helpers::Table::Resizer
- Hirb::Helpers::Table::TooManyFieldsForWidthError
- Hirb::Helpers::Tree
- Hirb::Helpers::Tree::Node
- Hirb::Helpers::Tree::Node::MissingLevelError
- Hirb::Helpers::Tree::Node::MissingValueError
- Hirb::Helpers::Tree::ParentlessNodeError
- Hirb::Helpers::VerticalTable
- Hirb::Menu
- Hirb::Menu::Error
- Hirb::ObjectMethods
- Hirb::Pager
- Hirb::String
- Hirb::Util
- Hirb::View
- Hirb::Views

No matching classes.

# Hirb::View

This class is responsible for managing all view-related functionality.

## Create a View

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 ...

## Configure a View

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.

### Constants

DEFAULT_HEIGHT
DEFAULT_WIDTH

### Attributes

config[R]
render_method[RW]

### Public Class Methods

add(klass, view_config) click to toggle source

Adds a view when View is enabled. See Formatter.add_view for more details.

    # File lib/hirb/view.rb, line 166
    def add(klass, view_config)
      if enabled?
        formatter.add_view(klass, view_config)
      else
        puts "View must be enabled to add a view"
      end
    end

capture_and_render(&block) click to toggle source

Captures STDOUT and renders it using render_method(). The main use case is to conditionally page captured stdout.

    # File lib/hirb/view.rb, line 134
    def capture_and_render(&block)
      render_method.call Util.capture_stdout(&block)
    end

disable() click to toggle source

Disable’s Hirb’s output and revert’s irb’s output method if irb exists.

    # File lib/hirb/view.rb, line 98
    def disable
      @enabled = false
      unalias_output_method(@output_method) if @output_method
      false
    end

enable(options={}, &block) click to toggle source

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:

#### Options:

- config_file: Name of config file(s) that are merged into existing config

- output_method: Specify an object’s class and instance method (separated by a period) to be realiased with hirb’s view system. The instance method should take a string to be output. Default is IRB::Irb.output_value if using irb.

Examples:

    Hirb.enable
    Hirb.enable :formatter=>false, :output_method=>"Mini.output"

    # File lib/hirb/view.rb, line 81
    def enable(options={}, &block)
      Array(options.delete(:config_file)).each {|e|
        @new_config_file = true
        Hirb.config_files << e
      }
      enable_output_method(options.delete(:output_method))
      merge_or_load_config options
      resize(config[:width], config[:height])
      @enabled = true
    end

enabled?() click to toggle source

Indicates if Hirb::View is enabled.

    # File lib/hirb/view.rb, line 93
    def enabled?
      @enabled || false
    end

formatter_config() click to toggle source

Current formatter config, storing a hash of all static views

    # File lib/hirb/view.rb, line 161
    def formatter_config
      formatter.config
    end

height() click to toggle source

Current console height

    # File lib/hirb/view.rb, line 156
    def height
      config ? config[:height] : DEFAULT_HEIGHT
    end

reset_render_method() click to toggle source

Resets render_method back to its default.

    # File lib/hirb/view.rb, line 146
    def reset_render_method
      @render_method = default_render_method
    end

resize(width=nil, height=nil) click to toggle source

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 “an” * height to find height.

    # File lib/hirb/view.rb, line 117
    def resize(width=nil, height=nil)
      config[:width], config[:height] = determine_terminal_size(width, height)
      pager.resize(config[:width], config[:height])
    end

toggle_formatter() click to toggle source

Toggles formatter on or off.

    # File lib/hirb/view.rb, line 110
    def toggle_formatter
      config[:formatter] = !config[:formatter]
    end

toggle_pager() click to toggle source

Toggles pager on or off. The pager only works while Hirb::View is enabled.

    # File lib/hirb/view.rb, line 105
    def toggle_pager
      config[:pager] = !config[:pager]
    end

view_output(output, options={}) click to toggle source

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.

    # File lib/hirb/view.rb, line 125
    def view_output(output, options={})
      enabled? && config[:formatter] && render_output(output, options)
    rescue Exception=>e
      index = (obj = e.backtrace.find {|f| f =~ /^\(eval\)/}) ? e.backtrace.index(obj) : e.backtrace.length
      $stderr.puts "Hirb Error: #{e.message}", e.backtrace.slice(0,index).map {|e| "    " + e }
      true
    end

width() click to toggle source

Current console width

    # File lib/hirb/view.rb, line 151
    def width
      config ? config[:width] : DEFAULT_WIDTH
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.