class SmartDiff

Attributes

code_one[RW]
code_two[RW]
file_one[RW]
file_two[RW]
node_diff[RW]

Public Class Methods

new(file_one, file_two) click to toggle source

Create a SmartDiff object which will create diff of two source files based on AST generated by JRubyParser.

@param file_one [String] The path to the first source file. @param file_two [String] The path to the second source file.

@return [SmartDiff] A Ruby object which holds a reference to a diff.

# 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

Public Instance Methods

diff() click to toggle source

Create a diff of the two AST objects.

@return [java.util.ArrayList<DeepDiff>] The differences.

# 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) click to toggle source

Parse the source into an abstract syntax tree.

@param code_to_parse [String] Ruby source code. @param file_name [String] The path to the file containing code_to_parse

@return [org.jrubyparser.Node] A Node object representing the code as AST.

# 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
    message = e.message
    message << "\nThis file, #{file_name} ,is not valid ruby."
    message << "\nCheck syntax and try again."
    se = SyntaxException.new(e.pid, e.position, message)
    raise se
  end
end
read(file_name) click to toggle source

Read the source code into a string.

@param file_name [String] the path to a file containing ruby source

@return [String] The code read in from the file path passed in.

# File lib/smart_diff.rb, line 33
def read(file_name)
  path = Pathname.new(file_name).expand_path
  if path.exist?
    File.read path
  else
    raise "#{path} not found. Check the path."
  end
end