Class: RailsLens::FileInsertionHelper
- Inherits:
-
Object
- Object
- RailsLens::FileInsertionHelper
- Defined in:
- lib/rails_lens/file_insertion_helper.rb
Overview
Helper class to handle file insertion logic, particularly for inserting content after frozen_string_literal comments while maintaining proper formatting
Constant Summary collapse
- FROZEN_STRING_LITERAL_REGEX =
/^# frozen_string_literal: true$/
Class Method Summary collapse
-
.insert_after_frozen_string_literal(content, insertion_content) ⇒ String
Insert content after frozen_string_literal comment with proper spacing.
-
.insert_at_class_definition(file_path, _class_name, annotation) ⇒ Boolean
Insert content at a specific class definition location.
-
.insert_at_line(file_path, line_number, annotation) ⇒ Boolean
Insert content at a specific line number.
-
.remove_after_frozen_string_literal(content, marker_start, marker_end) ⇒ String
Remove content that was inserted after frozen_string_literal.
Class Method Details
.insert_after_frozen_string_literal(content, insertion_content) ⇒ String
Insert content after frozen_string_literal comment with proper spacing
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/rails_lens/file_insertion_helper.rb', line 70 def insert_after_frozen_string_literal(content, insertion_content) # First check if frozen_string_literal exists unless content.match?(/^# frozen_string_literal: true/) # If no frozen_string_literal, insert at the beginning with newline return "#{insertion_content}\n#{content}" end lines = content.split("\n", -1) # Preserve empty lines insert_index = find_frozen_string_literal_index(lines) return content unless insert_index # Ensure proper spacing after frozen_string_literal insert_index = ensure_blank_line_after_frozen_literal(lines, insert_index) # Insert the new content lines.insert(insert_index, insertion_content) # Preserve original line ending result = lines.join("\n") result += "\n" if content.end_with?("\n") && !result.end_with?("\n") result end |
.insert_at_class_definition(file_path, _class_name, annotation) ⇒ Boolean
Insert content at a specific class definition location
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/rails_lens/file_insertion_helper.rb', line 16 def insert_at_class_definition(file_path, _class_name, annotation) return false unless File.exist?(file_path) original_content = File.read(file_path) content = original_content # First remove any existing annotations (schema + the marker we're inserting) content = remove_existing_annotations(content, annotation) modified_content = insert_after_frozen_string_literal(content, annotation) return false if modified_content == original_content File.write(file_path, modified_content) true rescue StandardError false end |
.insert_at_line(file_path, line_number, annotation) ⇒ Boolean
Insert content at a specific line number
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/rails_lens/file_insertion_helper.rb', line 40 def insert_at_line(file_path, line_number, annotation) original_content = File.read(file_path) content = original_content # First remove any existing annotations (schema + the marker we're inserting) content = remove_existing_annotations(content, annotation) lines = content.split("\n", -1) # Preserve trailing newlines # Find correct insertion point considering frozen_string_literal insert_index = find_insertion_point_for_line(lines, line_number) lines.insert(insert_index, annotation) # Preserve original line ending result = lines.join("\n") result += "\n" if content.end_with?("\n") && !result.end_with?("\n") return false if result == original_content File.write(file_path, result) true rescue StandardError false end |
.remove_after_frozen_string_literal(content, marker_start, marker_end) ⇒ String
Remove content that was inserted after frozen_string_literal
101 102 103 104 105 |
# File 'lib/rails_lens/file_insertion_helper.rb', line 101 def remove_after_frozen_string_literal(content, marker_start, marker_end) # Use regex to remove the marked content pattern = /^.*#{Regexp.escape(marker_start)}.*$\n(.*\n)*?^.*#{Regexp.escape(marker_end)}.*$\n/ content.gsub(pattern, '') end |