Class: Gauge::CodeParser Private

Inherits:
Object
  • Object
show all
Defined in:
lib/code_parser.rb

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Class Method Summary collapse

Class Method Details

.refactor(code, param_positions, new_param_values, new_step_text) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



64
65
66
67
68
69
70
71
72
# File 'lib/code_parser.rb', line 64

def self.refactor(code, param_positions, new_param_values, new_step_text)
  source_code=code.source
  file, _ = code.source_location
  refactored_code=refactor_args(source_code, param_positions, new_param_values, new_step_text)
  tmp_file = Tempfile.new File.basename(file, ".rb")
  tmp_file.write(File.open(file, "r") { |f| f.read.gsub(source_code, refactored_code)})
  tmp_file.close
  FileUtils.mv tmp_file.path, file
end

.refactor_args(code, param_positions, new_param_values, new_step_text) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/code_parser.rb', line 33

def self.refactor_args(code, param_positions, new_param_values, new_step_text)
  new_params = []
  args = step_args_from_code code
  param_positions.sort_by!(&:newPosition).each.with_index { |e,i|
    if e.oldPosition == -1
      param = Util.remove_special_chars new_param_values[e.newPosition].downcase.split.join('_')
      if param == ''
        param = i
      end
      new_params[e.newPosition] = "arg_#{param}"
    else
      new_params[e.newPosition] = args[e.oldPosition].children[0]
    end
  }
  buffer = Parser::Source::Buffer.new '(rewriter)'
  buffer.source=code
  
  ast = code_to_ast(code)
  new_params_string = "|#{new_params.join(', ')}|".gsub("||", "") # no params = empty string
  
  rewriter = Parser::Source::Rewriter.new(buffer)
    .replace(ast.children[0].location.expression, "step '#{new_step_text}'")
  
  # hack, could not find an easy way to manipulate the ast to include arguments, when none existed originally.
  # it's just easy to add arguments via string substitution.
  return include_args(rewriter.process, new_params_string) if ast.children[1].location.expression.nil?
  
  #insert new arguments
  rewriter.replace(ast.children[1].location.expression, new_params_string).process
end

.step_args_from_code(code) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



27
28
29
30
31
# File 'lib/code_parser.rb', line 27

def self.step_args_from_code(code)
  ast=code_to_ast(code)
  arg_node = ast.children[1]
  arg_node.children
end