Module: Fixbraces

Defined in:
lib/fixbraces.rb,
lib/fixbraces/version.rb

Constant Summary collapse

VERSION =
"1.3.2"

Class Method Summary collapse

Class Method Details

.dry_process_file(file) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
# File 'lib/fixbraces.rb', line 44

def Fixbraces.dry_process_file(file)
  corrected_text = ""

  # Read in the text and pass it to the method that corrects it.
  File.open(file, "r") do |f|
    contents = f.read
    corrected_text = fixbraces contents
  end

  corrected_text ? file : nil
end

.fixbraces(text) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/fixbraces.rb', line 5

def Fixbraces.fixbraces(text)
  text_changed = false

  # Move the opening brace to the same line as the opening clause
  if text.gsub!(/\n[ \t]*\{[ \t]*$/, " {")
    text_changed = true
  end

  # If there are a pair of braces on their own line move them both up to the
  # same line as the opening line
  if text.gsub!(/\n[\s]*\{\}[\s]*$/, " {}")
    text_changed = true
  end

  text_changed ? text : nil
end

.process_file(file) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/fixbraces.rb', line 22

def Fixbraces.process_file(file)
  corrected_text = ""

  # Read in the text and pass it to the method that corrects it.
  File.open(file, "r") do |f|
    contents = f.read
    corrected_text = fixbraces contents
  end

  if corrected_text
    # Write the text to a temp file before overwriting the original file.
    temp_file = Tempfile.new "fixbraces"
    temp_file.write corrected_text
    temp_file.close

    FileUtils.cp temp_file.path, file
  end

  # Return the file path or nil if no changes were made
  corrected_text ? file : nil
end