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, InsertAction, InsertAfterAction, RemoveAction, ReplaceErbStmtWithExprAction, ReplaceWithAction
Instance Method Summary collapse
-
#<=>(action) ⇒ Integer
Compare actions by begin position.
-
#initialize(instance, code) ⇒ 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) ⇒ Action
Initialize an action.
10 11 12 13 14 |
# File 'lib/synvert/core/rewriter/action.rb', line 10 def initialize(instance, code) @instance = instance @code = code @node = @instance.current_node end |
Instance Method Details
#<=>(action) ⇒ Integer
Compare actions by begin position.
47 48 49 |
# File 'lib/synvert/core/rewriter/action.rb', line 47 def <=>(action) self.begin_pos <=> action.begin_pos end |
#line ⇒ Integer
Line number of current node.
19 20 21 |
# File 'lib/synvert/core/rewriter/action.rb', line 19 def line @node.loc.expression.line end |
#rewritten_code ⇒ String
The rewritten source code with proper indent.
26 27 28 29 30 31 32 33 34 |
# File 'lib/synvert/core/rewriter/action.rb', line 26 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.
39 40 41 |
# File 'lib/synvert/core/rewriter/action.rb', line 39 def rewritten_source @rewritten_source ||= @node.rewritten_source(@code) end |