Class: NodeMutation::InsertAction

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

Overview

InsertAction to add code to the node.

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:, at: 'end', to: nil, and_comma: false) ⇒ InsertAction

Initialize an InsertAction.

Parameters:

  • ndoe (Node)
  • code (String)

    to be inserted

  • at (String) (defaults to: 'end')

    position to insert, beginning or end

  • to (<nil|String>) (defaults to: nil)

    name of child node

  • and_comma (Boolean) (defaults to: false)

    insert extra comma.

  • adapter (NodeMutation::Adapter)


13
14
15
16
17
18
19
# File 'lib/node_mutation/action/insert_action.rb', line 13

def initialize(node, code, adapter:, at: 'end', to: nil, and_comma: false)
  super(node, code, adapter: adapter)
  @at = at
  @to = to
  @and_comma = and_comma
  @type = :insert
end

Instance Method Details

#new_codeString

The rewritten source code.

Returns:

  • (String)

    rewritten code.



24
25
26
27
28
29
30
# File 'lib/node_mutation/action/insert_action.rb', line 24

def new_code
  if @and_comma
    @at == 'end' ? ", #{rewritten_source}" : "#{rewritten_source}, "
  else
    rewritten_source
  end
end