Class: Formation::Printer
- Inherits:
-
Object
- Object
- Formation::Printer
- Defined in:
- lib/formation/printer.rb
Instance Attribute Summary collapse
-
#form ⇒ Object
readonly
Returns the value of attribute form.
Instance Method Summary collapse
-
#initialize(form) ⇒ Printer
constructor
A new instance of Printer.
- #print ⇒ Object
- #print_error(error) ⇒ Object
- #print_errors(errors) ⇒ Object
- #print_field(field) ⇒ Object
- #print_fieldset(fieldset) ⇒ Object
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
#form ⇒ Object (readonly)
Returns the value of attribute form.
3 4 5 |
# File 'lib/formation/printer.rb', line 3 def form @form end |
Instance Method Details
#print ⇒ Object
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 |
#print_error(error) ⇒ Object
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 |
#print_errors(errors) ⇒ Object
28 29 30 |
# File 'lib/formation/printer.rb', line 28 def print_errors(errors) errors.map { |error| print_error error }.join("\n") end |
#print_field(field) ⇒ Object
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 |
#print_fieldset(fieldset) ⇒ Object
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 |