Class: Prosereflect::Transform::Slice
- Inherits:
-
Object
- Object
- Prosereflect::Transform::Slice
- Defined in:
- lib/prosereflect/transform/slice.rb
Overview
Represents a slice of a document - a contiguous portion that can be inserted, deleted, or moved. Tracks open boundaries for proper joining.
Instance Attribute Summary collapse
-
#content ⇒ Object
readonly
Returns the value of attribute content.
-
#open_end ⇒ Object
readonly
Returns the value of attribute open_end.
-
#open_start ⇒ Object
readonly
Returns the value of attribute open_start.
Class Method Summary collapse
-
.empty ⇒ Object
Create an empty slice.
Instance Method Summary collapse
-
#content_size ⇒ Object
Size of just the content.
-
#cut(from = 0, to = nil) ⇒ Object
Cut the slice at given boundaries.
-
#empty? ⇒ Boolean
Check if this slice is empty (no content and no open boundaries).
-
#eq?(other) ⇒ Boolean
(also: #==)
Check equality.
-
#initialize(content, open_start = 0, open_end = 0) ⇒ Slice
constructor
A new instance of Slice.
- #inspect ⇒ Object
-
#size ⇒ Object
Total size of the slice including open boundaries.
- #to_s ⇒ Object
Constructor Details
#initialize(content, open_start = 0, open_end = 0) ⇒ Slice
Returns a new instance of Slice.
10 11 12 13 14 |
# File 'lib/prosereflect/transform/slice.rb', line 10 def initialize(content, open_start = 0, open_end = 0) @content = content @open_start = open_start @open_end = open_end end |
Instance Attribute Details
#content ⇒ Object (readonly)
Returns the value of attribute content.
8 9 10 |
# File 'lib/prosereflect/transform/slice.rb', line 8 def content @content end |
#open_end ⇒ Object (readonly)
Returns the value of attribute open_end.
8 9 10 |
# File 'lib/prosereflect/transform/slice.rb', line 8 def open_end @open_end end |
#open_start ⇒ Object (readonly)
Returns the value of attribute open_start.
8 9 10 |
# File 'lib/prosereflect/transform/slice.rb', line 8 def open_start @open_start end |
Class Method Details
Instance Method Details
#content_size ⇒ Object
Size of just the content
27 28 29 30 31 |
# File 'lib/prosereflect/transform/slice.rb', line 27 def content_size size = 0 @content.each { |node| size += node.node_size } size end |
#cut(from = 0, to = nil) ⇒ Object
Cut the slice at given boundaries
34 35 36 37 38 39 40 41 42 43 |
# File 'lib/prosereflect/transform/slice.rb', line 34 def cut(from = 0, to = nil) to ||= size if from.zero? && to == size return self end result = cut_internal(from, to) Slice.new(result[:content], result[:open_start], result[:open_end]) end |
#empty? ⇒ Boolean
Check if this slice is empty (no content and no open boundaries)
17 18 19 |
# File 'lib/prosereflect/transform/slice.rb', line 17 def empty? @content.empty? && @open_start.zero? && @open_end.zero? end |
#eq?(other) ⇒ Boolean Also known as: ==
Check equality
46 47 48 49 50 51 52 |
# File 'lib/prosereflect/transform/slice.rb', line 46 def eq?(other) return false unless other.is_a?(Slice) @open_start == other.open_start && @open_end == other.open_end && @content.to_a.map(&:to_h) == other.content.to_a.map(&:to_h) end |
#inspect ⇒ Object
60 61 62 |
# File 'lib/prosereflect/transform/slice.rb', line 60 def inspect to_s end |
#size ⇒ Object
Total size of the slice including open boundaries
22 23 24 |
# File 'lib/prosereflect/transform/slice.rb', line 22 def size content_size + @open_start + @open_end end |
#to_s ⇒ Object
56 57 58 |
# File 'lib/prosereflect/transform/slice.rb', line 56 def to_s "<Slice open_start=#{@open_start} open_end=#{@open_end} content=#{@content.length} items>" end |