Class: PaintbrushSupport::BoundedColorElement

Inherits:
Object
  • Object
show all
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

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

#boundariesObject



33
34
35
# File 'lib/paintbrush_support/bounded_color_element.rb', line 33

def boundaries
  @boundaries ||= [open_index, close_index]
end

#close_indexObject



41
42
43
# File 'lib/paintbrush_support/bounded_color_element.rb', line 41

def close_index
  escaped_output.index(Escapes.close(index))
end

#codeObject



29
30
31
# File 'lib/paintbrush_support/bounded_color_element.rb', line 29

def code
  color_element.code
end

#indexObject



25
26
27
# File 'lib/paintbrush_support/bounded_color_element.rb', line 25

def index
  color_element.index
end

#inspectObject



21
22
23
# File 'lib/paintbrush_support/bounded_color_element.rb', line 21

def inspect
  "<#{self.class} boundaries=#{boundaries}>"
end

#open_indexObject



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

Returns:

  • (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