Class: Forme::Serializer

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

Overview

Default serializer class used by the library. Any other serializer classes that want to produce html should probably subclass this class.

Registered as :default.

Direct Known Subclasses

AmericanTime, Bootstrap3, Bootstrap5

Defined Under Namespace

Classes: AmericanTime, Bootstrap3, Bootstrap5, PlainText

Constant Summary collapse

SELF_CLOSING =

Which tags are self closing (such tags ignore children).

[:img, :input]

Instance Method Summary collapse

Instance Method Details

#call(tag) ⇒ Object

Serialize the tag object to an html string. Supports Tag instances, Input instances (recursing into call with the result of formatting the input), arrays (recurses into call for each entry and joins the result), and (html escapes the string version of them, unless they include the Raw module, in which case no escaping is done).



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/forme/transformers/serializer.rb', line 19

def call(tag)
  case tag
  when Tag
    if SELF_CLOSING.include?(tag.type)
      "<#{tag.type}#{attr_html(tag.attr)}/>"
    else
      "#{serialize_open(tag)}#{call(tag.children)}#{serialize_close(tag)}"
    end
  when Input
    call(tag.format)
  when Array
    tag.map{|x| call(x)}.join
  when DateTime, Time
    format_time(tag)
  when Date
    format_date(tag)
  when BigDecimal
    tag.to_s('F')
  when Raw
    tag.to_s
  else
    Forme.h(tag)
  end
end

#serialize_close(tag) ⇒ Object

Returns the closing part of the given tag.



50
51
52
# File 'lib/forme/transformers/serializer.rb', line 50

def serialize_close(tag)
  "</#{tag.type}>"
end

#serialize_open(tag) ⇒ Object

Returns the opening part of the given tag.



45
46
47
# File 'lib/forme/transformers/serializer.rb', line 45

def serialize_open(tag)
  "<#{tag.type}#{attr_html(tag.attr)}>"
end