Class: Rxhp::ComposableElement

Inherits:
Element
  • Object
show all
Includes:
AttributeValidator
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 included from AttributeValidator

match?, #valid_attributes?, #validate_attributes!

Methods inherited from Element

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

Methods included from Scope

current, define_element, #fragment, 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.



46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/rxhp/composable_element.rb', line 46

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)


40
41
42
# File 'lib/rxhp/composable_element.rb', line 40

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.



27
28
29
30
31
32
33
34
35
# File 'lib/rxhp/composable_element.rb', line 27

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

#validate!Object

Check that there are no detectable problems, such as invalid attributes.



17
18
19
20
# File 'lib/rxhp/composable_element.rb', line 17

def validate!
  super
  validate_attributes!
end