Class: Rxhp::Element

Inherits:
Object
  • Object
show all
Includes:
Scope
Defined in:
lib/rxhp/element.rb

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

CustomElement, Fragment, HtmlElement

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Scope

current, define_element, 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

#attributesObject

A name => value map of attributes.



13
14
15
# File 'lib/rxhp/element.rb', line 13

def attributes
  @attributes
end

#childrenObject

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.

Returns:

  • (Boolean)


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 fill_options options
  {
    :pretty => true,
    :format => Rxhp::HTML_FORMAT,
    :skip_doctype => false,
    :doctype => Rxhp::HTML_5,
    :depth => 0,
    :indent => 2,
  }.merge(options)
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

See Rxhp for values. Default is HTML_5

Raises:

  • (NotImplementedError)


66
67
68
# File 'lib/rxhp/element.rb', line 66

def render options = {}
  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 options = {}
  return if children.empty?

  flattened_children.map{ |child| render_child(child, options) }.join
end

#render_string(string, options) ⇒ Object

Called when something that isn’t an element is found in the tree.

Implemented in HtmlElement.

Raises:

  • (NotImplementedError)


73
74
75
# File 'lib/rxhp/element.rb', line 73

def render_string string, options
  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.

Returns:

  • (Boolean)


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.

Raises:

  • Rxhp::ValidationError if a problem is found.



49
50
51
# File 'lib/rxhp/element.rb', line 49

def validate!
  # no-op
end