annotato 0.1.5 → 0.1.7

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: e80ca78ee10d8d0ade55aea343a5cf37c9831190611eca4adbc913e82183dbb0
4
- data.tar.gz: ede81923b6cb487f342b2b23db71aa9fdf56cdd82daf0c7af3b6e0a29de185f9
3
+ metadata.gz: 40145b6423788c1e730c30b88fb7d0858f4e5103b5cf6848b888b58dbed9883b
4
+ data.tar.gz: 84c1713c14000ad436410c8c2ae408d312a439f82158a6fc1a7cccfdf378e537
5
5
  SHA512:
6
- metadata.gz: 9a7b2c8a7008c0703d0eae63d2c586d56b32adac93e2bbe912df16b496f26e5d590013c6a842a00bdd792801879efcb3846082fcb8089313c366c0365090f15c
7
- data.tar.gz: 83da6bec671419c9f8ead2fadf173e59caaf7593d63d5d53eab3cbc125dc48ada3554e08836a7061d639ae79a73cd38415443291c232eee92f73c000f1de8c8d
6
+ metadata.gz: 2b3103a08cccb5d2658e969eafddf36977b65e88f94cc72d35a1e485f841f67216071ee9bd37955a81d9c29844da11e4a3777d47dbf645e07ae861d4553f8d57
7
+ data.tar.gz: f38edaf28cc9ca220f2825219ffc619d8638301719bebecbc60aae6d7bdadf01811fd9a84ce4e5d05b2d0432f908608d991c2e6be8e40fcaaef8f34bbebd8440
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- annotato (0.1.5)
4
+ annotato (0.1.7)
5
5
  rails (>= 6.0)
6
6
 
7
7
  GEM
@@ -1,65 +1,77 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "json"
3
+ require 'json'
4
4
 
5
5
  module Annotato
6
6
  class ColumnFormatter
7
7
  def self.format(model, connection)
8
8
  table_name = model.table_name
9
9
  primary_key = model.primary_key
10
- unique_indexes = connection.indexes(table_name).select(&:unique)
11
10
  enums = model.defined_enums
11
+ unique_indexes = connection.indexes(table_name).select(&:unique)
12
+ columns = connection.columns(table_name)
12
13
 
13
- columns_raw = connection.columns(table_name).map do |col|
14
+ raw_data = columns.map do |col|
14
15
  name = col.name
15
16
  type = col.sql_type
16
- options = []
17
+ default = col.default
18
+ opts = []
17
19
 
18
- default = format_default(col.default)
19
- options << default if default
20
- options << "not null" unless col.null
21
- options << "primary key" if name == primary_key
22
- options << "is an Array" if type.end_with?("[]")
23
- options << "unique" if unique_indexes.any? { |idx| idx.columns == [name] }
24
- options << "enum" if enums.key?(name)
20
+ opts << "default(#{default.inspect})" unless default.nil?
21
+ opts << "not null" unless col.null
22
+ opts << "primary key" if name == primary_key
23
+ opts << "is an Array" if type.end_with?("[]")
24
+ opts << "unique" if unique_indexes.any? { |idx| idx.columns == [name] }
25
+ opts << "enum" if enums.key?(name)
25
26
 
26
- [name, type, options.join(", ")]
27
+ [name, type, default, opts]
27
28
  end
28
29
 
29
- name_width = columns_raw.map { |name, _, _| name.length }.max
30
- type_width = columns_raw.map { |_, type, _| type.length }.max
30
+ name_width = raw_data.map { |name, *_| name.length }.max
31
+ type_width = raw_data.map { |_, type, *_| type.length }.max
31
32
 
32
- columns_raw.map do |name, type, opts|
33
- line = "# %-#{name_width}s :%-#{type_width}s" % [name, type]
34
- line += " #{opts}" unless opts.empty?
35
- line.rstrip
36
- end
37
- end
33
+ raw_data.flat_map do |name, type, default, opts|
34
+ base_line = "# %-#{name_width}s :%-#{type_width}s" % [name, type]
35
+ indent = ' ' * (base_line.length + 1)
38
36
 
39
- def self.format_default(value)
40
- return nil if value.nil?
37
+ if multiline_default?(default)
38
+ formatted_defaults = format_multiline_default(default)
39
+ remaining_opts = opts.reject { |o| o.start_with?("default(") }
41
40
 
42
- if json_like?(value)
43
- stripped = value.strip
44
- begin
45
- parsed = JSON.parse(stripped)
46
- if parsed.is_a?(Array) && parsed.all? { |e| e.is_a?(String) }
47
- lines = parsed.map { |e| %Q( "#{e}",) }
48
- lines[-1] = lines[-1].chomp(',') if lines.last # <-- тут зміна
49
- ["default([", *lines, "])"].join("\n# ")
50
- else
51
- "default(#{stripped.gsub(/\s+/, ' ')})"
41
+ lines = []
42
+ lines << "#{base_line} default(["
43
+ formatted_defaults.each do |v|
44
+ lines << "#{'#' + indent}#{v}"
52
45
  end
53
- rescue JSON::ParserError
54
- "default(#{stripped.gsub(/\s+/, ' ')})"
46
+
47
+ closing = "#{'#' + ' ' * (indent.length - 1)}]),"
48
+ closing += " #{remaining_opts.join(', ')}" unless remaining_opts.empty?
49
+ lines << closing.rstrip
50
+ lines
51
+ else
52
+ line = base_line
53
+ line += " #{opts.join(', ')}" unless opts.empty?
54
+ line.rstrip
55
55
  end
56
- else
57
- "default(#{value.inspect})"
58
56
  end
59
57
  end
60
58
 
61
- def self.json_like?(value)
62
- value.is_a?(String) && value.strip.match?(/\A[\[{].*[\]}]\z/m)
59
+ def self.multiline_default?(value)
60
+ value.is_a?(String) && value.strip.start_with?("[") && value.strip.end_with?("]") && value.include?(",")
61
+ end
62
+
63
+ def self.format_multiline_default(value)
64
+ parsed = JSON.parse(value)
65
+ return [value] unless parsed.is_a?(Array)
66
+
67
+ items = parsed.is_a?(Array) && parsed[0].is_a?(Array) ? parsed[0] : parsed
68
+
69
+ items.map.with_index do |item, idx|
70
+ comma = idx == items.size - 1 ? "" : ","
71
+ "#{item.to_json}#{comma}"
72
+ end
73
+ rescue JSON::ParserError
74
+ [value]
63
75
  end
64
76
  end
65
77
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Annotato
4
- VERSION = "0.1.5"
4
+ VERSION = "0.1.7"
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.5
4
+ version: 0.1.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Serhii Bodnaruk