Class: Synvert::Core::Rewriter::ReplaceErbStmtWithExprAction
- Defined in:
- lib/synvert/core/rewriter/action/replace_erb_stmt_with_expr_action.rb
Overview
ReplaceErbStmtWithExprAction to replace erb stmt code to expr,
e.g. <% form_for ... %> => <%= form_for ... %>.
Instance Method Summary collapse
-
#begin_pos ⇒ Integer
Begin position of code to replace.
-
#end_pos ⇒ Integer
End position of code to replace.
-
#initialize(instance, code = nil) ⇒ ReplaceErbStmtWithExprAction
constructor
A new instance of ReplaceErbStmtWithExprAction.
-
#rewritten_code ⇒ String
The rewritten erb expr code.
Methods inherited from Action
#<=>, #line, #rewritten_source
Constructor Details
#initialize(instance, code = nil) ⇒ ReplaceErbStmtWithExprAction
Returns a new instance of ReplaceErbStmtWithExprAction.
7 8 9 |
# File 'lib/synvert/core/rewriter/action/replace_erb_stmt_with_expr_action.rb', line 7 def initialize(instance, code=nil) super end |
Instance Method Details
#begin_pos ⇒ Integer
Begin position of code to replace.
14 15 16 17 18 19 |
# File 'lib/synvert/core/rewriter/action/replace_erb_stmt_with_expr_action.rb', line 14 def begin_pos node_begin_pos = @node.loc.expression.begin_pos while @node.loc.expression.source_buffer.source[node_begin_pos -= 1] == ' ' end node_begin_pos - Engine::ERUBY_STMT_SPLITTER.length + 1 end |
#end_pos ⇒ Integer
End position of code to replace.
24 25 26 27 28 29 30 |
# File 'lib/synvert/core/rewriter/action/replace_erb_stmt_with_expr_action.rb', line 24 def end_pos node_begin_pos = @node.loc.expression.begin_pos node_begin_pos += @node.loc.expression.source.index "do" while @node.loc.expression.source_buffer.source[node_begin_pos += 1] != '@' end node_begin_pos end |
#rewritten_code ⇒ String
The rewritten erb expr code.
35 36 37 38 |
# File 'lib/synvert/core/rewriter/action/replace_erb_stmt_with_expr_action.rb', line 35 def rewritten_code @node.loc.expression.source_buffer.source[begin_pos...end_pos].sub(Engine::ERUBY_STMT_SPLITTER, "@output_buffer.append= ") .sub(Engine::ERUBY_STMT_SPLITTER, Engine::ERUBY_EXPR_SPLITTER) end |