annotato 0.1.5 → 0.1.6

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: abbd50b99b491161e60be9f141a6e5315d56602dbf4b361f2f0a9d0fe15ad49b
4
+ data.tar.gz: b4f5fb6095559edb538bc3f4fe9f8b6ffd723c3e34a75c9259f2b460474aeed8
5
5
  SHA512:
6
- metadata.gz: 9a7b2c8a7008c0703d0eae63d2c586d56b32adac93e2bbe912df16b496f26e5d590013c6a842a00bdd792801879efcb3846082fcb8089313c366c0365090f15c
7
- data.tar.gz: 83da6bec671419c9f8ead2fadf173e59caaf7593d63d5d53eab3cbc125dc48ada3554e08836a7061d639ae79a73cd38415443291c232eee92f73c000f1de8c8d
6
+ metadata.gz: 05ed566b4eba95b988ec2873588d2f6ac10d650d1807a54ca0374f2657d8f5e29dea50daf8b255f55c7ac58d6e553a1b646a1cef71f0ad31d809331f59eda612
7
+ data.tar.gz: 2203994c5c48cff744ad5e1d6583eec04211cd664b9d60aafbbec1cee02ff74dc1ae9a67350736655388e9d419c54c7bf46f897c6b66474805908c103bee4e97
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.6)
5
5
  rails (>= 6.0)
6
6
 
7
7
  GEM
@@ -1,65 +1,76 @@
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
+ closing = "#{'#' + indent}]),"
47
+ closing += " #{remaining_opts.join(', ')}" unless remaining_opts.empty?
48
+ lines << closing.rstrip
49
+ lines
50
+ else
51
+ line = base_line
52
+ line += " #{opts.join(', ')}" unless opts.empty?
53
+ line.rstrip
55
54
  end
56
- else
57
- "default(#{value.inspect})"
58
55
  end
59
56
  end
60
57
 
61
- def self.json_like?(value)
62
- value.is_a?(String) && value.strip.match?(/\A[\[{].*[\]}]\z/m)
58
+ def self.multiline_default?(value)
59
+ value.is_a?(String) && value.strip.start_with?("[") && value.strip.end_with?("]") && value.include?(",")
60
+ end
61
+
62
+ def self.format_multiline_default(value)
63
+ parsed = JSON.parse(value)
64
+ return [value] unless parsed.is_a?(Array)
65
+
66
+ items = parsed.is_a?(Array) && parsed[0].is_a?(Array) ? parsed[0] : parsed
67
+
68
+ items.map.with_index do |item, idx|
69
+ comma = idx == items.size - 1 ? "" : ","
70
+ "#{item.to_json}#{comma}"
71
+ end
72
+ rescue JSON::ParserError
73
+ [value]
63
74
  end
64
75
  end
65
76
  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.6"
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.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Serhii Bodnaruk