Skip to content

Commit fd6db90

Browse files
Refactor: Extract logic into smaller methods
1 parent 6b4ae1b commit fd6db90

File tree

1 file changed

+20
-5
lines changed

1 file changed

+20
-5
lines changed

lib/suspenders/cleanup/organize_gemfile.rb

+20-5
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,22 @@ def initialize(gemfile)
2020
end
2121

2222
def perform
23-
# Remove line breaks to ensure consistent spacing
23+
remove_line_breaks
24+
sort_gems_and_groups
25+
add_gem_groups_to_gemfile
26+
add_line_breaks
27+
cleanup
28+
29+
File.open(gemfile, "w+") { _1.write new_lines.join }
30+
end
31+
32+
private
33+
34+
def remove_line_breaks
2435
current_lines.delete("\n")
36+
end
2537

38+
def sort_gems_and_groups
2639
current_lines.each do |line|
2740
if line.starts_with?(/group/)
2841
@current_group = line
@@ -42,8 +55,9 @@ def perform
4255
@current_group = nil
4356
end
4457
end
58+
end
4559

46-
# Add consolidated gem groups to new Gemfile
60+
def add_gem_groups_to_gemfile
4761
gem_groups.keys.each do |group|
4862
gems = gem_groups[group]
4963

@@ -59,8 +73,9 @@ def perform
5973
end
6074
end
6175
end
76+
end
6277

63-
# Conditionally add line breaks
78+
def add_line_breaks
6479
new_lines.each_with_index do |line, index|
6580
previous_line = new_lines[index - 1] if index > 0
6681
next_line = new_lines[index + 1]
@@ -106,13 +121,13 @@ def perform
106121

107122
new_lines.insert(marker_offset, "\n")
108123
end
124+
end
109125

126+
def cleanup
110127
# Remove last line
111128
if /\n/.match?(new_lines.last)
112129
new_lines.pop
113130
end
114-
115-
File.open(gemfile, "w+") { _1.write new_lines.join }
116131
end
117132
end
118133
end

0 commit comments

Comments
 (0)