Class: Formular::HtmlBlock

Inherits:
Object
  • Object
show all
Defined in:
lib/formular/html_block.rb

Overview

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.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(element, fn) ⇒ HtmlBlock

Returns a new instance of HtmlBlock.



6
7
8
9
10
# File 'lib/formular/html_block.rb', line 6

def initialize(element, fn)
  @fn = fn
  @element = element
  @context = get_context
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m, *args, &blk) ⇒ Object (private)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

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

Since:

  • 0.1.0



66
67
68
# File 'lib/formular/html_block.rb', line 66

def method_missing(m, *args, &blk)
  @context.__send__(m, *args, &blk)
end

Instance Attribute Details

#elementObject (readonly)

Returns the value of attribute element.



11
12
13
# File 'lib/formular/html_block.rb', line 11

def element
  @element
end

#fnObject (readonly)

Returns the value of attribute fn.



11
12
13
# File 'lib/formular/html_block.rb', line 11

def fn
  @fn
end

Instance Method Details

#callObject

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



15
16
17
18
# File 'lib/formular/html_block.rb', line 15

def call
  @output = ''
  instance_exec(element, &fn).to_s
end

#closed_start_tagObject

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



37
38
39
# File 'lib/formular/html_block.rb', line 37

def closed_start_tag
  start_tag.gsub(/\>$/, '/>')
end

#concat(content) ⇒ Object

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



22
23
24
# File 'lib/formular/html_block.rb', line 22

def concat(content)
  @output << content.to_s
end

#end_tagObject

returns the end/ closing tag for an element



42
43
44
# File 'lib/formular/html_block.rb', line 42

def end_tag
  "</#{element.tag}>"
end

#start_tagObject

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



28
29
30
31
32
33
34
# 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