kolla 0.1.0 → 0.1.1

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: aa7af9c82815bce009d2214337606318aaf9af44fa9874fbbd70793926ab9aca
4
- data.tar.gz: 58d03e71d4ebb50a0ff1bdef9d33dfebb0cd9f3199d2a7f2e5212f90d3df4742
3
+ metadata.gz: 98522a2e33c960997f0f916d48c56aeb29fdade6f153e5b1e6098b769b4cf6a4
4
+ data.tar.gz: 15ac9b7c46afb68861a8243155a30ac825c5e6071bc4fe567722b42edf0fc963
5
5
  SHA512:
6
- metadata.gz: eaf1ca3aaf552e86d5812f2a4f11be50a9766c5cc2e1fa862a77c74f3930381aa01e932cbf660d51bac053fe96b12d819da6c07f5ef0c03af599c547f2206745
7
- data.tar.gz: 46903fc9c6446b1f51c8896ae88140e88ad9483e62336aa2dc1e9e3a95f3d994e61de5bfa09f15e8bce88a42d0faec91506cc20499f953733871c26edbd78ee5
6
+ metadata.gz: f51517d99c73fdb2748ce884c8ebe7959771f586dcc95e10e4d56434a530ec64b7a9a6504835e2097e5d36064efcdbb6a338119b57e475573e5c29141f5c11db
7
+ data.tar.gz: 1a632a31c1667b7fad4b37693f4241fd0ed0a77a44958f50ef1dc2f5e7066c10c94dcc4f40548d66ca459561275e30805eaed0ad6b5cbdd4e0d16457f0e60c2d
data/lib/kolla/parser.rb CHANGED
@@ -5,6 +5,9 @@ module Kolla
5
5
  DEFAULT_FILE = File.join("db", "schema.rb").freeze
6
6
  IGNORED_PREFIXES = %w[action_ active_].freeze
7
7
 
8
+ Field = Struct.new(:name, :type, :constraints, keyword_init: true)
9
+ Index = Struct.new(:columns, :unique, keyword_init: true)
10
+
8
11
  def initialize(file_path: DEFAULT_FILE)
9
12
  @file_path = File.expand_path(file_path)
10
13
  @content = File.read(@file_path)
@@ -26,32 +29,61 @@ module Kolla
26
29
  block = match[1]
27
30
  fields = parse_fields(block)
28
31
  indexes = parse_indexes(block)
29
- fields + indexes
32
+
33
+ format_output(name, fields, indexes)
30
34
  end
31
35
 
32
36
  private
33
37
 
34
38
  def parse_fields(block)
35
39
  field_regex = /t\.(\w+)\s+"(\w+)"(.*)/
36
- block.scan(field_regex).map do |type, name, rest|
40
+ block.scan(field_regex).filter_map do |type, name, rest|
37
41
  next if type == "index"
38
42
 
39
- parts = [type]
40
- parts << "null: false" if rest.include?("null: false")
43
+ constraints = []
44
+ constraints << "null: false" if rest.include?("null: false")
41
45
  if (default_match = rest.match(/default:\s*("(?:[^"\\]|\\.)*"|[\w.]+)/))
42
- parts << "default: #{default_match[1]}"
46
+ constraints << "default: #{default_match[1]}"
43
47
  end
44
- "#{name} (#{parts.join(', ')})"
45
- end.compact
48
+ Field.new(name: name, type: type, constraints: constraints)
49
+ end
46
50
  end
47
51
 
48
52
  def parse_indexes(block)
49
53
  index_regex = /t\.index\s+\[([^\]]+)\](.*)$/
50
54
  block.scan(index_regex).map do |columns, rest|
51
55
  cols = columns.scan(/"(\w+)"/).flatten
52
- unique = rest.include?("unique: true") ? " (unique)" : ""
53
- "index: [#{cols.join(', ')}]#{unique}"
56
+ Index.new(columns: cols, unique: rest.include?("unique: true"))
54
57
  end
55
58
  end
59
+
60
+ def format_output(table_name, fields, indexes)
61
+ return [] if fields.empty? && indexes.empty?
62
+
63
+ name_width = fields.map { |f| f.name.length }.max
64
+ type_width = fields.map { |f| f.type.length }.max
65
+
66
+ lines = []
67
+ lines << table_name
68
+ lines << "\u2500" * [name_width + type_width + 5, table_name.length].max
69
+
70
+ fields.each do |f|
71
+ line = " #{f.name.ljust(name_width)} #{f.type.ljust(type_width)}"
72
+ line << " #{f.constraints.join(', ')}" unless f.constraints.empty?
73
+ lines << line
74
+ end
75
+
76
+ unless indexes.empty?
77
+ lines << ""
78
+ lines << " Indexes:"
79
+ indexes.each do |idx|
80
+ line = " [#{idx.columns.join(', ')}]"
81
+ line << " (unique)" if idx.unique
82
+ lines << line
83
+ end
84
+ end
85
+
86
+ lines
87
+ end
56
88
  end
57
89
  end
data/lib/kolla/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Kolla
4
- VERSION = "0.1.0"
4
+ VERSION = "0.1.1"
5
5
  end
data/lib/kolla.rb CHANGED
@@ -1,5 +1,9 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ if RUBY_VERSION < "3.0"
4
+ abort "kolla requires Ruby 3.0 or later (you have #{RUBY_VERSION})"
5
+ end
6
+
3
7
  require_relative "kolla/version"
4
8
  require_relative "kolla/errors"
5
9
  require_relative "kolla/parser"
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: kolla
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Antony Sastre