Class: Heading

Inherits:
ActiveComponent::Base show all
Defined in:
lib/active_component/components/heading.rb

Constant Summary

Constants included from ActiveComponent

ActiveComponent::BLOCK_ELEMENTS, ActiveComponent::EMPTY_ELEMENTS, ActiveComponent::HEADING_ELEMENTS, ActiveComponent::HTML5_ELEMENTS, ActiveComponent::PHRASING_ELEMENTS, ActiveComponent::SECTION_ELEMENTS

Instance Attribute Summary collapse

Attributes inherited from ActiveComponent::Base

#attributes, #childrenHash, #node_content, #node_name, #parent, #title

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from ActiveComponent::Base

#<<, #<=>, #[], #add, #breadth, #breadth_each, #children, #class_name, #content, #content=, def_component_helper, def_html_sub_components, #depth, #detached_copy, #each, #each_leaf, #firstChild, #firstSibling, #freezeTree!, #hasChildren?, #hasnode_content?, #html_class, #in_degree, inherited, #init_component, #init_node, #isFirstSibling?, #isLastSibling?, #isLeaf?, #isOnlyChild?, #is_html_tag_wrapper?, #is_root?, json_create, #lastChild, #lastSibling, #length, #marshal_dump, #marshal_load, #nextSibling, #nodeDepth, #nodeHeight, #out_degree, #parentage, #preordered_each, #prepend, #previousSibling, #printTree, #remove!, #removeAll!, #removeFromParent!, #root, #siblings, #size, #to_json, #to_s

Methods included from Enumerable

#find_a, #includes_a?, #transmogrify

Methods included from ActiveComponent

#print_contents, #print_object, #print_tag, #wrap_contents

Constructor Details

#initialize(*args, &content_block) ⇒ Heading

Returns a new instance of Heading.



15
16
17
# File 'lib/active_component/components/heading.rb', line 15

def initialize(*args, &content_block)
  init_component(args, [:content, :title, :level, :attributes], &content_block)
end

Instance Attribute Details

#levelObject

Returns the value of attribute level.



5
6
7
# File 'lib/active_component/components/heading.rb', line 5

def level
  @level
end

Class Method Details

.has_parent_heading?(node) ⇒ Boolean

Checks whether a Heading exists in the node hierarchy above a given node

Returns:

  • (Boolean)


52
53
54
55
56
57
# File 'lib/active_component/components/heading.rb', line 52

def Heading.has_parent_heading?(node)
  !node.is_root? && (
    node.parent.siblings.includes_a?(Heading) || 
    Heading.has_parent_heading?(node.parent)
  )
end

.parent_heading(node) ⇒ Object

Retrieves the next Heading of the node hierarchy above a given node

Raises:

  • (ArgumentException)


46
47
48
49
# File 'lib/active_component/components/heading.rb', line 46

def Heading.parent_heading(node)
  raise ArgumentException, "Node has no heading parent." unless Heading.has_parent_heading?(node)
  node.parent.siblings.find_a(Heading) or Heading.parent_heading(node.parent)
end

Instance Method Details

#determine_levelObject

Determines the heading level by adopting the siblings’ one or by determining the parent’s one recursively



27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/active_component/components/heading.rb', line 27

def determine_level
  return 1     if is_root?
  return level if level.present?
  
  siblings_level or (
    if Heading.has_parent_heading?(self)
      Heading.parent_heading(self).determine_level + 1
    else
      1
    end
  )
end

#siblings_levelObject

Collects the level of sibling headings



41
42
43
# File 'lib/active_component/components/heading.rb', line 41

def siblings_level
  siblings.collect {|sib| sib.level if sib.is_a?(Heading)}.compact.min
end

#to_htmlObject



19
20
21
22
23
# File 'lib/active_component/components/heading.rb', line 19

def to_html
  @level ||= determine_level

  wrap_contents("h" + @level.to_s, content, nil, @attributes)
end