Class: PaintbrushSupport::BoundedColorElement
- Inherits:
-
Object
- Object
- PaintbrushSupport::BoundedColorElement
- Defined in:
- lib/paintbrush_support/bounded_color_element.rb
Overview
Wraps a Paintbrush::ColorElement instance and maps its start and end boundaries within a compiled escaped string by matching specific unique (indexed) escape codes. Provides ‘#surround?` for detecting if another element exists within the current element’s boundaries.
Instance Method Summary collapse
- #boundaries ⇒ Object
- #close_index ⇒ Object
- #code ⇒ Object
- #index ⇒ Object
-
#initialize(color_element:, escaped_output:) ⇒ BoundedColorElement
constructor
A new instance of BoundedColorElement.
- #inspect ⇒ Object
- #open_index ⇒ Object
- #surround?(element) ⇒ Boolean
Constructor Details
#initialize(color_element:, escaped_output:) ⇒ BoundedColorElement
Returns a new instance of BoundedColorElement.
8 9 10 11 |
# File 'lib/paintbrush_support/bounded_color_element.rb', line 8 def initialize(color_element:, escaped_output:) @color_element = color_element @escaped_output = escaped_output end |
Instance Method Details
#boundaries ⇒ Object
33 34 35 |
# File 'lib/paintbrush_support/bounded_color_element.rb', line 33 def boundaries @boundaries ||= [open_index, close_index] end |
#close_index ⇒ Object
41 42 43 |
# File 'lib/paintbrush_support/bounded_color_element.rb', line 41 def close_index escaped_output.index(Escapes.close(index)) end |
#code ⇒ Object
29 30 31 |
# File 'lib/paintbrush_support/bounded_color_element.rb', line 29 def code color_element.code end |
#index ⇒ Object
25 26 27 |
# File 'lib/paintbrush_support/bounded_color_element.rb', line 25 def index color_element.index end |
#inspect ⇒ Object
21 22 23 |
# File 'lib/paintbrush_support/bounded_color_element.rb', line 21 def inspect "<#{self.class} boundaries=#{boundaries}>" end |
#open_index ⇒ Object
37 38 39 |
# File 'lib/paintbrush_support/bounded_color_element.rb', line 37 def open_index @open_index ||= escaped_output.index(Escapes.open(index)) end |
#surround?(element) ⇒ Boolean
13 14 15 16 17 18 19 |
# File 'lib/paintbrush_support/bounded_color_element.rb', line 13 def surround?(element) return false if element == self return false unless element.open_index.between?(open_index, close_index) return false unless element.close_index.between?(open_index, close_index) true end |