Class: Markbridge::Renderers::Discourse::RenderContext
- Inherits:
-
Object
- Object
- Markbridge::Renderers::Discourse::RenderContext
- Defined in:
- lib/markbridge/renderers/discourse/render_context.rb
Overview
Immutable context for rendering that wraps the parent chain Provides query methods to ask about parent elements without the renderer knowing about specific element types
Uses a hash-based cache for O(1) parent lookups instead of O(depth) scans
Instance Attribute Summary collapse
-
#depth ⇒ Object
readonly
Returns the value of attribute depth.
-
#parents ⇒ Object
readonly
Returns the value of attribute parents.
Instance Method Summary collapse
-
#count_parents(klass) ⇒ Integer
Count parents of given type O(1) instead of O(depth).
-
#find_parent(klass) ⇒ AST::Element?
Find closest parent of given type O(1) hash lookup instead of O(depth) scan.
-
#has_parent?(klass) ⇒ Boolean
Check if parent of type exists O(1) check.
- #html_mode? ⇒ Boolean
-
#initialize(parents = [], parent_cache: nil, html_mode: false) ⇒ RenderContext
constructor
A new instance of RenderContext.
-
#root? ⇒ Boolean
Check if we’re at the root (no parents).
-
#with_html_mode(value) ⇒ RenderContext
Create new context with html_mode toggled Preserves parent chain and cache.
-
#with_parent(element) ⇒ RenderContext
Create new context with element added to parent chain.
Constructor Details
#initialize(parents = [], parent_cache: nil, html_mode: false) ⇒ RenderContext
Returns a new instance of RenderContext.
14 15 16 17 18 19 |
# File 'lib/markbridge/renderers/discourse/render_context.rb', line 14 def initialize(parents = [], parent_cache: nil, html_mode: false) @parents = parents.freeze @depth = parents.size @parent_cache = parent_cache || build_cache(parents) @html_mode = html_mode end |
Instance Attribute Details
#depth ⇒ Object (readonly)
Returns the value of attribute depth.
12 13 14 |
# File 'lib/markbridge/renderers/discourse/render_context.rb', line 12 def depth @depth end |
#parents ⇒ Object (readonly)
Returns the value of attribute parents.
12 13 14 |
# File 'lib/markbridge/renderers/discourse/render_context.rb', line 12 def parents @parents end |
Instance Method Details
#count_parents(klass) ⇒ Integer
Count parents of given type O(1) instead of O(depth)
62 63 64 |
# File 'lib/markbridge/renderers/discourse/render_context.rb', line 62 def count_parents(klass) @parent_cache[klass]&.size || 0 end |
#find_parent(klass) ⇒ AST::Element?
Find closest parent of given type O(1) hash lookup instead of O(depth) scan
54 55 56 |
# File 'lib/markbridge/renderers/discourse/render_context.rb', line 54 def find_parent(klass) @parent_cache[klass]&.last end |
#has_parent?(klass) ⇒ Boolean
Check if parent of type exists O(1) check
70 71 72 |
# File 'lib/markbridge/renderers/discourse/render_context.rb', line 70 def has_parent?(klass) !@parent_cache[klass].nil? end |
#html_mode? ⇒ Boolean
46 47 48 |
# File 'lib/markbridge/renderers/discourse/render_context.rb', line 46 def html_mode? @html_mode end |
#root? ⇒ Boolean
Check if we’re at the root (no parents)
76 77 78 |
# File 'lib/markbridge/renderers/discourse/render_context.rb', line 76 def root? @depth.zero? end |
#with_html_mode(value) ⇒ RenderContext
Create new context with html_mode toggled Preserves parent chain and cache
41 42 43 |
# File 'lib/markbridge/renderers/discourse/render_context.rb', line 41 def with_html_mode(value) self.class.new(@parents, parent_cache: @parent_cache, html_mode: value) end |
#with_parent(element) ⇒ RenderContext
Create new context with element added to parent chain. Incrementally updates the cache (O(1)) instead of rebuilding from parents (O(depth)) — important for deeply-nested documents.
26 27 28 29 30 31 32 33 34 35 |
# File 'lib/markbridge/renderers/discourse/render_context.rb', line 26 def with_parent(element) new_parents = @parents + [element] new_cache = @parent_cache.dup element_class = element.class new_cache[element_class] ||= [] new_cache[element_class] = new_cache[element_class] + [element] self.class.new(new_parents, parent_cache: new_cache, html_mode: @html_mode) end |