arql 0.1.32 → 0.2.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 48ce3b7b50ccdb8ab338b73e1895b73cd7ebec18ef779c18931603353e709d12
4
- data.tar.gz: 3a9bf2b11cdf4be30ab58590aedb81ea2944f6341ca26fd2ab584c9a490cb512
3
+ metadata.gz: bf94862baf6c8e5ff36589e84a022e892b4b88664ecc300c51d313a541e982fd
4
+ data.tar.gz: baa0a4b30e5efeb4083347c18f2863faa764052505e46442e45dd9af519612b0
5
5
  SHA512:
6
- metadata.gz: 1cb190b2e90d18b08ccc98878b5db934ebb1e008fc4aba6a850fd4e384e917d02f14837700a06aca8bc5bdc7799a8ea6ffd5c737bb38260304f60166db3a63ce
7
- data.tar.gz: 3b9c6e813b91904cf39bd044c1ac86925802f52d6f0a4e7a7d3f10f2309db812e01cecb0476555bbcb751df73f640c941fe3e307a96c05f9101d854659e53a70
6
+ metadata.gz: aa3a4aba5288c64c7969efe368713e9f2a64ff9dfde3f41d47b9d8f3ec2f64fbfbe5479d6dc1c95281d82f68497616f44f78e84b635e312b6c6e40528871e6cf
7
+ data.tar.gz: cae343a1411c59e683dcc99ee309d1ca91100c2c8cf22cbff37ffe04cc02e338d38562e0623a752c5f0803926fba1b0f5a2332074ec3f98083dadd0b8439ab0e
@@ -1,15 +1,17 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- arql (0.1.32)
4
+ arql (0.2.3)
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.1)
21
- activesupport (= 6.0.3.1)
22
- activerecord (6.0.3.1)
23
- activemodel (= 6.0.3.1)
24
- activesupport (= 6.0.3.1)
25
- activesupport (6.0.3.1)
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
- minitest (5.14.1)
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.6)
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)
@@ -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
@@ -1,4 +1,7 @@
1
1
  require 'table_print'
2
+ require 'roo'
3
+ require 'caxlsx'
4
+ require 'csv'
2
5
  require "arql/version"
3
6
  require 'arql/id'
4
7
  require 'arql/multi_io'
@@ -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
@@ -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
 
@@ -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
 
@@ -168,13 +178,26 @@ module Arql
168
178
  end
169
179
 
170
180
  ::ActiveRecord::Relation.class_eval do
171
- def t
172
- records.t
181
+ def t(*attrs, **options)
182
+ records.t(*attrs, **options)
173
183
  end
174
184
 
175
185
  def v
176
186
  records.v
177
187
  end
188
+
189
+ def a
190
+ to_a
191
+ end
192
+
193
+ def write_csv(filename, *fields, **options)
194
+ records.write_csv(filename, *fields, **options)
195
+ end
196
+
197
+ def write_excel(filename, *fields, **options)
198
+ records.write_excel(filename, *fields, **options)
199
+ end
200
+
178
201
  end
179
202
  end
180
203
  end
@@ -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
@@ -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
@@ -15,4 +15,8 @@ class Object
15
15
  def jjp
16
16
  puts jj
17
17
  end
18
+
19
+ def a
20
+ [self]
21
+ end
18
22
  end
@@ -7,7 +7,7 @@ module Arql
7
7
  class Repl
8
8
  def initialize
9
9
  Pry.config.prompt = Pry::Prompt.new("", "", prompt)
10
- main_object.pry
10
+ Pry.start
11
11
  end
12
12
 
13
13
  def main_object
@@ -25,13 +25,17 @@ 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
32
- nest_level_prompt = "(#{obj}:#{nest_level})"
32
+ nest_level_prompt = if nest_level.zero?
33
+ "(#{obj})"
34
+ else
35
+ "(#{obj}:#{nest_level})"
36
+ end
33
37
  end
34
- "%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]
35
39
  end]
36
40
  end
37
41
  end
@@ -1,3 +1,3 @@
1
1
  module Arql
2
- VERSION = "0.1.32"
2
+ VERSION = "0.2.3"
3
3
  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.1.32
4
+ version: 0.2.3
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-08-26 00:00:00.000000000 Z
11
+ date: 2020-09-24 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