Class: Synvert::Rewriter::Instance

Inherits:
Object
  • Object
show all
Defined in:
lib/synvert/rewriter/instances.rb

Instance Method Summary collapse

Constructor Details

#initialize(file_pattern) ⇒ Instance

Returns a new instance of Instance.



21
22
23
24
25
# File 'lib/synvert/rewriter/instances.rb', line 21

def initialize(file_pattern)
  @file_pattern = file_pattern
  @scopes = Rewriter::Scopes.new
  @conditions = Rewriter::Conditions.new
end

Instance Method Details

#insert(code) ⇒ Object



59
60
61
# File 'lib/synvert/rewriter/instances.rb', line 59

def insert(code)
  @action = Rewriter::InsertAction.new(code)
end

#process ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/synvert/rewriter/instances.rb', line 27

def process
  parser = Parser::CurrentRuby.new
  file_pattern = File.join(Configuration.instance.get(:path), @file_pattern)
  Dir.glob(file_pattern).each do |path|
    source = File.read(path)
    buffer = Parser::Source::Buffer.new path
    buffer.source = source

    parser.reset
    ast = parser.parse buffer

    scoped_nodes = @scopes.matching_nodes(ast)
    matching_nodes = @conditions.matching_nodes(scoped_nodes)
    matching_nodes.reverse.each do |node|
      source = @action.rewrite(source, node)
    end
    File.write path, source
  end
end

#replace_with(code) ⇒ Object



63
64
65
# File 'lib/synvert/rewriter/instances.rb', line 63

def replace_with(code)
  @action = Rewriter::ReplaceWithAction.new(code)
end

#unless_exist_node(options, &block) ⇒ Object



54
55
56
57
# File 'lib/synvert/rewriter/instances.rb', line 54

def unless_exist_node(options, &block)
  @conditions.add(Rewriter::UnlessExistCondition.new(options))
  instance_eval &block if block_given?
end

#within_node(options, &block) ⇒ Object Also known as: with_node



47
48
49
50
# File 'lib/synvert/rewriter/instances.rb', line 47

def within_node(options, &block)
  @scopes.add(options)
  instance_eval &block if block_given?
end