Class: NodeMutation::PrependAction

Inherits:
Action
  • Object
show all
Defined in:
lib/node_mutation/action/prepend_action.rb

Overview

PrependAction to prepend code to the top of node body.

Instance Attribute Summary

Attributes inherited from Action

#actions, #end, #start, #type

Instance Method Summary collapse

Methods inherited from Action

#process, #to_struct

Constructor Details

#initialize(node, code, adapter:) ⇒ PrependAction

Initialize an PrependAction.

Parameters:



10
11
12
13
# File 'lib/node_mutation/action/prepend_action.rb', line 10

def initialize(node, code, adapter:)
  super(node, code, adapter: adapter)
  @type = :insert
end

Instance Method Details

#new_codeString

The rewritten source code with proper indent.

Returns:

  • (String)

    rewritten code.



18
19
20
21
22
23
24
# File 'lib/node_mutation/action/prepend_action.rb', line 18

def new_code
  if rewritten_source.split("\n").length > 1
    "\n" + rewritten_source.split("\n").map { |line| indent(@node) + line }.join("\n") + "\n"
  else
    indent(@node) + rewritten_source + "\n"
  end
end