Module: View

Defined in:
lib/view.rb,
lib/view/helper.rb,
lib/view/railtie.rb,
lib/view/version.rb,
lib/view/formatter.rb,
lib/view/configuration.rb,
lib/view/formatters/self.rb,
lib/view/formatters/auto.rb,
lib/view/formatters/link.rb,
lib/view/formatters/date.rb,
lib/view/formatters/size.rb,
lib/view/formatters/array.rb,
lib/view/formatters/table.rb,
lib/view/formatters/guess.rb,
lib/view/formatters/phone.rb,
lib/view/formatters/image.rb,
lib/view/formatters/human.rb,
lib/view/formatters/blank.rb,
lib/view/formatters/boolean.rb,
lib/view/formatters/sentence.rb,
lib/view/formatters/currency.rb,
lib/view/formatters/datetime.rb,
lib/view/formatters/file_link.rb,
lib/view/formatters/precision.rb,
lib/view/formatters/html_safe.rb,
lib/view/formatters/delimited.rb,
lib/view/formatters/percentage.rb,
lib/view/formatters/definition_list.rb

Defined Under Namespace

Modules: Helper Classes: Array, Auto, Blank, Boolean, Configuration, Currency, Date, Datetime, DefinitionList, Delimited, FileLink, Formatter, Guess, HtmlSafe, Human, Image, Link, Percentage, Phone, Precision, Railtie, Self, Sentence, Size, Table

Constant Summary

VERSION =
'1.0.0.alpha.5'

Class Method Summary collapse

Class Method Details

.configurationConfiguration

Access to the configuration object



44
45
46
# File 'lib/view.rb', line 44

def self.configuration
  @configuration ||= Configuration.new
end

.configure {|config| ... } ⇒ Object

Configures this gem

Examples:

config/initializers/view.rb


View.configure do |config|
  config.default_formatter = :self
end

Yields:

  • (config)

    The configuration instance



57
58
59
# File 'lib/view.rb', line 57

def self.configure
  yield configuration
end

.format(value, options = {}, template = nil) {|formatter| ... } ⇒ String

This is the main method to use this gem. Any formatting from outside this gem should be done through this method (notwithstanding custom formatters).

In every day usage you will access it through the view helper method, because most formatters require a view to render links or images.

Examples:

Rendering a link

module PostsHelper
  def link_to_post
    View.format @post, { :as => :link }, self
  end
end

Options Hash (options):

  • :as (Symbol) — default: :auto

    The name of the formatter

  • :block_arguments (::Array) — default: []

    Adds arguments passed to the block of this method.

Yields:

  • (formatter)

    The block will be captured (using the template if possible) so you can use the formatter in more interesting ways.

See Also:



38
39
40
# File 'lib/view.rb', line 38

def self.format(value, options = {}, template = nil, &block)
  Formatter.format(value, options, template, &block)
end

.formattersObject

Contains a list of registered formatters



62
63
64
# File 'lib/view.rb', line 62

def self.formatters
  @formatters ||= []
end