Class: Prosereflect::Transform::Slice

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

Class Method Summary collapse

Instance Method Summary collapse

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

#contentObject (readonly)

Returns the value of attribute content.



8
9
10
# File 'lib/prosereflect/transform/slice.rb', line 8

def content
  @content
end

#open_endObject (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_startObject (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

.emptyObject

Create an empty slice



65
66
67
# File 'lib/prosereflect/transform/slice.rb', line 65

def self.empty
  new(Fragment.new([]), 0, 0)
end

Instance Method Details

#content_sizeObject

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)

Returns:

  • (Boolean)


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

Returns:

  • (Boolean)


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

#inspectObject



60
61
62
# File 'lib/prosereflect/transform/slice.rb', line 60

def inspect
  to_s
end

#sizeObject

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_sObject



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