Class: Spoom::Source::Insert

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(position, text) ⇒ Insert

: (Integer, String) -> void



47
48
49
50
51
52
# File 'lib/spoom/source/rewriter.rb', line 47

def initialize(position, text)
  super()

  @position = position
  @text = text
end

Instance Attribute Details

#positionObject (readonly)

: Integer



41
42
43
# File 'lib/spoom/source/rewriter.rb', line 41

def position
  @position
end

#textObject (readonly)

: String



44
45
46
# File 'lib/spoom/source/rewriter.rb', line 44

def text
  @text
end

Instance Method Details

#apply(bytes) ⇒ Object

: (Array) -> void

Raises:



56
57
58
59
60
61
# File 'lib/spoom/source/rewriter.rb', line 56

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

  bytes #: untyped
    .insert(position, *text.bytes)
end

#rangeObject

: -> [Integer, Integer]



65
66
67
# File 'lib/spoom/source/rewriter.rb', line 65

def range
  [position, position]
end

#to_sObject

: -> String



71
72
73
# File 'lib/spoom/source/rewriter.rb', line 71

def to_s
  "Insert `#{text}` at #{position}"
end