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.



1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
# File 'lib/forme.rb', line 1384

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