Class: Rxhp::Element
Overview
Base class for all element-like things in RXHP.
Everything in the tree is a subclass of this, a subclass of String, something that responds nicely to to_s, or something that will cause en error at render-time :p
Direct Known Subclasses
Instance Attribute Summary collapse
-
#attributes ⇒ Object
A name => value map of attributes.
-
#children ⇒ Object
A list of child elements of this one.
Instance Method Summary collapse
-
#children? ⇒ Boolean
Whether or not this element has any child element.
-
#fill_options(options) ⇒ Object
Fill default render options.
-
#initialize(attributes = {}) ⇒ Element
constructor
Construct a new element with no children.
-
#render(options = {}) ⇒ Object
Return a flat HTML string for this element and all its’ decendants.
-
#render_children(options = {}) ⇒ Object
Iterate over all the children, calling render.
-
#render_string(string, options) ⇒ Object
Called when something that isn’t an element is found in the tree.
-
#valid? ⇒ Boolean
Whether there are any detectable problems with this element.
-
#validate! ⇒ Object
Check that this element is valid.
Methods included from Scope
current, define_element, #fragment, with_parent
Constructor Details
#initialize(attributes = {}) ⇒ Element
Construct a new element with no children.
18 19 20 21 22 |
# File 'lib/rxhp/element.rb', line 18 def initialize attributes = {} @attributes = attributes @children = Array.new validate! end |
Instance Attribute Details
#attributes ⇒ Object
A name => value map of attributes.
13 14 15 |
# File 'lib/rxhp/element.rb', line 13 def attributes @attributes end |
#children ⇒ Object
A list of child elements of this one.
15 16 17 |
# File 'lib/rxhp/element.rb', line 15 def children @children end |
Instance Method Details
#children? ⇒ Boolean
Whether or not this element has any child element.
25 26 27 |
# File 'lib/rxhp/element.rb', line 25 def children? !children.empty? end |
#fill_options(options) ⇒ Object
Fill default render options.
These are as defined for #render, with the addition of a :depth
value of 0. Other values aren’t guaranteed to stay fixed, check source for current values.
90 91 92 93 94 95 96 97 98 99 |
# File 'lib/rxhp/element.rb', line 90 def { :pretty => true, :format => Rxhp::HTML_FORMAT, :skip_doctype => false, :doctype => Rxhp::HTML_5, :depth => 0, :indent => 2, }.merge() end |
#render(options = {}) ⇒ Object
Return a flat HTML string for this element and all its’ decendants.
You probably don’t want to implement this yourself - interesting implementations are in Fragment, HtmlElement, and ComposableElement.
Valid options include:
:pretty
-
add whitespace to make the output more readable. Defaults to true.
:indent
-
how many spaces to use to indent when
:pretty
is true. :format
-
See Rxhp for values. Default is HTML_FORMAT
:skip_doctype
-
Self explanatory. Defaults to false.
:doctype
66 67 68 |
# File 'lib/rxhp/element.rb', line 66 def render = {} raise NotImplementedError.new end |
#render_children(options = {}) ⇒ Object
Iterate over all the children, calling render.
79 80 81 82 83 |
# File 'lib/rxhp/element.rb', line 79 def render_children = {} return if children.empty? flattened_children.map{ |child| render_child(child, ) }.join end |
#render_string(string, options) ⇒ Object
Called when something that isn’t an element is found in the tree.
Implemented in HtmlElement.
73 74 75 |
# File 'lib/rxhp/element.rb', line 73 def render_string string, raise NotImplementedError.new end |
#valid? ⇒ Boolean
Whether there are any detectable problems with this element.
See AttributeValidator for an example.
You probably don’t want to override this function in your subclasses; instead, you probably want to change #validate! to recognize your validations - all this does is check that it #validate! executes without raising a ValidationError.
37 38 39 40 41 42 43 44 |
# File 'lib/rxhp/element.rb', line 37 def valid? begin validate! true rescue Rxhp::ValidationError false end end |
#validate! ⇒ Object
Check that this element is valid.
49 50 51 |
# File 'lib/rxhp/element.rb', line 49 def validate! # no-op end |