Class: Rxhp::ComposableElement

Inherits:
CustomElement show all
Defined in:
lib/rxhp/composable_element.rb

Overview

This defines an element that is only a handy way to think of a tree of other elements - it has no real rendering by itself, just those of its' children.

Most of the time, those children will either be ComposableElement's in turn, or subclasses of Rxhp::HtmlElement

Instance Attribute Summary

Attributes inherited from Element

#attributes, #children

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from CustomElement

#validate!

Methods included from AttributeValidator

match?, #valid_attributes?, #validate_attributes!

Methods inherited from Element

#children?, #fill_options, #initialize, #render_children, #render_string, #valid?, #validate!

Methods included from Scope

current, define_element, with_parent

Constructor Details

This class inherits a constructor from Rxhp::Element

Class Method Details

.inherited(subclass) ⇒ Object

Automatically add a foo_bar method to Rxhp scopes when a FooBar subclass of this is created.


36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/rxhp/composable_element.rb', line 36

def self.inherited subclass
  Rxhp::AttributeValidator.inherited(subclass)
  full_name = subclass.name
  parts = full_name.split('::')
  klass_name = parts.pop
  namespace = Kernel
  parts.each do |part|
    namespace = namespace.const_get(part)
  end  # UpperCamelCase => under_scored

  tag_name = klass_name.gsub(/(.)([A-Z])/, '\1_\2').downcase

  Rxhp::Scope.define_element tag_name, subclass, namespace
end

Instance Method Details

#composeObject

Implement this method - return an Rxhp::Element subclass.

Yield Returns:

  • child elements

Raises:

  • (NotImplementedError)

30
31
32
# File 'lib/rxhp/composable_element.rb', line 30

def compose
  raise NotImplementedError.new
end

#render(options = {}) ⇒ Object

You don't want to implement this function in your subclasses - just reimplement compose instead.

This calls compose, provides the 'yield' magic, and callls render on the output.


17
18
19
20
21
22
23
24
25
# File 'lib/rxhp/composable_element.rb', line 17

def render options = {}
  validate!
  self.compose do
    # Allow 'yield' to embed all children
    self.children.each do |child|
      fragment child
    end
  end.render(options)
end