Class: Formular::HtmlBlock
- Inherits:
-
Object
- Object
- Formular::HtmlBlock
- 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
-
#element ⇒ Object
readonly
Returns the value of attribute element.
-
#fn ⇒ Object
readonly
Returns the value of attribute fn.
Instance Method Summary collapse
-
#call ⇒ Object
this calls our html function passing the element instance as a variable.
-
#closed_start_tag ⇒ Object
return a closed start tag (e.g. <input name=“body”/>).
-
#concat(content) ⇒ Object
append a string to the output buffer.
-
#end_tag ⇒ Object
returns the end/ closing tag for an element.
-
#initialize(element, fn) ⇒ HtmlBlock
constructor
A new instance of HtmlBlock.
-
#start_tag ⇒ Object
return the start/opening tag with the element attributes hash converted into valid html attributes.
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.
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
#element ⇒ Object (readonly)
Returns the value of attribute element.
11 12 13 |
# File 'lib/formular/html_block.rb', line 11 def element @element end |
#fn ⇒ Object (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
#call ⇒ Object
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_tag ⇒ Object
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_tag ⇒ Object
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_tag ⇒ Object
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 |