Class Hirb::Formatter

  1. lib/hirb/formatter.rb
Parent: Object

A Formatter object formats an output object (using Formatter.format_output) into a string based on the views defined for its class and/or ancestry.


public instance

  1. add_view
  2. config
  3. format_output


dynamic_config [RW] This config is used by Formatter.format_output to lazily load dynamic views defined with Hirb::DynamicView. This hash has the same format as Formatter.config.

Public instance methods

add_view (klass, view_config)

Adds the view for the given class and view hash config. See Formatter.config for valid keys for view hash.

[show source]
# File lib/hirb/formatter.rb, line 37
    def add_view(klass, view_config)
      @config[klass.to_s] = view_config
config ()

A hash of Ruby class strings mapped to view hashes. A view hash must have at least a :method, :output_method or :class option for a view to be applied to an output. A view hash has the following keys:

Specifies a global (Kernel) method to do the formatting.
Specifies a class to do the formatting, using its render() class method. If a symbol it’s converted to a corresponding Hirb::Helpers::* class if it exists.
Specifies a method or proc to call on output before passing it to a helper. If the output is an array, it’s applied to every element in the array.
Options to pass the helper method or class.
Boolean which when true causes subclasses of the output class to inherit its config. This doesn’t effect the current output class. Defaults to false. This is used by ActiveRecord classes.
  {'WWW::Delicious::Element'=>{:class=>'Hirb::Helpers::ObjectTable', :ancestor=>true, :options=>{:max_width=>180}}}
  {'Date'=>{:class=>:auto_table, :ancestor=>true}}
[show source]
# File lib/hirb/formatter.rb, line 32
    def config
format_output (output, options={}, &block)

This method looks for an output object’s view in Formatter.config and then Formatter.dynamic_config. If a view is found, a stringified view is returned based on the object. If no view is found, nil is returned. The options this class takes are a view hash as described in Formatter.config. These options will be merged with any existing helper config hash an output class has in Formatter.config. Any block given is passed along to a helper class.

[show source]
# File lib/hirb/formatter.rb, line 47
    def format_output(output, options={}, &block)
      output_class = determine_output_class(output)
      options = parse_console_options(options) if options.delete(:console)
      options = Util.recursive_hash_merge(klass_config(output_class), options)
      _format_output(output, options, &block)