Class: Synvert::Rewriter::Instance
- Inherits:
-
Object
- Object
- Synvert::Rewriter::Instance
- Defined in:
- lib/synvert/rewriter/instance.rb,
lib/synvert/snippets/rails/upgrade_3_2_to_4_0.rb
Instance Attribute Summary collapse
-
#current_file ⇒ Object
Returns the value of attribute current_file.
-
#current_node ⇒ Object
Returns the value of attribute current_node.
-
#current_source ⇒ Object
Returns the value of attribute current_source.
Instance Method Summary collapse
- #append(code) ⇒ Object
- #assign(name, key, value) ⇒ Object
- #dynamic_finder_to_hash(node, prefix) ⇒ Object
- #fetch(name, key) ⇒ Object
- #if_only_exist_node(options, &block) ⇒ Object
-
#initialize(rewriter, file_pattern, &block) ⇒ Instance
constructor
A new instance of Instance.
- #insert(code) ⇒ Object
- #insert_after(node) ⇒ Object
- #node ⇒ Object
- #process ⇒ Object
- #remove ⇒ Object
- #replace_with(code) ⇒ Object
- #unless_exist_node(options, &block) ⇒ Object
- #within_node(options, &block) ⇒ Object (also: #with_node)
Constructor Details
#initialize(rewriter, file_pattern, &block) ⇒ Instance
Returns a new instance of Instance.
7 8 9 10 11 12 |
# File 'lib/synvert/rewriter/instance.rb', line 7 def initialize(rewriter, file_pattern, &block) @rewriter = rewriter @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
59 60 61 |
# File 'lib/synvert/rewriter/instance.rb', line 59 def append(code) @actions << Rewriter::AppendAction.new(self, code) end |
#assign(name, key, value) ⇒ Object
79 80 81 |
# File 'lib/synvert/rewriter/instance.rb', line 79 def assign(name, key, value) @rewriter.set name, key, value end |
#dynamic_finder_to_hash(node, prefix) ⇒ Object
4 5 6 7 8 9 |
# File 'lib/synvert/snippets/rails/upgrade_3_2_to_4_0.rb', line 4 def dynamic_finder_to_hash(node, prefix) fields = node..to_s[prefix.length..-1].split("_and_") fields.length.times.map { |i| fields[i] + ": " + node.arguments[i].source(self) }.join(", ") end |
#fetch(name, key) ⇒ Object
83 84 85 |
# File 'lib/synvert/rewriter/instance.rb', line 83 def fetch(name, key) @rewriter.get name, key end |
#if_only_exist_node(options, &block) ⇒ Object
55 56 57 |
# File 'lib/synvert/rewriter/instance.rb', line 55 def if_only_exist_node(, &block) Rewriter::IfOnlyExistCondition.new(self, , &block).process end |
#insert(code) ⇒ Object
63 64 65 |
# File 'lib/synvert/rewriter/instance.rb', line 63 def insert(code) @actions << Rewriter::InsertAction.new(self, code) end |
#insert_after(node) ⇒ Object
67 68 69 |
# File 'lib/synvert/rewriter/instance.rb', line 67 def insert_after(node) @actions << Rewriter::InsertAfterAction.new(self, node) end |
#node ⇒ Object
41 42 43 |
# File 'lib/synvert/rewriter/instance.rb', line 41 def node @current_node end |
#process ⇒ Object
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 39 |
# File 'lib/synvert/rewriter/instance.rb', line 14 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
75 76 77 |
# File 'lib/synvert/rewriter/instance.rb', line 75 def remove @actions << Rewriter::RemoveAction.new(self) end |
#replace_with(code) ⇒ Object
71 72 73 |
# File 'lib/synvert/rewriter/instance.rb', line 71 def replace_with(code) @actions << Rewriter::ReplaceWithAction.new(self, code) end |
#unless_exist_node(options, &block) ⇒ Object
51 52 53 |
# File 'lib/synvert/rewriter/instance.rb', line 51 def unless_exist_node(, &block) Rewriter::UnlessExistCondition.new(self, , &block).process end |