Class: Synvert::Core::Rewriter::Action
- Inherits:
-
Object
- Object
- Synvert::Core::Rewriter::Action
- Defined in:
- lib/synvert/core/rewriter/action.rb
Overview
Action defines rewriter action, add, replace or remove code.
Direct Known Subclasses
AppendAction, DeleteAction, InsertAction, InsertAfterAction, RemoveAction, ReplaceAction, ReplaceErbStmtWithExprAction, ReplaceWithAction
Constant Summary collapse
- DEFAULT_OPTIONS =
{ autoindent: true }.freeze
- DEFAULT_INDENT =
2
Instance Method Summary collapse
-
#initialize(instance, code, options = {}) ⇒ Action
constructor
Initialize an action.
-
#line ⇒ Integer
Line number of current node.
-
#rewritten_code ⇒ String
The rewritten source code with proper indent.
-
#rewritten_source ⇒ String
The rewritten source code.
Constructor Details
#initialize(instance, code, options = {}) ⇒ Action
Initialize an action.
14 15 16 17 18 19 |
# File 'lib/synvert/core/rewriter/action.rb', line 14 def initialize(instance, code, = {}) @instance = instance @code = code = DEFAULT_OPTIONS.merge() @node = @instance.current_node end |
Instance Method Details
#line ⇒ Integer
Line number of current node.
24 25 26 |
# File 'lib/synvert/core/rewriter/action.rb', line 24 def line @node.loc.expression.line end |
#rewritten_code ⇒ String
The rewritten source code with proper indent.
31 32 33 34 35 36 37 |
# File 'lib/synvert/core/rewriter/action.rb', line 31 def rewritten_code if rewritten_source.split("\n").length > 1 "\n\n" + rewritten_source.split("\n").map { |line| indent(@node) + line }.join("\n") else "\n" + indent(@node) + rewritten_source end end |
#rewritten_source ⇒ String
The rewritten source code.
42 43 44 |
# File 'lib/synvert/core/rewriter/action.rb', line 42 def rewritten_source @rewritten_source ||= @node.rewritten_source(@code) end |