class Forme::Formatter

  1. lib/forme/bs3.rb
  2. lib/forme/bs5.rb
  3. lib/forme/transformers/formatter.rb
  4. show all
Superclass: Object

The default formatter used by the library. Any custom formatters should probably inherit from this formatter unless they have very special needs.

Unlike most other transformers which are registered as instances and use a functional style, this class is registered as a class due to the large amount of state it uses.

Registered as :default.

Methods

Public Class

  1. call

Public Instance

  1. attr
  2. call
  3. form
  4. input
  5. opts

Constants

ATTRIBUTE_BOOLEAN_OPTIONS = [:autofocus, :required, :disabled]  

Options copied from the options hash into the attributes hash, where a true value in the options hash sets the attribute value to the same name as the key.

ATTRIBUTE_OPTIONS = [:name, :id, :placeholder, :value, :style]  

These options are copied directly from the options hash to the the attributes hash, so they don’t need to be specified in the :attr option. However, they can be specified in both places, and if so, the :attr option version takes precedence.

CHECKBOX_MAP = Hash.new(0)  

Used to specify the value of the hidden input created for checkboxes. Since the default for an unspecified checkbox value is 1, the default is

  1. If the checkbox value is ‘t’, the hidden value is ‘f’, since that is

common usage for boolean values.

DATE_SELECT_FORMAT = '%02i'.freeze  
DEFAULT_DATETIME_ORDER = [:year, '-'.freeze, :month, '-'.freeze, :day, ' '.freeze, :hour, ':'.freeze, :minute, ':'.freeze, :second].freeze  
DEFAULT_DATE_ORDER = [:year, '-'.freeze, :month, '-'.freeze, :day].freeze  
DEFAULT_DATE_SELECT_OPS = {:year=>1900..2050, :month=>1..12, :day=>1..31, :hour=>0..23, :minute=>0..59, :second=>0..59}.freeze  

Attributes

attr [R]

The attributes to to set on the lower level Tag form returned. This are derived from the input‘s opts, but some processing is done on them.

form [R]

The Form instance for the receiver, taken from the input.

input [R]

The Input instance for the receiver. This is what the receiver converts to the lower level Tag form (or an array of them).

opts [R]

The opts hash of the input.

Public Class methods

call(input)

Create a new instance and call it

[show source]
   # File lib/forme/transformers/formatter.rb
27 def self.call(input)
28   new.call(input)
29 end

Public Instance methods

call(input)

Transform the input into a Tag instance (or an array of them), wrapping it with the form‘s wrapper, and the form’s error_handler and labeler if the input has an :error or :label options.

[show source]
   # File lib/forme/transformers/formatter.rb
57 def call(input)
58   @input = input
59   @form = input.form
60   attr = input.opts[:attr]
61   @attr = attr ? attr.dup : {}
62   @opts = input.opts
63   normalize_options
64   tag = if html = input.opts[:html]
65     html = html.call(input) if html.respond_to?(:call)
66     form.raw(html)
67   else
68     convert_to_tag(input.type)
69   end
70   tag = wrap_tag_with_label(tag) if @opts[:label]
71   tag = wrap_tag_with_error(tag) if @opts[:error]
72   tag = wrap(:helper, tag) if input.opts[:help]
73   wrap_tag(tag)
74 end