class Formular::HtmlBlock

the HtmlBlock class is responsible for converting an element into an html string using the provided function providing the element as a variable to that function. this class also provides some simple helpers to make it easier to define your html.

Attributes

element[R]
fn[R]

Public Class Methods

new(element, fn) click to toggle source
# File lib/formular/html_block.rb, line 6
def initialize(element, fn)
  @fn = fn
  @element = element
  @context = get_context
end

Public Instance Methods

call() click to toggle source

this calls our html function passing the element instance as a variable. It returns our html as a string

# File lib/formular/html_block.rb, line 15
def call
  @output = ''
  instance_exec(element, &fn).to_s
end
closed_start_tag() click to toggle source

return a closed start tag (e.g. <input name=“body”/>)

# File lib/formular/html_block.rb, line 37
def closed_start_tag
  start_tag.gsub(/\>$/, '/>')
end
concat(content) click to toggle source

append a string to the output buffer. Useful when your html block is a bit more than one line

# File lib/formular/html_block.rb, line 22
def concat(content)
  @output << content.to_s
end
end_tag() click to toggle source

returns the end/ closing tag for an element

# File lib/formular/html_block.rb, line 42
def end_tag
  "</#{element.tag}>"
end
start_tag() click to toggle source

return the start/opening tag with the element attributes hash converted into valid html attributes

# File lib/formular/html_block.rb, line 28
def start_tag
  if element.attributes.empty?
    "<#{element.tag}>"
  else
    "<#{element.tag} #{element.attributes.to_html}>"
  end
end

Private Instance Methods

get_context() click to toggle source
# File lib/formular/html_block.rb, line 52
def get_context
  fn.binding.receiver
end
method_missing(m, *args, &blk) click to toggle source

Forward missing methods to the current context. This allows to access views local variables from nested content blocks.

@since 0.1.0 @api private

# File lib/formular/html_block.rb, line 66
def method_missing(m, *args, &blk)
  @context.__send__(m, *args, &blk)
end