arql 0.1.33 → 0.2.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +25 -9
- data/arql.gemspec +2 -0
- data/lib/arql.rb +3 -0
- data/lib/arql/app.rb +2 -1
- data/lib/arql/definition.rb +28 -4
- data/lib/arql/ext/array.rb +53 -2
- data/lib/arql/ext/hash.rb +25 -0
- data/lib/arql/ext/kernel.rb +43 -0
- data/lib/arql/ext/object.rb +4 -0
- data/lib/arql/repl.rb +2 -2
- data/lib/arql/version.rb +1 -1
- metadata +31 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: bcf50149ad9ded8d667ea9afc40dea78d432525c8c62fe4f33d0d164e02acb81
|
4
|
+
data.tar.gz: a6fd83949c0c87f1d4891fae1a8fb0fa65e13a48f9e8ddb327b02f499bdbc73a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9f73ccc2e3669a6f10fa7555737d09fadec7e0c0457c5b8819dae65055aa6a045153cce2137ccf9a9742f509d9f5c8567fd617e9f3dda7e2136935cf3686a2f3
|
7
|
+
data.tar.gz: ca3d6d547e8267066d72a6e67b09012b42b2e4defa36669c8b304707dc434cd6194a84a201c0fac8d3ed3f749ca017f2b4eaba6ce1f0a3813cfab34140827bc5
|
data/Gemfile.lock
CHANGED
@@ -1,15 +1,17 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
arql (0.
|
4
|
+
arql (0.2.4)
|
5
5
|
activerecord (~> 6.0.3)
|
6
6
|
activesupport (~> 6.0.3)
|
7
|
+
caxlsx (~> 3.0.2)
|
7
8
|
mysql2 (~> 0.5.3)
|
8
9
|
net-ssh-gateway (~> 2.0.0)
|
9
10
|
pry (~> 0.13.1)
|
10
11
|
pry-byebug (~> 3.9.0)
|
11
12
|
pry-doc (~> 1.1.0)
|
12
13
|
rainbow (~> 3.0.0)
|
14
|
+
roo (~> 2.8.3)
|
13
15
|
sqlite3 (~> 1.4)
|
14
16
|
table_print (~> 1.5.6)
|
15
17
|
terminal-table (~> 1.8.0)
|
@@ -17,28 +19,38 @@ PATH
|
|
17
19
|
GEM
|
18
20
|
remote: https://rubygems.org/
|
19
21
|
specs:
|
20
|
-
activemodel (6.0.3.
|
21
|
-
activesupport (= 6.0.3.
|
22
|
-
activerecord (6.0.3.
|
23
|
-
activemodel (= 6.0.3.
|
24
|
-
activesupport (= 6.0.3.
|
25
|
-
activesupport (6.0.3.
|
22
|
+
activemodel (6.0.3.3)
|
23
|
+
activesupport (= 6.0.3.3)
|
24
|
+
activerecord (6.0.3.3)
|
25
|
+
activemodel (= 6.0.3.3)
|
26
|
+
activesupport (= 6.0.3.3)
|
27
|
+
activesupport (6.0.3.3)
|
26
28
|
concurrent-ruby (~> 1.0, >= 1.0.2)
|
27
29
|
i18n (>= 0.7, < 2)
|
28
30
|
minitest (~> 5.1)
|
29
31
|
tzinfo (~> 1.1)
|
30
32
|
zeitwerk (~> 2.2, >= 2.2.2)
|
31
33
|
byebug (11.1.3)
|
34
|
+
caxlsx (3.0.2)
|
35
|
+
htmlentities (~> 4.3, >= 4.3.4)
|
36
|
+
mimemagic (~> 0.3)
|
37
|
+
nokogiri (~> 1.10, >= 1.10.4)
|
38
|
+
rubyzip (>= 1.3.0, < 3)
|
32
39
|
coderay (1.1.3)
|
33
40
|
concurrent-ruby (1.1.7)
|
41
|
+
htmlentities (4.3.4)
|
34
42
|
i18n (1.8.5)
|
35
43
|
concurrent-ruby (~> 1.0)
|
36
44
|
method_source (1.0.0)
|
37
|
-
|
45
|
+
mimemagic (0.3.5)
|
46
|
+
mini_portile2 (2.4.0)
|
47
|
+
minitest (5.14.2)
|
38
48
|
mysql2 (0.5.3)
|
39
49
|
net-ssh (6.1.0)
|
40
50
|
net-ssh-gateway (2.0.0)
|
41
51
|
net-ssh (>= 4.0.0)
|
52
|
+
nokogiri (1.10.10)
|
53
|
+
mini_portile2 (~> 2.4.0)
|
42
54
|
pry (0.13.1)
|
43
55
|
coderay (~> 1.1)
|
44
56
|
method_source (~> 1.0)
|
@@ -50,8 +62,12 @@ GEM
|
|
50
62
|
yard (~> 0.9.11)
|
51
63
|
rainbow (3.0.0)
|
52
64
|
rake (12.3.3)
|
65
|
+
roo (2.8.3)
|
66
|
+
nokogiri (~> 1)
|
67
|
+
rubyzip (>= 1.3.0, < 3.0.0)
|
68
|
+
rubyzip (2.3.0)
|
53
69
|
sqlite3 (1.4.2)
|
54
|
-
table_print (1.5.
|
70
|
+
table_print (1.5.7)
|
55
71
|
terminal-table (1.8.0)
|
56
72
|
unicode-display_width (~> 1.1, >= 1.1.1)
|
57
73
|
thread_safe (0.3.6)
|
data/arql.gemspec
CHANGED
@@ -37,4 +37,6 @@ Gem::Specification.new do |spec|
|
|
37
37
|
spec.add_dependency 'rainbow', '~> 3.0.0'
|
38
38
|
spec.add_dependency 'terminal-table', '~> 1.8.0'
|
39
39
|
spec.add_dependency 'table_print', '~> 1.5.6'
|
40
|
+
spec.add_dependency 'roo', '~> 2.8.3'
|
41
|
+
spec.add_dependency 'caxlsx', '~> 3.0.2'
|
40
42
|
end
|
data/lib/arql.rb
CHANGED
data/lib/arql/app.rb
CHANGED
@@ -4,7 +4,7 @@ module Arql
|
|
4
4
|
class App
|
5
5
|
|
6
6
|
class << self
|
7
|
-
attr_accessor :log_io, :env, :prompt
|
7
|
+
attr_accessor :log_io, :env, :prompt, :instance
|
8
8
|
|
9
9
|
def config
|
10
10
|
@@effective_config
|
@@ -29,6 +29,7 @@ module Arql
|
|
29
29
|
Connection.open(connect_options)
|
30
30
|
@definition = Definition.new(effective_config)
|
31
31
|
load_initializer!
|
32
|
+
App.instance = self
|
32
33
|
end
|
33
34
|
|
34
35
|
def connect_options
|
data/lib/arql/definition.rb
CHANGED
@@ -26,6 +26,14 @@ module Arql
|
|
26
26
|
self.class.to_upsert_sql([self])
|
27
27
|
end
|
28
28
|
|
29
|
+
def write_csv(filename, *fields, **options)
|
30
|
+
[self].write_csv(filename, *fields, **options)
|
31
|
+
end
|
32
|
+
|
33
|
+
def write_excel(filename, *fields, **options)
|
34
|
+
[self].write_excel(filename, *fields, **options)
|
35
|
+
end
|
36
|
+
|
29
37
|
included do
|
30
38
|
end
|
31
39
|
|
@@ -79,7 +87,7 @@ module Arql
|
|
79
87
|
table_name.camelize.tap do |const_name|
|
80
88
|
const_name = 'Modul' if const_name == 'Module'
|
81
89
|
const_name = 'Clazz' if const_name == 'Class'
|
82
|
-
Class.new(
|
90
|
+
Class.new(::ArqlModel) do
|
83
91
|
include Arql::Extension
|
84
92
|
self.primary_key = pkey
|
85
93
|
self.table_name = table_name
|
@@ -116,6 +124,8 @@ module Arql
|
|
116
124
|
end
|
117
125
|
end
|
118
126
|
end
|
127
|
+
|
128
|
+
App.instance&.load_initializer!
|
119
129
|
end
|
120
130
|
end
|
121
131
|
|
@@ -123,12 +133,13 @@ module Arql
|
|
123
133
|
@@options = options
|
124
134
|
@@models = []
|
125
135
|
ActiveRecord::Base.connection.tap do |conn|
|
136
|
+
Object.const_set('ArqlModel', Class.new(ActiveRecord::Base) { self.abstract_class = true })
|
126
137
|
conn.tables.each do |table_name|
|
127
138
|
conn.primary_key(table_name).tap do |pkey|
|
128
139
|
table_name.camelize.tap do |const_name|
|
129
140
|
const_name = 'Modul' if const_name == 'Module'
|
130
141
|
const_name = 'Clazz' if const_name == 'Class'
|
131
|
-
Class.new(
|
142
|
+
Class.new(::ArqlModel) do
|
132
143
|
include Arql::Extension
|
133
144
|
self.primary_key = pkey
|
134
145
|
self.table_name = table_name
|
@@ -168,13 +179,26 @@ module Arql
|
|
168
179
|
end
|
169
180
|
|
170
181
|
::ActiveRecord::Relation.class_eval do
|
171
|
-
def t
|
172
|
-
records.t
|
182
|
+
def t(*attrs, **options)
|
183
|
+
records.t(*attrs, **options)
|
173
184
|
end
|
174
185
|
|
175
186
|
def v
|
176
187
|
records.v
|
177
188
|
end
|
189
|
+
|
190
|
+
def a
|
191
|
+
to_a
|
192
|
+
end
|
193
|
+
|
194
|
+
def write_csv(filename, *fields, **options)
|
195
|
+
records.write_csv(filename, *fields, **options)
|
196
|
+
end
|
197
|
+
|
198
|
+
def write_excel(filename, *fields, **options)
|
199
|
+
records.write_excel(filename, *fields, **options)
|
200
|
+
end
|
201
|
+
|
178
202
|
end
|
179
203
|
end
|
180
204
|
end
|
data/lib/arql/ext/array.rb
CHANGED
@@ -13,8 +13,16 @@ class Array
|
|
13
13
|
end.join("\n")
|
14
14
|
end
|
15
15
|
|
16
|
-
def t(*attrs)
|
17
|
-
if attrs.present? && present? && first.is_a?(ActiveRecord::Base)
|
16
|
+
def t(*attrs, **options)
|
17
|
+
if (attrs.present? || options.present? && options[:except]) && present? && first.is_a?(ActiveRecord::Base)
|
18
|
+
if options.present? && options[:except]
|
19
|
+
attrs = first.attribute_names.map(&:to_sym) if attrs.empty?
|
20
|
+
if options[:except].is_a?(Regexp)
|
21
|
+
attrs.reject! { |e| e =~ options[:except] }
|
22
|
+
else
|
23
|
+
attrs -= [options[:except]].flatten
|
24
|
+
end
|
25
|
+
end
|
18
26
|
puts Terminal::Table.new { |t|
|
19
27
|
t << attrs
|
20
28
|
t << :separator
|
@@ -62,4 +70,47 @@ class Array
|
|
62
70
|
end
|
63
71
|
t
|
64
72
|
end
|
73
|
+
|
74
|
+
def write_csv(filename, *fields, **options)
|
75
|
+
generate_csv(filename, **options) do |csv|
|
76
|
+
if size > 0 && first.is_a?(ActiveRecord::Base)
|
77
|
+
if fields.empty?
|
78
|
+
fields = first.attributes.keys
|
79
|
+
else
|
80
|
+
fields = fields.map(&:to_s)
|
81
|
+
end
|
82
|
+
csv << fields
|
83
|
+
end
|
84
|
+
each do |row|
|
85
|
+
if row.is_a?(Array)
|
86
|
+
csv << row.map(&:to_s)
|
87
|
+
else
|
88
|
+
csv << row.slice(fields).values.map(&:to_s)
|
89
|
+
end
|
90
|
+
end
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
def write_excel(filename, *fields, **options)
|
95
|
+
sheet_name = options[:sheet_name] || 'Sheet1'
|
96
|
+
generate_excel(filename) do |workbook|
|
97
|
+
workbook.add_worksheet(name: sheet_name) do |sheet|
|
98
|
+
if size > 0 && first.is_a?(ActiveRecord::Base)
|
99
|
+
if fields.empty?
|
100
|
+
fields = first.attributes.keys
|
101
|
+
else
|
102
|
+
fields = fields.map(&:to_s)
|
103
|
+
end
|
104
|
+
sheet.add_row(fields, types: [:string] * fields.size)
|
105
|
+
end
|
106
|
+
each do |row|
|
107
|
+
if row.is_a?(Array)
|
108
|
+
sheet.add_row(row.map(&:to_s), types: [:string] * row.size)
|
109
|
+
else
|
110
|
+
sheet.add_row(row.slice(fields).values.map(&:to_s), types: [:string] * fields.size)
|
111
|
+
end
|
112
|
+
end
|
113
|
+
end
|
114
|
+
end
|
115
|
+
end
|
65
116
|
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
class Hash
|
2
|
+
def write_excel(filename)
|
3
|
+
generate_excel(filename) do |workbook|
|
4
|
+
each do |sheet_name, sheet_data|
|
5
|
+
workbook.add_worksheet(name: sheet_name) do |sheet|
|
6
|
+
if sheet_data.is_a?(Hash) && sheet_data[:fields].present?
|
7
|
+
fields = sheet_data[:fields].map(&:to_s)
|
8
|
+
else
|
9
|
+
fields = sheet_data[:data].first.attributes.keys
|
10
|
+
end
|
11
|
+
sheet.add_row(fields, types: [:string] * fields.size)
|
12
|
+
sheet_data = sheet_data[:data]
|
13
|
+
end
|
14
|
+
sheet_data.each do |row|
|
15
|
+
if row.is_a?(Array)
|
16
|
+
sheet.add_row(row.map(&:to_s), types: [:string] * row.size)
|
17
|
+
else
|
18
|
+
sheet.add_row(row.slice(fields).values.map(&:to_s), types: [:string] * fields.size)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
data/lib/arql/ext/kernel.rb
CHANGED
@@ -1,4 +1,6 @@
|
|
1
1
|
module Kernel
|
2
|
+
CSV_BOM = "\xef\xbb\xbf"
|
3
|
+
|
2
4
|
def sql(sql)
|
3
5
|
ActiveRecord::Base.connection.exec_query(sql)
|
4
6
|
end
|
@@ -62,4 +64,45 @@ module Kernel
|
|
62
64
|
|
63
65
|
outputs.each { |out| puts out; puts }
|
64
66
|
end
|
67
|
+
|
68
|
+
def generate_csv(filename, **options, &block)
|
69
|
+
opts = {
|
70
|
+
col_sep: "\t",
|
71
|
+
row_sep: "\r\n"
|
72
|
+
}
|
73
|
+
opts.merge!(options.except(:encoding))
|
74
|
+
encoding = options[:encoding] || 'UTF-16LE'
|
75
|
+
File.open(File.expand_path(filename), "w:#{encoding}") do |file|
|
76
|
+
file.write(CSV_BOM)
|
77
|
+
file.write CSV.generate(**opts, &block)
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
def parse_csv(filename, **options)
|
82
|
+
encoding = options[:encoding] || 'UTF-16'
|
83
|
+
opts = {
|
84
|
+
headers: false,
|
85
|
+
col_sep: "\t",
|
86
|
+
row_sep: "\r\n"
|
87
|
+
}
|
88
|
+
opts.merge!(options.except(:encoding))
|
89
|
+
CSV.parse(IO.read(File.expand_path(filename), encoding: encoding, binmode: true).encode('UTF-8'), **opts).to_a
|
90
|
+
end
|
91
|
+
|
92
|
+
def generate_excel(filename)
|
93
|
+
Axlsx::Package.new do |package|
|
94
|
+
yield(package.workbook)
|
95
|
+
package.serialize(filename)
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
99
|
+
def parse_excel(filename)
|
100
|
+
xlsx = Roo::Excelx.new(File.expand_path(filename))
|
101
|
+
xlsx.sheets.each_with_object({}) do |sheet_name, result|
|
102
|
+
begin
|
103
|
+
result[sheet_name] = xlsx.sheet(sheet_name).to_a
|
104
|
+
rescue
|
105
|
+
end
|
106
|
+
end
|
107
|
+
end
|
65
108
|
end
|
data/lib/arql/ext/object.rb
CHANGED
data/lib/arql/repl.rb
CHANGED
@@ -25,7 +25,7 @@ module Arql
|
|
25
25
|
end
|
26
26
|
|
27
27
|
def prompt
|
28
|
-
[proc do |obj, nest_level,
|
28
|
+
[proc do |obj, nest_level, pry_instance|
|
29
29
|
if obj == main_object && nest_level == 0
|
30
30
|
nest_level_prompt = ''
|
31
31
|
else
|
@@ -35,7 +35,7 @@ module Arql
|
|
35
35
|
"(#{obj}:#{nest_level})"
|
36
36
|
end
|
37
37
|
end
|
38
|
-
"%s#{Rainbow('@').green}%s#{nest_level_prompt} %s " % [Rainbow('ARQL').red, Rainbow(App.prompt).yellow, Rainbow('❯').green]
|
38
|
+
"%s#{Rainbow('@').green}%s#{nest_level_prompt} [%d] %s " % [Rainbow('ARQL').red, Rainbow(App.prompt).yellow, pry_instance.input_ring.count, Rainbow('❯').green]
|
39
39
|
end]
|
40
40
|
end
|
41
41
|
end
|
data/lib/arql/version.rb
CHANGED
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
|
+
version: 0.2.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Liu Xiang
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-
|
11
|
+
date: 2020-12-01 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: mysql2
|
@@ -164,6 +164,34 @@ dependencies:
|
|
164
164
|
- - "~>"
|
165
165
|
- !ruby/object:Gem::Version
|
166
166
|
version: 1.5.6
|
167
|
+
- !ruby/object:Gem::Dependency
|
168
|
+
name: roo
|
169
|
+
requirement: !ruby/object:Gem::Requirement
|
170
|
+
requirements:
|
171
|
+
- - "~>"
|
172
|
+
- !ruby/object:Gem::Version
|
173
|
+
version: 2.8.3
|
174
|
+
type: :runtime
|
175
|
+
prerelease: false
|
176
|
+
version_requirements: !ruby/object:Gem::Requirement
|
177
|
+
requirements:
|
178
|
+
- - "~>"
|
179
|
+
- !ruby/object:Gem::Version
|
180
|
+
version: 2.8.3
|
181
|
+
- !ruby/object:Gem::Dependency
|
182
|
+
name: caxlsx
|
183
|
+
requirement: !ruby/object:Gem::Requirement
|
184
|
+
requirements:
|
185
|
+
- - "~>"
|
186
|
+
- !ruby/object:Gem::Version
|
187
|
+
version: 3.0.2
|
188
|
+
type: :runtime
|
189
|
+
prerelease: false
|
190
|
+
version_requirements: !ruby/object:Gem::Requirement
|
191
|
+
requirements:
|
192
|
+
- - "~>"
|
193
|
+
- !ruby/object:Gem::Version
|
194
|
+
version: 3.0.2
|
167
195
|
description: Use ActiveRecord and Pry as your favorite SQL query editor.
|
168
196
|
email:
|
169
197
|
- liuxiang921@gmail.com
|
@@ -199,6 +227,7 @@ files:
|
|
199
227
|
- lib/arql/definition.rb
|
200
228
|
- lib/arql/ext.rb
|
201
229
|
- lib/arql/ext/array.rb
|
230
|
+
- lib/arql/ext/hash.rb
|
202
231
|
- lib/arql/ext/kernel.rb
|
203
232
|
- lib/arql/ext/object.rb
|
204
233
|
- lib/arql/ext/string.rb
|