Class: Fast::Rewriter
- Inherits:
-
Parser::TreeRewriter
- Object
- Parser::TreeRewriter
- Fast::Rewriter
- Defined in:
- lib/fast.rb
Overview
Note:
the standalone class needs to combines #replace_on to properly generate the ‘on_<node-type>` methods depending on the expression being used.
Rewriter encapsulates #match_index to allow ExperimentFile#partial_replace in a ExperimentFile.
Instance Attribute Summary collapse
-
#buffer ⇒ Object
Returns the value of attribute buffer.
-
#match_index ⇒ Integer
readonly
With occurrence index.
-
#replacement ⇒ Object
Returns the value of attribute replacement.
-
#search ⇒ Object
Returns the value of attribute search.
Instance Method Summary collapse
-
#execute_replacement(node, captures) ⇒ Object
Execute #replacement block.
-
#initialize(*args) ⇒ Rewriter
constructor
A new instance of Rewriter.
- #match?(node) ⇒ Boolean
-
#replace_on(*types) ⇒ Object
Generate methods for all affected types.
Constructor Details
#initialize(*args) ⇒ Rewriter
Returns a new instance of Rewriter.
289 290 291 292 |
# File 'lib/fast.rb', line 289 def initialize(*args) super @match_index = 0 end |
Instance Attribute Details
#buffer ⇒ Object
Returns the value of attribute buffer.
288 289 290 |
# File 'lib/fast.rb', line 288 def buffer @buffer end |
#match_index ⇒ Integer (readonly)
Returns with occurrence index.
287 288 289 |
# File 'lib/fast.rb', line 287 def match_index @match_index end |
#replacement ⇒ Object
Returns the value of attribute replacement.
288 289 290 |
# File 'lib/fast.rb', line 288 def replacement @replacement end |
#search ⇒ Object
Returns the value of attribute search.
288 289 290 |
# File 'lib/fast.rb', line 288 def search @search end |
Instance Method Details
#execute_replacement(node, captures) ⇒ Object
Execute #replacement block
315 316 317 318 319 320 321 |
# File 'lib/fast.rb', line 315 def execute_replacement(node, captures) if replacement.parameters.length == 1 instance_exec node, &replacement else instance_exec node, captures, &replacement end end |
#match?(node) ⇒ Boolean
294 295 296 |
# File 'lib/fast.rb', line 294 def match?(node) Fast.match?(search, node) end |
#replace_on(*types) ⇒ Object
Generate methods for all affected types.
300 301 302 303 304 305 306 307 308 309 310 |
# File 'lib/fast.rb', line 300 def replace_on(*types) types.map do |type| self.class.send :define_method, "on_#{type}" do |node| if captures = match?(node) # rubocop:disable Lint/AssignmentInCondition @match_index += 1 execute_replacement(node, captures) end super(node) end end end |