Class: Prosereflect::Transform::ReplaceStep

Inherits:
Step
  • Object
show all
Defined in:
lib/prosereflect/transform/replace_step.rb

Overview

Replaces a range of the document with a slice of content.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Step

#pos

Constructor Details

#initialize(from, to, slice = Slice.empty) ⇒ ReplaceStep

Returns a new instance of ReplaceStep.



11
12
13
14
15
16
# File 'lib/prosereflect/transform/replace_step.rb', line 11

def initialize(from, to, slice = Slice.empty)
  super()
  @from = from
  @to = to
  @slice = slice
end

Instance Attribute Details

#fromObject (readonly)

Returns the value of attribute from.



9
10
11
# File 'lib/prosereflect/transform/replace_step.rb', line 9

def from
  @from
end

#sliceObject (readonly)

Returns the value of attribute slice.



9
10
11
# File 'lib/prosereflect/transform/replace_step.rb', line 9

def slice
  @slice
end

#toObject (readonly)

Returns the value of attribute to.



9
10
11
# File 'lib/prosereflect/transform/replace_step.rb', line 9

def to
  @to
end

Class Method Details

.from_json(_schema, json) ⇒ Object



99
100
101
102
103
104
105
106
# File 'lib/prosereflect/transform/replace_step.rb', line 99

def self.from_json(_schema, json)
  from_val = json["from"]
  to_val = json["to"]
  slice_json = json["slice"] || []
  slice_content = slice_json.map { |h| Prosereflect::Node.from_h(h) }
  slice = Slice.new(Fragment.new(slice_content))
  new(from_val, to_val, slice)
end

Instance Method Details

#append_content(other) ⇒ Object



73
74
75
76
# File 'lib/prosereflect/transform/replace_step.rb', line 73

def append_content(other)
  new_content = join_slices(@slice, other.slice)
  ReplaceStep.new(@from, other.to, new_content)
end

#apply(doc) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/prosereflect/transform/replace_step.rb', line 18

def apply(doc)
  # Validate positions
  return Result.fail("Invalid positions") if @from > @to
  return Result.fail("from < 0") if @from.negative?
  return Result.fail("to > doc size") if @to > doc.node_size

  # Build the new document
  new_doc = apply_replace(doc)
  Result.ok(new_doc)
rescue StandardError => e
  Result.fail(e.message)
end

#can_append_content?(other) ⇒ Boolean

Returns:

  • (Boolean)


69
70
71
# File 'lib/prosereflect/transform/replace_step.rb', line 69

def can_append_content?(other)
  @to == other.from && !other.slice.empty?
end

#can_extend_deletion?(other) ⇒ Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/prosereflect/transform/replace_step.rb', line 53

def can_extend_deletion?(other)
  @to == other.from && @slice.empty?
end

#can_prepend_content?(other) ⇒ Boolean

Returns:

  • (Boolean)


78
79
80
# File 'lib/prosereflect/transform/replace_step.rb', line 78

def can_prepend_content?(other)
  other.to == @from && !other.slice.empty?
end

#can_prepend_deletion?(other) ⇒ Boolean

Returns:

  • (Boolean)


61
62
63
# File 'lib/prosereflect/transform/replace_step.rb', line 61

def can_prepend_deletion?(other)
  other.to == @from && other.slice.empty?
end

#extend_deletion(other) ⇒ Object



57
58
59
# File 'lib/prosereflect/transform/replace_step.rb', line 57

def extend_deletion(other)
  ReplaceStep.new(@from, other.to, Slice.empty)
end

#get_mapObject



31
32
33
34
# File 'lib/prosereflect/transform/replace_step.rb', line 31

def get_map
  delta = @slice.size - (@to - @from)
  StepMap.new([[@from, @to, @from, @from + delta]])
end

#invert(doc) ⇒ Object



36
37
38
39
40
# File 'lib/prosereflect/transform/replace_step.rb', line 36

def invert(doc)
  # Find what was removed
  removed = content_between(doc, @from, @to)
  ReplaceStep.new(@from, @from + @slice.size, removed)
end

#merge(other) ⇒ Object



42
43
44
45
46
47
48
49
50
51
# File 'lib/prosereflect/transform/replace_step.rb', line 42

def merge(other)
  return nil unless other.is_a?(ReplaceStep)

  return extend_deletion(other) if can_extend_deletion?(other)
  return prepend_deletion(other) if can_prepend_deletion?(other)
  return append_content(other) if can_append_content?(other)
  return prepend_content(other) if can_prepend_content?(other)

  nil
end

#prepend_content(other) ⇒ Object



82
83
84
85
# File 'lib/prosereflect/transform/replace_step.rb', line 82

def prepend_content(other)
  new_content = join_slices(other.slice, @slice)
  ReplaceStep.new(other.from, @to, new_content)
end

#prepend_deletion(other) ⇒ Object



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

def prepend_deletion(other)
  ReplaceStep.new(other.from, @to, Slice.empty)
end

#step_typeObject



87
88
89
# File 'lib/prosereflect/transform/replace_step.rb', line 87

def step_type
  "replace"
end

#to_json(*_args) ⇒ Object



91
92
93
94
95
96
97
# File 'lib/prosereflect/transform/replace_step.rb', line 91

def to_json(*_args)
  json = super
  json["from"] = @from
  json["to"] = @to
  json["slice"] = @slice.content.map(&:to_h)
  json
end