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.
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 that are is_a? klass (handles subclasses).
-
#find_parent(klass) ⇒ AST::Element?
Find closest parent that is_a? klass (handles subclasses).
-
#has_parent?(klass) ⇒ Boolean
Check if any parent is_a? klass (handles subclasses).
- #html_mode? ⇒ Boolean
-
#initialize(parents = [], 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.
-
#with_parent(element) ⇒ RenderContext
Create new context with element added to parent chain.
Constructor Details
#initialize(parents = [], html_mode: false) ⇒ RenderContext
Returns a new instance of RenderContext.
12 13 14 15 16 |
# File 'lib/markbridge/renderers/discourse/render_context.rb', line 12 def initialize(parents = [], html_mode: false) @parents = parents.freeze @depth = parents.size @html_mode = html_mode end |
Instance Attribute Details
#depth ⇒ Object (readonly)
Returns the value of attribute depth.
10 11 12 |
# File 'lib/markbridge/renderers/discourse/render_context.rb', line 10 def depth @depth end |
#parents ⇒ Object (readonly)
Returns the value of attribute parents.
10 11 12 |
# File 'lib/markbridge/renderers/discourse/render_context.rb', line 10 def parents @parents end |
Instance Method Details
#count_parents(klass) ⇒ Integer
Count parents that are is_a? klass (handles subclasses).
47 48 49 |
# File 'lib/markbridge/renderers/discourse/render_context.rb', line 47 def count_parents(klass) @parents.count { |parent| parent.is_a?(klass) } end |
#find_parent(klass) ⇒ AST::Element?
Find closest parent that is_a? klass (handles subclasses).
40 41 42 |
# File 'lib/markbridge/renderers/discourse/render_context.rb', line 40 def find_parent(klass) @parents.reverse_each.find { |parent| parent.is_a?(klass) } end |
#has_parent?(klass) ⇒ Boolean
Check if any parent is_a? klass (handles subclasses).
54 55 56 |
# File 'lib/markbridge/renderers/discourse/render_context.rb', line 54 def has_parent?(klass) @parents.any? { |parent| parent.is_a?(klass) } end |
#html_mode? ⇒ Boolean
33 34 35 |
# File 'lib/markbridge/renderers/discourse/render_context.rb', line 33 def html_mode? @html_mode end |
#root? ⇒ Boolean
Check if we’re at the root (no parents).
60 61 62 |
# File 'lib/markbridge/renderers/discourse/render_context.rb', line 60 def root? @depth.zero? end |
#with_html_mode(value) ⇒ RenderContext
Create new context with html_mode toggled.
28 29 30 |
# File 'lib/markbridge/renderers/discourse/render_context.rb', line 28 def with_html_mode(value) self.class.new(@parents, html_mode: value) end |
#with_parent(element) ⇒ RenderContext
Create new context with element added to parent chain.
21 22 23 |
# File 'lib/markbridge/renderers/discourse/render_context.rb', line 21 def with_parent(element) self.class.new(@parents + [element], html_mode: @html_mode) end |