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.



459
460
461
462
463
464
465
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
# File 'lib/htmlcom.rb', line 459

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
end

Instance Method Details

#to_docObject



501
502
503
# File 'lib/htmlcom.rb', line 501

def to_doc()
  @form.to_doc
end

#to_htmlObject



505
506
507
# File 'lib/htmlcom.rb', line 505

def to_html()
  @form.to_doc.root.xml pretty: true
end