Class: Synvert::Rewriter::Instance

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file_pattern, &block) ⇒ Instance

Returns a new instance of Instance.



7
8
9
10
11
# File 'lib/synvert/rewriter/instance.rb', line 7

def initialize(file_pattern, &block)
  @actions = []
  @file_pattern = file_pattern
  @block = block
end

Instance Attribute Details

#current_file ⇒ Object

Returns the value of attribute current_file.



5
6
7
# File 'lib/synvert/rewriter/instance.rb', line 5

def current_file
  @current_file
end

#current_node ⇒ Object

Returns the value of attribute current_node.



5
6
7
# File 'lib/synvert/rewriter/instance.rb', line 5

def current_node
  @current_node
end

#current_source ⇒ Object

Returns the value of attribute current_source.



5
6
7
# File 'lib/synvert/rewriter/instance.rb', line 5

def current_source
  @current_source
end

Instance Method Details

#append(code) ⇒ Object



62
63
64
# File 'lib/synvert/rewriter/instance.rb', line 62

def append(code)
  @actions << Rewriter::AppendAction.new(self, code)
end

#if_exist_node(rules, &block) ⇒ Object



50
51
52
# File 'lib/synvert/rewriter/instance.rb', line 50

def if_exist_node(rules, &block)
  Rewriter::IfExistCondition.new(self, rules, &block).process
end

#if_only_exist_node(rules, &block) ⇒ Object



58
59
60
# File 'lib/synvert/rewriter/instance.rb', line 58

def if_only_exist_node(rules, &block)
  Rewriter::IfOnlyExistCondition.new(self, rules, &block).process
end

#insert(code) ⇒ Object



66
67
68
# File 'lib/synvert/rewriter/instance.rb', line 66

def insert(code)
  @actions << Rewriter::InsertAction.new(self, code)
end

#insert_after(node) ⇒ Object



70
71
72
# File 'lib/synvert/rewriter/instance.rb', line 70

def insert_after(node)
  @actions << Rewriter::InsertAfterAction.new(self, node)
end

#node ⇒ Object



40
41
42
# File 'lib/synvert/rewriter/instance.rb', line 40

def node
  @current_node
end

#process ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/synvert/rewriter/instance.rb', line 13

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

    parser.reset
    ast = parser.parse buffer

    @current_file = file_path
    @current_source = source
    @current_node = ast
    instance_eval &@block
    @current_node = ast

    @actions.sort.reverse.each do |action|
      source[action.begin_pos...action.end_pos] = action.rewritten_code
      source = remove_code_or_whole_line(source, action.line)
    end
    @actions = []

    File.write file_path, source
  end
end

#remove ⇒ Object



78
79
80
# File 'lib/synvert/rewriter/instance.rb', line 78

def remove
  @actions << Rewriter::RemoveAction.new(self)
end

#replace_with(code) ⇒ Object



74
75
76
# File 'lib/synvert/rewriter/instance.rb', line 74

def replace_with(code)
  @actions << Rewriter::ReplaceWithAction.new(self, code)
end

#unless_exist_node(rules, &block) ⇒ Object



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

def unless_exist_node(rules, &block)
  Rewriter::UnlessExistCondition.new(self, rules, &block).process
end

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



44
45
46
# File 'lib/synvert/rewriter/instance.rb', line 44

def within_node(rules, &block)
  Rewriter::Scope.new(self, rules, &block).process
end