Class: View::Auto

Inherits:
Formatter show all
Defined in:
lib/view/formatters/auto.rb

Overview

The auto formatter tries to figure out what other formatter should be the most appropriate to use and delegates the formatting to that formatter.

This is fully configurable too. See Auto.add

Instance Attribute Summary

Attributes inherited from Formatter

#block, #template, #value

Class Method Summary (collapse)

Instance Method Summary (collapse)

Methods inherited from Formatter

#all_options, as, #format!, inherited, #options, skip_blank_formatter, #to_s, type

Class Method Details

+ (Object) add(formatter_name) { ... }

Adds behavior to check which view should automatically be used.

Examples:


View::Auto.add :boolean do
  value == true || value == false
end

Parameters:

  • formatter_name (Symbol)

    The name of the formatter

Yields:

  • The block is eval'd inside the formatter, so you can use the instance methods that are available for every formatters, like value and options.

Yield Returns:

  • (true, false)

    The first block to evaluate to true will determine the formatter. New blocks are checked first.



27
28
29
# File 'lib/view/formatters/auto.rb', line 27

def self.add(formatter_name, &block)
  auto_formatters.unshift(:formatter => formatter_name, :block => block)
end

Instance Method Details

- (Object) format



31
32
33
# File 'lib/view/formatters/auto.rb', line 31

def format
  formatted_value
end