Class: Forme::Formatter

Inherits:
Object
  • Object
show all
Defined in:
lib/forme/transformers/formatter.rb

Overview

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.

Direct Known Subclasses

Bs3, Bs3ReadOnly, Disabled, ReadOnly

Defined Under Namespace

Classes: Bs3, Bs3ReadOnly, Disabled, ReadOnly

Constant Summary collapse

ATTRIBUTE_OPTIONS =

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.

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

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.

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

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.

Hash.new(0)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#attrObject (readonly)

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.


41
42
43
# File 'lib/forme/transformers/formatter.rb', line 41

def attr
  @attr
end

#formObject (readonly)

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


32
33
34
# File 'lib/forme/transformers/formatter.rb', line 32

def form
  @form
end

#inputObject (readonly)

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


36
37
38
# File 'lib/forme/transformers/formatter.rb', line 36

def input
  @input
end

#optsObject (readonly)

The opts hash of the input.


44
45
46
# File 'lib/forme/transformers/formatter.rb', line 44

def opts
  @opts
end

Class Method Details

.call(input) ⇒ Object

Create a new instance and call it


27
28
29
# File 'lib/forme/transformers/formatter.rb', line 27

def self.call(input)
  new.call(input)
end

Instance Method Details

#call(input) ⇒ Object

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.


57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/forme/transformers/formatter.rb', line 57

def call(input)
  @input = input
  @form = input.form
  attr = input.opts[:attr]
  @attr = attr ? attr.dup : {}
  @opts = input.opts
  normalize_options
  tag = if html = input.opts[:html]
    html = html.call(input) if html.respond_to?(:call)
    form.raw(html)
  else
    convert_to_tag(input.type)
  end
  tag = wrap_tag_with_label(tag) if @opts[:label]
  tag = wrap_tag_with_error(tag) if @opts[:error]
  tag = wrap(:helper, tag) if input.opts[:help]
  wrap_tag(tag)
end