Class: SqlBeautifier::CommentParser
- Inherits:
-
Object
- Object
- SqlBeautifier::CommentParser
- Defined in:
- lib/sql_beautifier/comment_parser.rb
Defined Under Namespace
Classes: Result
Constant Summary collapse
- SENTINEL_PREFIX =
"/*__sqlb_"- SENTINEL_SUFFIX =
"__*/"- SENTINEL_PATTERN =
%r{/\*__sqlb_(\d+)__\*/}
Class Method Summary collapse
- .call(sql, removable_types) ⇒ Object
- .restore(formatted_sql, comment_map) ⇒ Object
- .restore_inline_comment(sql, sentinel, comment_text) ⇒ Object
Instance Method Summary collapse
- #call ⇒ Object
-
#initialize(sql, removable_types) ⇒ CommentParser
constructor
A new instance of CommentParser.
Constructor Details
#initialize(sql, removable_types) ⇒ CommentParser
Returns a new instance of CommentParser.
53 54 55 56 57 58 59 60 61 62 |
# File 'lib/sql_beautifier/comment_parser.rb', line 53 def initialize(sql, removable_types) @sql = sql @removal_set = resolve_removal_set(removable_types) @output = +"" @comment_map = {} @comments = [] @sentinel_index = 0 @position = 0 @pending_line_comments = [] end |
Class Method Details
.call(sql, removable_types) ⇒ Object
11 12 13 |
# File 'lib/sql_beautifier/comment_parser.rb', line 11 def self.call(sql, removable_types) new(sql, removable_types).call end |
.restore(formatted_sql, comment_map) ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/sql_beautifier/comment_parser.rb', line 15 def self.restore(formatted_sql, comment_map) return formatted_sql if comment_map.empty? result = formatted_sql comment_map.each do |index, entry| sentinel = "#{SENTINEL_PREFIX}#{index}#{SENTINEL_SUFFIX}" result = begin case entry[:type] when :blocks result.sub(sentinel, entry[:text]) when :line result.sub(%r{#{Regexp.escape(sentinel)}[ \n]?}, "#{entry[:text]}\n") when :inline restore_inline_comment(result, sentinel, entry[:text]) else result end end end result end |
.restore_inline_comment(sql, sentinel, comment_text) ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/sql_beautifier/comment_parser.rb', line 40 def self.restore_inline_comment(sql, sentinel, comment_text) pattern = %r{ ?#{Regexp.escape(sentinel)}([^\n]*)} sql.sub(pattern) do trailing_content = Regexp.last_match(1) if trailing_content.strip.empty? " #{comment_text}" else "#{trailing_content.strip} #{comment_text}" end end end |
Instance Method Details
#call ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/sql_beautifier/comment_parser.rb', line 64 def call while @position < @sql.length character = @sql[@position] if @in_single_quoted_string consume_single_quoted_character!(character) elsif @in_double_quoted_identifier consume_double_quoted_character!(character) elsif character == Constants::SINGLE_QUOTE flush_pending_line_comments! @in_single_quoted_string = true @output << character @position += 1 elsif character == Constants::DOUBLE_QUOTE flush_pending_line_comments! @in_double_quoted_identifier = true @output << character @position += 1 elsif line_comment_start? handle_line_comment! elsif block_comment_start? flush_pending_line_comments! handle_block_comment! else flush_pending_line_comments! unless character == "\n" @output << character @position += 1 end end flush_pending_line_comments! Result.new(@output, @comment_map, @comments) end |