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.



466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
# File 'lib/htmlcom.rb', line 466

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, [value.first]]
  end.to_h

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

  klass = {dropdown: HtmlCom::Dropdown, textarea: HtmlCom::Textarea}

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

  h.each do |key, value|

    id = key
    type, content = value
    action = case type
    when :dropdown
      'Select'
    else
      'Enter'
    end

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

    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



511
512
513
# File 'lib/htmlcom.rb', line 511

def to_doc()
  @form.to_doc
end

#to_htmlObject



515
516
517
# File 'lib/htmlcom.rb', line 515

def to_html()
  @form.to_html
end