Class: Prosereflect::Transform::ReplaceStep
- Defined in:
- lib/prosereflect/transform/replace_step.rb
Overview
Replaces a range of the document with a slice of content.
Instance Attribute Summary collapse
-
#from ⇒ Object
readonly
Returns the value of attribute from.
-
#slice ⇒ Object
readonly
Returns the value of attribute slice.
-
#to ⇒ Object
readonly
Returns the value of attribute to.
Class Method Summary collapse
Instance Method Summary collapse
- #append_content(other) ⇒ Object
- #apply(doc) ⇒ Object
- #can_append_content?(other) ⇒ Boolean
- #can_extend_deletion?(other) ⇒ Boolean
- #can_prepend_content?(other) ⇒ Boolean
- #can_prepend_deletion?(other) ⇒ Boolean
- #extend_deletion(other) ⇒ Object
- #get_map ⇒ Object
-
#initialize(from, to, slice = Slice.empty) ⇒ ReplaceStep
constructor
A new instance of ReplaceStep.
- #invert(doc) ⇒ Object
- #merge(other) ⇒ Object
- #prepend_content(other) ⇒ Object
- #prepend_deletion(other) ⇒ Object
- #step_type ⇒ Object
- #to_json(*_args) ⇒ Object
Methods inherited from Step
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
#from ⇒ Object (readonly)
Returns the value of attribute from.
9 10 11 |
# File 'lib/prosereflect/transform/replace_step.rb', line 9 def from @from end |
#slice ⇒ Object (readonly)
Returns the value of attribute slice.
9 10 11 |
# File 'lib/prosereflect/transform/replace_step.rb', line 9 def slice @slice end |
#to ⇒ Object (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.) end |
#can_append_content?(other) ⇒ 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
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
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
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_map ⇒ Object
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_type ⇒ Object
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 |