Class: Spoom::Source::Rewriter
- Inherits:
-
Object
- Object
- Spoom::Source::Rewriter
- Defined in:
- lib/spoom/source/rewriter.rb
Instance Method Summary collapse
- #<<(other) ⇒ Object
-
#initialize ⇒ Rewriter
constructor
A new instance of Rewriter.
- #rewrite!(bytes) ⇒ Object
Constructor Details
#initialize ⇒ Rewriter
Returns a new instance of Rewriter.
150 151 152 |
# File 'lib/spoom/source/rewriter.rb', line 150 def initialize @edits = [] #: Array[Edit] end |
Instance Method Details
#<<(other) ⇒ Object
155 156 157 |
# File 'lib/spoom/source/rewriter.rb', line 155 def <<(other) @edits << other end |
#rewrite!(bytes) ⇒ Object
160 161 162 163 164 165 166 167 168 169 |
# File 'lib/spoom/source/rewriter.rb', line 160 def rewrite!(bytes) # To avoid remapping positions after each edit, # we sort the changes by position and apply them in reverse order. # When ranges are equal, preserve the original order @edits.each_with_index.sort_by do |(edit, idx)| [edit.range, idx] end.reverse_each do |(edit, _)| edit.apply(bytes) end end |