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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 7404f5bdf1c9e60f767884ae022d5176a120dad6cd18b2c9597ed76a88a2c89c
4
- data.tar.gz: 7dbfe7c5c06c22a0279a1a3005a637a68974973d9bf1962a3d2fad92bd3be912
3
+ metadata.gz: bcf50149ad9ded8d667ea9afc40dea78d432525c8c62fe4f33d0d164e02acb81
4
+ data.tar.gz: a6fd83949c0c87f1d4891fae1a8fb0fa65e13a48f9e8ddb327b02f499bdbc73a
5
5
  SHA512:
6
- metadata.gz: fd8ea1e246a0127800744706f132eefe5079e2f27d76e424896d40cf54a7f64827b0903f0cfeb3e329b57d862a158d11610660c7c93c61fb10d5859b9889ae6c
7
- data.tar.gz: 9e9b1ff3104f01d5c4a2685eae6e50f5f1f5e1a14ed8af1991676a1beaeb9c8542cb793b3d1dbee7834e8b0824d8651749d730f4ea3ef5ac921292af60ab640e
6
+ metadata.gz: 9f73ccc2e3669a6f10fa7555737d09fadec7e0c0457c5b8819dae65055aa6a045153cce2137ccf9a9742f509d9f5c8567fd617e9f3dda7e2136935cf3686a2f3
7
+ data.tar.gz: ca3d6d547e8267066d72a6e67b09012b42b2e4defa36669c8b304707dc434cd6194a84a201c0fac8d3ed3f749ca017f2b4eaba6ce1f0a3813cfab34140827bc5
@@ -1,15 +1,17 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- arql (0.1.33)
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.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
 
@@ -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(ActiveRecord::Base) do
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(ActiveRecord::Base) do
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
@@ -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
@@ -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
@@ -1,3 +1,3 @@
1
1
  module Arql
2
- VERSION = "0.1.33"
2
+ VERSION = "0.2.4"
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.33
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-08-27 00:00:00.000000000 Z
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