annotato 0.1.14 → 0.1.15

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 486945ef7d921533d808722e4b443ad00754ff0f509f9bc79af62f4ebb956433
4
- data.tar.gz: 82a7f612b51dbe7733af41dc642339529536f03094472115c229ef68003d2df2
3
+ metadata.gz: c9018e17dc7b82a75e12b7e43cf5845abf9d397512cfc987aaf40dc4c70af045
4
+ data.tar.gz: ba4b35b5f17dbeebe540e32bc5bbfa4b124667915072bc73d6690429b5e3777f
5
5
  SHA512:
6
- metadata.gz: f012e06a8de2ca29b0925bcc16e5d74b2792cbb8188190c2dac858d8724fdbd0383a8643c606cd2993aa9877a10b555dd16c73182fb82bb18845db1f97af4f85
7
- data.tar.gz: b24cd2ef2b6be077b37247619862e3217566794c98169b9e5f669947bcc5619fc7dd3a7f6e1f591349faeaf163ce30ef9793db8ad40c045b3a2c11f7a343994d
6
+ metadata.gz: 2d573afafb8041a325f75eca8cf1ee9396520e380d28cd5a293b375e17b60f9bb872525c99a7cf0df7a54da402d5a89cadb36ec5cefbdd6906340f8bcae49a40
7
+ data.tar.gz: 0753e255496f9f03ec1d91ab54317ff435d2133d9e0dee7fa285c30bdef6a8692abe4a79a7d2412784436ec2696bed060feb30c5c0efde384ffcc75ce9c08273
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- annotato (0.1.14)
4
+ annotato (0.1.15)
5
5
  rails (>= 6.0)
6
6
 
7
7
  GEM
@@ -2,13 +2,84 @@
2
2
 
3
3
  module Annotato
4
4
  class IndexFormatter
5
+ MAX_LINE = 100
6
+
5
7
  def self.format(conn, table_name)
6
8
  conn.indexes(table_name).map do |idx|
7
9
  cols_list = Array(idx.columns).join(',')
8
10
  unique_clause = idx.unique ? " unique" : ""
9
- where_clause = idx.where ? "\n# where (#{idx.where})" : ""
11
+
12
+ where_clause = ""
13
+ if idx.where
14
+ where_clause = "\n" + wrap_where(idx.where, max_len: MAX_LINE)
15
+ end
16
+
10
17
  "# #{idx.name} (#{cols_list})#{unique_clause}#{where_clause}"
11
18
  end
12
19
  end
20
+
21
+ # Produces one or more lines, each already prefixed with "# "
22
+ # First line: "# where (..."
23
+ # Next lines: "# ..." (aligned under the "(")
24
+ def self.wrap_where(where_sql, max_len:)
25
+ first_prefix = "# where ("
26
+ cont_prefix = "# " # aligns under the "(" after "where "
27
+
28
+ text = where_sql.to_s
29
+ lines = []
30
+
31
+ # If it already fits, keep it one-liner
32
+ if (first_prefix.length + text.length + 1) <= max_len
33
+ return "#{first_prefix}#{text})"
34
+ end
35
+
36
+ remaining = text.dup
37
+ current_prefix = first_prefix
38
+
39
+ while remaining.length > 0
40
+ available = max_len - current_prefix.length - 1 # -1 for closing ")" on last line (or just safety)
41
+ available = 20 if available < 20
42
+
43
+ if remaining.length <= available
44
+ lines << "#{current_prefix}#{remaining}"
45
+ remaining = ""
46
+ break
47
+ end
48
+
49
+ # Prefer breaking on logical operators within the window
50
+ window = remaining[0, available]
51
+ cut =
52
+ window.rindex(" AND ") ||
53
+ window.rindex(" OR ") ||
54
+ window.rindex(", ") ||
55
+ window.rindex(") ") ||
56
+ window.rindex(" ") # last resort: whitespace
57
+
58
+ # If we found a breakpoint, include it in the line (so operators stay visible)
59
+ if cut
60
+ # If we’re cutting on AND/OR/comma, keep that token at the end of the line when possible
61
+ if window[cut, 5] == " AND " || window[cut, 4] == " OR "
62
+ cut += (window[cut, 5] == " AND " ? 5 : 4)
63
+ elsif window[cut, 2] == ", "
64
+ cut += 2
65
+ elsif window[cut, 2] == ") "
66
+ cut += 2
67
+ else
68
+ cut += 1
69
+ end
70
+ else
71
+ cut = available
72
+ end
73
+
74
+ lines << "#{current_prefix}#{remaining[0, cut].rstrip}"
75
+ remaining = remaining[cut..].to_s.lstrip
76
+ current_prefix = cont_prefix
77
+ end
78
+
79
+ # Close the paren on the last line
80
+ lines[-1] = "#{lines[-1]})"
81
+
82
+ lines.join("\n")
83
+ end
13
84
  end
14
85
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Annotato
4
- VERSION = "0.1.14"
4
+ VERSION = "0.1.15"
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: annotato
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.14
4
+ version: 0.1.15
5
5
  platform: ruby
6
6
  authors:
7
7
  - Serhii Bodnaruk