Class: Forme::Sinatra::Form

Inherits:
Form
  • Object
show all
Defined in:
lib/forme/sinatra.rb

Overview

Subclass used when using Forme/Sinatra ERB integration. Handles integrating into the view template so that methods with blocks can inject strings into the output.

Instance Attribute Summary collapse

Attributes inherited from Form

#error_handler, #formatter, #inputs_wrapper, #labeler, #obj, #opts, #serializer, #wrapper

Instance Method Summary collapse

Methods inherited from Form

#<<, #_input, #_tag, #button, #close, form, #format, #input, #open, #serialize, #transform, #transformer

Constructor Details

#initializeForm

Set the template output object when initializing.



14
15
16
17
# File 'lib/forme/sinatra.rb', line 14

def initialize(*)
  super
  @output = @opts[:output]
end

Instance Attribute Details

#outputObject (readonly)

Template output object, where serialized output gets injected.



11
12
13
# File 'lib/forme/sinatra.rb', line 11

def output
  @output
end

Instance Method Details

#emit(tag) ⇒ Object

Serialize the tag and inject it into the output.



20
21
22
# File 'lib/forme/sinatra.rb', line 20

def emit(tag)
  output << tag.to_s
end

#form(*a, &block) ⇒ Object

Always return nil, so that use with <%= doesn’t cause multiple things to be output.



33
34
35
36
# File 'lib/forme/sinatra.rb', line 33

def form(*a, &block)
  super
  nil
end

#inputs(*a) ⇒ Object

Always return nil, so that use with <%= doesn’t cause multiple things to be output.



26
27
28
29
# File 'lib/forme/sinatra.rb', line 26

def inputs(*a)
  super
  nil
end

#tag(type, attr = {}, children = []) ⇒ Object

If a block is provided, inject an opening tag into the output, inject any given children into the output, yield to the block, inject a closing tag into the output, and the return nil so that usage with <%= doesn’t cause multiple things to be output. If a block is not given, just return the tag created.



43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/forme/sinatra.rb', line 43

def tag(type, attr={}, children=[])
  tag = _tag(type, attr, children)
  if block_given?
    emit(serializer.serialize_open(tag)) if serializer.respond_to?(:serialize_open)
    children.each{|c| emit(c)}
    yield self
    emit(serializer.serialize_close(tag)) if serializer.respond_to?(:serialize_close)
    nil
  else
    tag
  end
end