Class: Formation::Printer

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(form) ⇒ Printer

Returns a new instance of Printer.



5
6
7
# File 'lib/formation/printer.rb', line 5

def initialize(form)
  @form = form
end

Instance Attribute Details

#formObject (readonly)

Returns the value of attribute form.



3
4
5
# File 'lib/formation/printer.rb', line 3

def form
  @form
end

Instance Method Details



9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/formation/printer.rb', line 9

def print
  html = ''
  html << print_errors(form.errors)
  form.elements.each do |element|
    if element.field?
      html << print_field(element)
    else
      html << print_fieldset(element)
    end
  end
  html
end


22
23
24
25
26
# File 'lib/formation/printer.rb', line 22

def print_error(error)
  <<-HTML
    <p style="color: red">#{error}</p>
  HTML
end


28
29
30
# File 'lib/formation/printer.rb', line 28

def print_errors(errors)
  errors.map { |error| print_error error }.join("\n")
end


32
33
34
35
36
37
38
39
# File 'lib/formation/printer.rb', line 32

def print_field(field)
  <<-HTML
    <li>
      <label>#{field.label}</label>
      #{field.type.to_html}
    </li>
  HTML
end


41
42
43
44
45
46
47
48
49
50
# File 'lib/formation/printer.rb', line 41

def print_fieldset(fieldset)
  <<-HTML
    <fieldset>
      <legend>#{fieldset.legend}</legend>
      <ol>
        #{fieldset.fields.map { |f| print_field f }.join("\n")}
      </ol>
    </fieldset>
  HTML
end