Class: Coradoc::Introspection::ElementCounter

Inherits:
Visitor::Base show all
Defined in:
lib/coradoc/introspection/element_counter.rb

Overview

Visitor that walks a document and counts each CoreModel node by its type key. Used by Introspection.count_element_types to back the Coradoc.document_stats API.

Typed StructuralElement / Block nodes are counted under their element_type (semantic identity). Other nodes fall back to a snake_case rendering of their class name.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Visitor::Base

#visit_abbreviation, #visit_annotation_block, #visit_array, #visit_bibliography, #visit_bibliography_entry, #visit_block, #visit_definition_item, #visit_definition_list, #visit_element_attribute, #visit_footnote, #visit_footnote_reference, #visit_image, #visit_inline_element, #visit_list_block, #visit_list_item, #visit_metadata, #visit_metadata_entry, #visit_structural_element, #visit_table, #visit_table_cell, #visit_table_row, #visit_term, #visit_toc, #visit_toc_entry, #visit_unknown

Constructor Details

#initializeElementCounter

Returns a new instance of ElementCounter.



13
14
15
# File 'lib/coradoc/introspection/element_counter.rb', line 13

def initialize
  @counts = Hash.new(0)
end

Instance Attribute Details

#countsObject (readonly)

Returns the value of attribute counts.



17
18
19
# File 'lib/coradoc/introspection/element_counter.rb', line 17

def counts
  @counts
end

Instance Method Details

#visit(element) ⇒ Object



19
20
21
22
23
24
# File 'lib/coradoc/introspection/element_counter.rb', line 19

def visit(element)
  return super(element) unless element.is_a?(CoreModel::Base)

  @counts[type_key_for(element)] += 1
  super(element)
end