Class: HtmlCom::FormBuilder

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

Overview

e.g. inputs = [:textarea], audiotype: [:dropdown, ‘gsm’], voice: [:dropdown, ‘Stuart’]

options = {audiotype: ['gsm', 'wav'], voice: %w(Stuart Kirsty Andrew)}

fb = HtmlCom::FormBuilder.new(inputs: inputs, options: options) puts fb.to_html

Instance Method Summary collapse

Constructor Details

#initialize(inputs: {}, options: {}, id: 'form1', method: :get, action: '', debug: false) ⇒ FormBuilder

Returns a new instance of FormBuilder.



502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
# File 'lib/htmlcom.rb', line 502

def initialize(inputs: {}, options: {}, id: 'form1', method: :get, action: '', debug: false)

  @debug  = debug

  h = inputs.map do |key, value|

    if debug then
      puts 'id: ' + key.inspect
      puts 'klass: ' + value.first.inspect
    end

    [key.to_sym, value]
  end.to_h

  if options then
    options.each do |key, value|
      h[key.to_sym] << value
    end
  end

  klass = {
    dropdown: HtmlCom::Dropdown,
    textarea: HtmlCom::Textarea,
    text: HtmlCom::Text,
    hidden: HtmlCom::Hidden
  }

  @form = HtmlCom::Form.new(id: id, method: method, action: action)

  h.each do |key, value|

    id = key
    puts 'value: ' + value.inspect
    type, content = value
    action = case type.to_sym
    when :dropdown
      content = value.last
      'Select'
    else
      'Enter'
    end

    puts 'type: ' + type.inspect
    obj = klass[type.to_sym].new content, id: id, label: action + ' ' + id.to_s
    puts 'after klass'

    obj.to_doc.root.element(type.to_s).elements.each do |e|
      @form.html_element.add e
    end

  end

  @form.add_submit

end

Instance Method Details

#to_docObject



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

def to_doc()
  @form.to_doc
end

#to_htmlObject



562
563
564
# File 'lib/htmlcom.rb', line 562

def to_html()
  @form.to_html
end