Class: Spoom::Source::Replace

Inherits:
Edit
  • Object
show all
Defined in:
lib/spoom/source/rewriter.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(from, to, text) ⇒ Replace

: (Integer, Integer, String) -> void



84
85
86
87
88
89
90
# File 'lib/spoom/source/rewriter.rb', line 84

def initialize(from, to, text)
  super()

  @from = from
  @to = to
  @text = text
end

Instance Attribute Details

#fromObject (readonly)

: Integer



78
79
80
# File 'lib/spoom/source/rewriter.rb', line 78

def from
  @from
end

#textObject (readonly)

: String



81
82
83
# File 'lib/spoom/source/rewriter.rb', line 81

def text
  @text
end

#toObject (readonly)

: Integer



78
79
80
# File 'lib/spoom/source/rewriter.rb', line 78

def to
  @to
end

Instance Method Details

#apply(bytes) ⇒ Object

: (Array) -> void

Raises:



94
95
96
97
98
# File 'lib/spoom/source/rewriter.rb', line 94

def apply(bytes)
  raise PositionError, "Position is out of bounds" if from < 0 || to < 0 || from > bytes.size || to > bytes.size || from > to

  bytes[from..to] = *text.bytes
end

#rangeObject

: -> [Integer, Integer]



102
103
104
# File 'lib/spoom/source/rewriter.rb', line 102

def range
  [from, to]
end

#to_sObject

: -> String



108
109
110
# File 'lib/spoom/source/rewriter.rb', line 108

def to_s
  "Replace #{from}-#{to} with `#{text}`"
end