Class: Forme::Serializer::PlainText

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

Overview

Serializer class that converts tags to plain text strings.

Registered at :text.

Instance Method Summary collapse

Instance Method Details

#call(tag) ⇒ Object

Serialize the tag to plain text string.



1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
# File 'lib/forme.rb', line 1142

def call(tag)
  case tag
  when Tag
    case tag.type.to_sym
    when :input
      case tag.attr[:type].to_sym
      when :radio, :checkbox
        tag.attr[:checked] ? '_X_' : '___'
      when :submit, :reset, :hidden
        ''
      when :password
        "********\n"
      else
        "#{tag.attr[:value].to_s}\n"
      end
    when :select
      "\n#{call(tag.children)}"
    when :option
      "#{call([tag.attr[:selected] ? '_X_ ' : '___ ', tag.children])}\n"
    when :textarea, :label
      "#{call(tag.children)}\n"
    when :legend
      v = call(tag.children)
      "#{v}\n#{'-' * v.length}\n"
    else
      call(tag.children)
    end
  when Input
    call(tag.format)
  when Array
    tag.map{|x| call(x)}.join
  else
    tag.to_s
  end
end