Class: SmartDiff
- Inherits:
-
Object
- Object
- SmartDiff
- Defined in:
- lib/smart_diff.rb
Instance Attribute Summary collapse
-
#code_one ⇒ Object
Returns the value of attribute code_one.
-
#code_two ⇒ Object
Returns the value of attribute code_two.
-
#file_one ⇒ Object
Returns the value of attribute file_one.
-
#file_two ⇒ Object
Returns the value of attribute file_two.
-
#node_diff ⇒ Object
Returns the value of attribute node_diff.
Instance Method Summary collapse
-
#diff ⇒ java.util.ArrayList<DeepDiff>
Create a diff of the two AST objects.
-
#initialize(file_one, file_two) ⇒ SmartDiff
constructor
Create a SmartDiff object which will create diff of two source files based on AST generated by JRubyParser.
-
#parse(code_to_parse, file_name) ⇒ org.jrubyparser.Node
Parse the source into an abstract syntax tree.
-
#read(file_name) ⇒ String
Read the source code into a string.
Constructor Details
#initialize(file_one, file_two) ⇒ SmartDiff
Create a SmartDiff object which will create diff of two source files based on AST generated by JRubyParser.
20 21 22 23 24 |
# File 'lib/smart_diff.rb', line 20 def initialize(file_one, file_two) @file_one = file_one @file_two = file_two @node_diff = diff() end |
Instance Attribute Details
#code_one ⇒ Object
Returns the value of attribute code_one.
9 10 11 |
# File 'lib/smart_diff.rb', line 9 def code_one @code_one end |
#code_two ⇒ Object
Returns the value of attribute code_two.
9 10 11 |
# File 'lib/smart_diff.rb', line 9 def code_two @code_two end |
#file_one ⇒ Object
Returns the value of attribute file_one.
9 10 11 |
# File 'lib/smart_diff.rb', line 9 def file_one @file_one end |
#file_two ⇒ Object
Returns the value of attribute file_two.
9 10 11 |
# File 'lib/smart_diff.rb', line 9 def file_two @file_two end |
#node_diff ⇒ Object
Returns the value of attribute node_diff.
9 10 11 |
# File 'lib/smart_diff.rb', line 9 def node_diff @node_diff end |
Instance Method Details
#diff ⇒ java.util.ArrayList<DeepDiff>
Create a diff of the two AST objects.
67 68 69 70 71 72 73 74 75 76 |
# File 'lib/smart_diff.rb', line 67 def diff() @code_one = read(@file_one) @code_two = read(@file_two) if @code_one && @code_two nodeA = parse(@code_one, @file_one) nodeB = parse(@code_two, @file_two) nd = org.jrubyparser.util.diff.NodeDiff.new(nodeA, @code_one, nodeB, @code_two) nd.deep_diff end end |
#parse(code_to_parse, file_name) ⇒ org.jrubyparser.Node
Parse the source into an abstract syntax tree.
50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/smart_diff.rb', line 50 def parse(code_to_parse, file_name) begin JRubyParser.parse(code_to_parse, { :filename => file_name }) rescue SyntaxException => e = e. << "\nThis file, #{file_name} ,is not valid ruby." << "\nCheck syntax and try again." se = SyntaxException.new(e.pid, e.position, ) raise se end end |
#read(file_name) ⇒ String
Read the source code into a string.
33 34 35 36 37 38 39 40 |
# File 'lib/smart_diff.rb', line 33 def read(file_name) path = Pathname.new(file_name). if path.exist? File.read path else raise "#{path} not found. Check the path." end end |