arql 0.4.4 → 0.4.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: 14f47987d63ecc0665d5cce2b8b503ffeff2587f6235499138f6d6da22fc104c
4
- data.tar.gz: aa143c6f15dd4e2df5f79a992547548938ba59fd8126f9bfdfeaa2dcb83f0cef
3
+ metadata.gz: 2d0018fc91ed033164ab2a18c2c3e5edcaaffaefe2ec9d3cc7fab0cc3323e53a
4
+ data.tar.gz: '07874855f77dd53005dc53c213e683fb9e6e0b05896a9f8874be0d6383fc474c'
5
5
  SHA512:
6
- metadata.gz: '0863699e29b5c7ec31db4359657e7c3d3010f2ca5636484c9f93a2e1826f06e6daf846aad678a5a59d8b0414eb9470058788143cdafc8187e653c43916d29b5a'
7
- data.tar.gz: fe913b85a782e139af4d1751d54fc9f33178eed8a202b593acb81f5db02c13af71e86e817f234285c6350970324000f42dc5422107fe8f2877bb8fedce235c22
6
+ metadata.gz: 732c1c901ed187c26930cf65dfb278257a65ab446635389ae6741553617d5873b2e733f6c70c52983ca536e95e6b914d4e43857af35c2dbcca9f052d2fd9416d
7
+ data.tar.gz: 063c3c0e6734073644dfd54b5735a9813262cfabbd0cad788ea3b56850a99daacdf653747f6e9613022837c19c5ed53ba08d3a6fd1f8d07af5c1f4bfb7d80bef
data/Gemfile.lock CHANGED
@@ -1,10 +1,11 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- arql (0.4.4)
4
+ arql (0.4.6)
5
5
  activerecord (>= 6.1.5, < 7.1.0)
6
6
  activesupport (>= 6.1.5, < 7.1.0)
7
7
  caxlsx (~> 3.3.0)
8
+ chartkick (~> 5.0.7)
8
9
  composite_primary_keys (~> 14.0.4)
9
10
  kaminari-activerecord (~> 1.2.2)
10
11
  mysql2 (~> 0.5.4)
@@ -40,6 +41,7 @@ GEM
40
41
  marcel (~> 1.0)
41
42
  nokogiri (~> 1.10, >= 1.10.4)
42
43
  rubyzip (>= 1.3.0, < 3)
44
+ chartkick (5.0.7)
43
45
  coderay (1.1.3)
44
46
  composite_primary_keys (14.0.4)
45
47
  activerecord (~> 7.0.2)
data/arql.gemspec CHANGED
@@ -44,5 +44,6 @@ Gem::Specification.new do |spec|
44
44
  spec.add_dependency 'ransack', '~> 3.2.1'
45
45
  spec.add_dependency 'youplot', '~> 0.4.5'
46
46
  spec.add_dependency 'tty-tree', '~> 0.4.0'
47
+ spec.add_dependency 'chartkick', '~> 5.0.7'
47
48
  spec.add_development_dependency "gem-release", "~> 2.2.2"
48
49
  end
data/lib/arql/app.rb CHANGED
@@ -25,6 +25,36 @@ module Arql
25
25
  def config
26
26
  instance.config
27
27
  end
28
+
29
+ def create(options)
30
+ options = {
31
+ config_file: default_config_file,
32
+ initializer: default_initializer,
33
+ babel_compatable: false,
34
+ ssh: {}
35
+ }.merge(options)
36
+ options = OpenStruct.new(options)
37
+ app = App.new(options)
38
+ app.instance_exec do
39
+ show_sql if should_show_sql?
40
+ write_sql if should_write_sql?
41
+ append_sql if should_append_sql?
42
+ end
43
+ require 'arql/commands'
44
+ nil
45
+ end
46
+
47
+ def default_config_file
48
+ ['~/.arql.yml', '~/.arql.yaml', '~/.arql.d/init.yml', '~/.arql.d/init.yaml'].find { |f|
49
+ File.file?(File.expand_path(f))
50
+ }.try { |f| File.expand_path(f) }
51
+ end
52
+
53
+ def default_initializer
54
+ ['~/.arql.rb', '~/.arql.d/init.rb',].find { |f|
55
+ File.file?(File.expand_path(f))
56
+ }.try { |f| File.expand_path(f) }
57
+ end
28
58
  end
29
59
 
30
60
  def prompt
data/lib/arql/chart.rb ADDED
@@ -0,0 +1,57 @@
1
+ require 'chartkick'
2
+
3
+ module Arql
4
+ module Chart
5
+ include Chartkick::Helper
6
+
7
+ def line_chart(data_source, **options)
8
+ html_chart "LineChart", data_source, **options
9
+ end
10
+
11
+ def pie_chart(data_source, **options)
12
+ html_chart "PieChart", data_source, **options
13
+ end
14
+
15
+ def column_chart(data_source, **options)
16
+ html_chart "ColumnChart", data_source, **options
17
+ end
18
+
19
+ def bar_chart(data_source, **options)
20
+ html_chart "BarChart", data_source, **options
21
+ end
22
+
23
+ def area_chart(data_source, **options)
24
+ html_chart "AreaChart", data_source, **options
25
+ end
26
+
27
+ def scatter_chart(data_source, **options)
28
+ html_chart "ScatterChart", data_source, **options
29
+ end
30
+
31
+ def geo_chart(data_source, **options)
32
+ html_chart "GeoChart", data_source, **options
33
+ end
34
+
35
+ def timeline(data_source, **options)
36
+ html_chart "Timeline", data_source, **options
37
+ end
38
+
39
+ def html_chart(klass, data_source, **options)
40
+ chart = chartkick_chart(klass, data_source, **options)
41
+
42
+ html = <<~HTML
43
+ <html>
44
+ <head>
45
+ <script src="https://www.hackit.fun/js/Chart.bundle.js"></script>
46
+ <script src="https://www.hackit.fun/js/chartkick.js"></script>
47
+ </head>
48
+ <body style="color: red">
49
+ #{chart}
50
+ </body>
51
+ </html>
52
+ HTML
53
+ html = html.lines.reject {|line| line =~ /(data-turbo)|(Chartkick" in window)|(^\s*\}\s*else\s*\{\s*$)|(chartkick:load)|(^\s*\}\s*$)/}.join("\n")
54
+ IRuby.display(html, mime: 'text/html')
55
+ end
56
+ end
57
+ end
@@ -1,14 +1,17 @@
1
1
  require 'terminal-table'
2
+ require 'arql/table'
2
3
 
3
4
  module Arql::Commands
4
5
  module Models
5
6
  class << self
6
7
  def filter_tables(env_name, definition, format, table_regexp=nil)
7
8
  result = ''
8
- result << '-- ' if format == 'sql'
9
- result << "Environment: #{env_name}\n"
10
- result << "------------------------------\n\n"
11
- if format == 'sql'
9
+ if format.to_s == 'sql'
10
+ result << '-- '
11
+ result << "Environment: #{env_name}\n"
12
+ result << "------------------------------\n\n"
13
+ end
14
+ if format.to_s == 'sql'
12
15
  definition.models.each do |model|
13
16
  if table_regexp? || ( model[:table] =~ table_regexp || model[:comment] =~ table_regexp )
14
17
  result << "-- Table: #{table_name}\n\n"
@@ -16,77 +19,43 @@ module Arql::Commands
16
19
  end
17
20
  end
18
21
  else
19
- Terminal::Table.new do |t|
20
- t.style = table_style_for_format(format)
21
- t << ['Table Name', 'Model Class', 'Abbr', 'Comment']
22
- t << :separator
22
+ tbl = Arql::Table.new("Environment: #{env_name}") { |t|
23
+ t.headers = ['Table Name', 'Model Class', 'Abbr', 'Comment']
23
24
  definition.models.each do |model|
24
25
  if table_regexp.nil? || ( model[:table] =~ table_regexp || model[:comment] =~ table_regexp )
25
- t << [model[:table], model[:model].name, model[:abbr] || '', model[:comment] || '']
26
+ t.body << [model[:table], model[:model].name, model[:abbr] || '', model[:comment] || '']
26
27
  end
27
28
  end
28
- end.try { |e|
29
- case format
30
- when 'md'
31
- result << e.to_s.lines.map { |l| ' ' + l }.join
32
- when 'org'
33
- result << e.to_s.lines.map { |l| ' ' + l.gsub(/^\+|\+$/, '|') }.join
34
- else
35
- result << e.to_s
36
- end
37
29
  }
30
+ if $iruby && format.to_s == 'terminal'
31
+ return tbl.to_iruby
32
+ else
33
+ result << tbl.to_terminal(format)
34
+ end
38
35
  end
39
36
  result
40
37
  end
41
38
 
42
39
  def filter_columns(env_name, definition, format, column_regexp=nil)
43
- result = ''
44
- result << '-- ' if format == 'sql'
45
- result << "Environment: #{env_name}\n"
46
- result << "------------------------------\n\n"
47
- Terminal::Table.new do |t|
48
- t.style = table_style_for_format(format)
49
- t << ['Table', 'Model', 'Name', 'SQL Type', 'Ruby Type', 'Limit', 'Precision', 'Scale', 'Default', 'Nullable', 'Comment']
50
- t << :separator
40
+ tbl = Arql::Table.new("Environment: #{env_name}") { |t|
41
+ t.headers = ['Table', 'Model', 'Name', 'SQL Type', 'Ruby Type', 'Limit', 'Precision', 'Scale', 'Default', 'Nullable', 'Comment']
51
42
  definition.models.each do |model_def|
52
43
  model_class = model_def[:model]
53
44
  matched_columns = model_class.columns.select { |column| column.name =~ column_regexp || column.comment =~ column_regexp }
54
45
  next if matched_columns.empty?
55
46
  matched_columns.each do |column|
56
- t << [model_def[:table], model_class.name, column.name, column.sql_type,
47
+ t.body << [model_def[:table], model_class.name, column.name, column.sql_type,
57
48
  column.sql_type_metadata.type, column.sql_type_metadata.limit || '',
58
49
  column.sql_type_metadata.precision || '', column.sql_type_metadata.scale || '', column.default || '',
59
50
  column.null, "#{model_def[:comment]} - #{column.comment}"]
60
51
  end
61
52
  end
62
- end.try { |e|
63
- case format
64
- when 'md'
65
- result << e.to_s.lines.map { |l| ' ' + l }.join
66
- when 'org'
67
- result << e.to_s.lines.map { |l| ' ' + l.gsub(/^\+|\+$/, '|') }.join
68
- else
69
- result << e.to_s
70
- end
71
53
  }
72
- result
73
- end
74
54
 
75
- def table_style_for_format(format)
76
- case format
77
- when 'md'
78
- {
79
- border_top: false,
80
- border_bottom: false,
81
- border_i: '|'
82
- }
83
- when 'org'
84
- {
85
- border_top: false,
86
- border_bottom: false,
87
- }
55
+ if $iruby && format.to_s == 'terminal'
56
+ return tbl.to_iruby
88
57
  else
89
- {}
58
+ result << tbl.to_terminal(format)
90
59
  end
91
60
  end
92
61
  end
@@ -17,19 +17,17 @@ module Arql
17
17
  end
18
18
 
19
19
  def t(compact: false, format: :terminal)
20
- puts Terminal::Table.new { |t|
21
- t.style = self.class.table_style_for_format(format)
22
- v(compact: compact).each { |row| t << (row || :separator) }
23
- }.try { |e|
24
- case format
25
- when :md
26
- e.to_s.lines.map { |l| ' ' + l }.join
27
- when :org
28
- e.to_s.lines.map { |l| ' ' + l.gsub(/^\+|\+$/, '|') }.join
29
- else
30
- e.to_s
31
- end
32
- }
20
+ tbl = Arql::Table.new do |t|
21
+ values = v(compact: compact)
22
+ t.headers = values&.first || []
23
+ (values[2..] || []).each { |row| t.body << (row || []) } if values.size > 2
24
+ end
25
+
26
+ if $iruby && format.to_s == 'terminal'
27
+ return tbl.to_iruby
28
+ else
29
+ puts tbl.to_terminal(format)
30
+ end
33
31
  end
34
32
 
35
33
  def vd(compact: false)
@@ -68,7 +66,6 @@ module Arql
68
66
  end
69
67
 
70
68
  class_methods do
71
-
72
69
  def v
73
70
  t = [['PK', 'Name', 'SQL Type', 'Ruby Type', 'Limit', 'Precision', 'Scale', 'Default', 'Nullable', 'Comment']]
74
71
  t << nil
@@ -95,23 +92,21 @@ module Arql
95
92
  when :sql
96
93
  '-- '
97
94
  end
98
- puts "\n#{heading_prefix}Table: #{table_name}\n\n"
99
- if format == :sql
95
+ caption = "\n#{heading_prefix}Table: #{table_name}\n\n"
96
+ if format.to_s == 'sql'
100
97
  puts to_create_sql + ';'
101
98
  else
102
- puts(Terminal::Table.new { |t|
103
- t.style = self.table_style_for_format(format)
104
- v.each { |row| t << (row || :separator) }
105
- }.try { |e|
106
- case format
107
- when :md
108
- e.to_s.lines.map { |l| ' ' + l }.join
109
- when :org
110
- e.to_s.lines.map { |l| ' ' + l.gsub(/^\+|\+$/, '|') }.join
111
- else
112
- e.to_s
113
- end
114
- })
99
+ tbl = Arql::Table.new(caption) do |t|
100
+ values = v()
101
+ t.headers = values&.first || []
102
+ (values[2..] || []).each { |row| t.body << (row || []) } if values.size > 2
103
+ end
104
+
105
+ if $iruby && format.to_s == 'terminal'
106
+ return tbl.to_iruby
107
+ else
108
+ puts tbl.to_terminal(format)
109
+ end
115
110
  end
116
111
  end
117
112
 
@@ -145,24 +140,6 @@ module Arql
145
140
  def dump(filename, no_create_table=false)
146
141
  Arql::Mysqldump.new(superclass.definition.options).dump_table(filename, table_name, no_create_table)
147
142
  end
148
-
149
- def table_style_for_format(format)
150
- case format.to_s
151
- when 'md'
152
- {
153
- border_top: false,
154
- border_bottom: false,
155
- border_i: '|'
156
- }
157
- when 'org'
158
- {
159
- border_top: false,
160
- border_bottom: false,
161
- }
162
- else
163
- {}
164
- end
165
- end
166
143
  end
167
144
  end
168
145
  end
@@ -76,6 +76,16 @@ module Arql
76
76
  model[:comment] = @comments[table_name]
77
77
  @models << model
78
78
  end
79
+
80
+ order_column = @options[:order_column]
81
+ if order_column
82
+ Thread.new do
83
+ @models.each do |model|
84
+ model_class = model[:model]
85
+ model_class.implicit_order_column = order_column if model_class.column_names.include?(order_column)
86
+ end
87
+ end
88
+ end
79
89
  end
80
90
 
81
91
  def model_names_mapping
@@ -144,8 +154,6 @@ module Arql
144
154
  end
145
155
  self.table_name = table_name
146
156
  self.inheritance_column = nil
147
- order_column = options[:order_column]
148
- self.implicit_order_column = order_column if order_column
149
157
  ActiveRecord.default_timezone = :local
150
158
  if options[:created_at].present?
151
159
  define_singleton_method :timestamp_attributes_for_create do
@@ -32,46 +32,25 @@ class Array
32
32
  # if options[:compact]
33
33
  # attrs = attrs.select { |e| any { |r| r.attributes[e.to_s]&.present? } }
34
34
  # end
35
- puts Terminal::Table.new { |t|
35
+ tbl = Arql::Table.new { |t|
36
36
  t.style = table_style_for_format(format)
37
- t << attrs
38
- t << :separator
37
+ t.headers = attrs
39
38
  each do |e|
40
- t << e.attributes.values_at(*attrs.map(&:to_s))
41
- end
42
- }.try { |e|
43
- case format
44
- when 'md'
45
- e.to_s.lines.map { |l| ' ' + l }.join
46
- when 'org'
47
- e.to_s.lines.map { |l| ' ' + l.gsub(/^\+|\+$/, '|') }.join
48
- else
49
- e.to_s
39
+ t.body << e.attributes.values_at(*attrs.map(&:to_s))
50
40
  end
51
41
  }
52
42
  else
53
- table = Terminal::Table.new { |t|
54
- t.style = table_style_for_format(format)
55
- v(**options).each { |row| t << (row || :separator)}
56
- }.try { |e|
57
- case format
58
- when 'md'
59
- e.to_s.lines.map { |l| ' ' + l }.join
60
- when 'org'
61
- e.to_s.lines.map { |l| ' ' + l.gsub(/^\+|\+$/, '|') }.join
62
- else
63
- e.to_s
64
- end
43
+ values = v(**options)
44
+ tbl = Arql::Table.new { |t|
45
+ t.headers = values&.first || []
46
+ (values[2..] || []).each { |row| t.body << (row || []) } if values.size > 2
65
47
  }
48
+ end
66
49
 
67
- terminal_width = `tput cols`.to_i
68
- if table.lines.first.size > terminal_width
69
- table = table.lines.map(&:chomp)
70
- puts table[0..2].join("\n")
71
- puts table[3..-1].join("\n#{'-' * terminal_width}\n")
72
- else
73
- puts table
74
- end
50
+ if $iruby
51
+ tbl.to_iruby
52
+ else
53
+ puts tbl.to_terminal(format)
75
54
  end
76
55
  end
77
56
 
@@ -11,6 +11,17 @@ class String
11
11
  expa
12
12
  end
13
13
 
14
+ def to_link
15
+ html = <<~EOF
16
+ <a href="#{expa}" target="_blank">#{expa}</a>
17
+ EOF
18
+ IRuby.display(html, mime: 'text/html')
19
+ end
20
+
21
+ def to_file
22
+ File.open(expa)
23
+ end
24
+
14
25
  def parse_excel
15
26
  if File.file?(File.expand_path(self))
16
27
  Kernel.parse_excel(File.expand_path(self))
data/lib/arql/table.rb ADDED
@@ -0,0 +1,86 @@
1
+ require 'terminal-table'
2
+
3
+ module Arql
4
+ class Table
5
+ attr_accessor :caption, :headers, :body
6
+ def initialize(caption=nil, headers=[], body=[])
7
+ @headers = headers
8
+ @body = body
9
+ @caption = caption
10
+ yield(self) if block_given?
11
+ end
12
+
13
+ def to_iruby
14
+ IRuby.display(to_html, mime: 'text/html')
15
+ end
16
+
17
+ def to_terminal(format='terminal')
18
+ tbl = Terminal::Table.new do |t|
19
+ t.style = terminal_style_for_format(format)
20
+ t << headers || []
21
+ t << :separator
22
+ (body || []).each { |row| t << row }
23
+ end
24
+
25
+ title = @caption.try do |c|
26
+ "#{c}\n---------------------------------------\n\n"
27
+ end || ''
28
+
29
+ table = case format.to_s
30
+ when 'md'
31
+ tbl.to_s.lines.map { |l| ' ' + l }.join
32
+ when 'org'
33
+ tbl.to_s.lines.map { |l| ' ' + l.gsub(/^\+|\+$/, '|') }.join
34
+ else
35
+ tbl.to_s
36
+ end
37
+
38
+ terminal_width = `tput cols`.to_i
39
+ table_lines = table.lines.map(&:chomp)
40
+ if table_lines.first.size > terminal_width
41
+ title + (table_lines[0..2] || []).join("\n") + "\n" + (table_lines[3..-1] || []).join("\n#{'-' * terminal_width}\n")
42
+ else
43
+ title + table
44
+ end
45
+
46
+ end
47
+
48
+ def terminal_style_for_format(format)
49
+ case format.to_s
50
+ when 'md'
51
+ {
52
+ border_top: false,
53
+ border_bottom: false,
54
+ border_i: '|'
55
+ }
56
+ when 'org'
57
+ {
58
+ border_top: false,
59
+ border_bottom: false,
60
+ }
61
+ else
62
+ {}
63
+ end
64
+ end
65
+
66
+ def to_html
67
+ html = "<table style='border-collapse: collapse; font-family: JetBrains Mono, Source Code Pro, Ubuntu Mono, Monaco, Menlo, Courier New'>"
68
+ html << "<caption style='text-align: left;'>#{@caption}</caption>" if @caption.present?
69
+ html << "<thead><tr>"
70
+ (@headers || []).each do |h|
71
+ html << "<th style='text-align: left; white-space: nowrap;'>#{h}</th>"
72
+ end
73
+ html << "</tr></thead>"
74
+ html << "<tbody>"
75
+ (@body || []).each do |row|
76
+ html << "<tr>"
77
+ row.each do |r|
78
+ html << "<td style='text-align: left; white-space: nowrap;'>#{r}</td>"
79
+ end
80
+ html << "</tr>"
81
+ end
82
+ html << "</tbody>"
83
+ html << "</table>"
84
+ end
85
+ end
86
+ end
data/lib/arql/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Arql
2
- VERSION = "0.4.4"
2
+ VERSION = "0.4.6"
3
3
  end
data/lib/arql.rb CHANGED
@@ -1,3 +1,4 @@
1
+ ENV['RAILS_DISABLE_DEPRECATED_TO_S_CONVERSION'] = 'true'
1
2
  require 'table_print'
2
3
  require 'roo'
3
4
  require 'caxlsx'
@@ -24,5 +25,19 @@ require 'arql/ext/active_record/relation'
24
25
  require 'arql/ext/active_record/result'
25
26
  require 'arql/ext/ransack/search'
26
27
 
28
+ $iruby = false
29
+
27
30
  module Arql
31
+ def self.create(options)
32
+ if ::Object.const_defined?(:IRuby) && ::IRuby.const_defined?(:OStream) && $stdout.is_a?(IRuby::OStream)
33
+ IRuby::Kernel.instance.switch_backend!(:pry)
34
+ $iruby = true
35
+ end
36
+ App.create(options)
37
+ end
28
38
  end
39
+
40
+ if ::Object.const_defined?(:IRuby)
41
+ require 'arql/chart'
42
+ ::Kernel.include(Arql::Chart)
43
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: arql
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.4
4
+ version: 0.4.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Liu Xiang
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2024-05-21 00:00:00.000000000 Z
11
+ date: 2024-05-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: mysql2
@@ -274,6 +274,20 @@ dependencies:
274
274
  - - "~>"
275
275
  - !ruby/object:Gem::Version
276
276
  version: 0.4.0
277
+ - !ruby/object:Gem::Dependency
278
+ name: chartkick
279
+ requirement: !ruby/object:Gem::Requirement
280
+ requirements:
281
+ - - "~>"
282
+ - !ruby/object:Gem::Version
283
+ version: 5.0.7
284
+ type: :runtime
285
+ prerelease: false
286
+ version_requirements: !ruby/object:Gem::Requirement
287
+ requirements:
288
+ - - "~>"
289
+ - !ruby/object:Gem::Version
290
+ version: 5.0.7
277
291
  - !ruby/object:Gem::Dependency
278
292
  name: gem-release
279
293
  requirement: !ruby/object:Gem::Requirement
@@ -326,6 +340,7 @@ files:
326
340
  - initializer-structure.org
327
341
  - lib/arql.rb
328
342
  - lib/arql/app.rb
343
+ - lib/arql/chart.rb
329
344
  - lib/arql/cli.rb
330
345
  - lib/arql/commands.rb
331
346
  - lib/arql/commands/info.rb
@@ -358,6 +373,7 @@ files:
358
373
  - lib/arql/repl.rb
359
374
  - lib/arql/ssh_proxy.rb
360
375
  - lib/arql/ssh_proxy_patch.rb
376
+ - lib/arql/table.rb
361
377
  - lib/arql/vd.rb
362
378
  - lib/arql/version.rb
363
379
  - oss-files-zh_CN.org