Class: Forme::Input

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

Overview

High level abstract tag form, transformed by formatters into the lower level Tag form (or an array of them).

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(form, type, opts = {}) ⇒ Input

Set the form, type, and opts.



534
535
536
537
# File 'lib/forme.rb', line 534

def initialize(form, type, opts={})
  @form, @type = form, type
  @opts = (form.input_defaults[type.to_s] || {}).merge(opts)
end

Instance Attribute Details

#formObject (readonly)

The Form object related to the receiver.



503
504
505
# File 'lib/forme.rb', line 503

def form
  @form
end

#optsObject (readonly)

The options hash for the receiver. Here are some of the supported options used by the built-in formatter transformers:

:error

Set an error message, invoking the error_handler

:label

Set a label, invoking the labeler

:wrapper

Set a custom wrapper, overriding the form’s default

:labeler

Set a custom labeler, overriding the form’s default

:error_handler

Set a custom error_handler, overriding the form’s default

:attr

The attributes hash to use for the given tag, takes precedence over other options that set attributes.

:data

A hash of data-* attributes for the resulting tag. Keys in this hash will have attributes created with data- prepended to the attribute name.

:name

The name attribute to use

:id

The id attribute to use

:placeholder

The placeholder attribute to use

:value

The value attribute to use for input tags, the content of the textarea for textarea tags, or the selected option(s) for select tags.

:class

A class to use. Unlike other options, this is combined with the classes set in the :attr hash.

:disabled

Set the disabled attribute if true

:required

Set the required attribute if true

For other supported options, see the private methods in Formatter.



531
532
533
# File 'lib/forme.rb', line 531

def opts
  @opts
end

#typeObject (readonly)

The type of input, should be a symbol (e.g. :submit, :text, :select).



506
507
508
# File 'lib/forme.rb', line 506

def type
  @type
end

Instance Method Details

#formatObject

Transform the receiver into a lower level Tag form (or an array of them).



558
559
560
# File 'lib/forme.rb', line 558

def format
  form.format(self)
end

#merge_opts(hash) ⇒ Object

Replace the opts by merging the given hash into opts, without modifying opts.



541
542
543
# File 'lib/forme.rb', line 541

def merge_opts(hash)
  @opts = @opts.merge(hash)
end

#tag(*a, &block) ⇒ Object

Create a new Tag instance with the given arguments and block related to the receiver’s form.



547
548
549
# File 'lib/forme.rb', line 547

def tag(*a, &block)
  form._tag(*a, &block)
end

#to_sObject

Return a string containing the serialized content of the receiver.



552
553
554
# File 'lib/forme.rb', line 552

def to_s
  form.serialize(self)
end