arql 0.4.15 → 0.4.16

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: d444bc5ecc11b4c15db01b52b413341e85525d71d82f24e994315418b728a989
4
- data.tar.gz: 1e69014e37aafa3974c3aae3be52c37c7f3c27db2c7b59d169caa8b4de5ce6d7
3
+ metadata.gz: 2e298f643cc8cba53568449f235fed8f490cb93b35f6238477f3031bac708cf4
4
+ data.tar.gz: 5a575e23be87acd3031bcc870d6ed538c651b75f3bbdc7ffd258730bd1008ea7
5
5
  SHA512:
6
- metadata.gz: 0dd50276ddff1a65d3d01bc7eb8ec05dbdd59d441eb8fe2555ca3ccaf2b951650ace6b7bf5908f4eccb2f38a0dec7000130f41c63f62df74e4a8ef4c6504897d
7
- data.tar.gz: ada7756d3e50fbff30e8b6581b1eceaebff35fdacb7bf76bf504348410fd7152398515799de35f355e91c3d3cb87d87646a401901da3003bb4b16c2ead88bfd5
6
+ metadata.gz: c0de2dc777e08f079a7a52ab1423a5139b42bbf12fb7b315fe3ce4f44d525ab699629fdedde4bbd661fe98409692c36ab3800c232272e61d0b49a8b1ff2d1ced
7
+ data.tar.gz: c1e1aca6d3ead4305fd70dee0fbd5cb00e68ed87c39db856b2d6bf5080c4b7d3b2adc1fa20e5519e6b7bad295487e1fcd0604310ee6f7b3403cc218db179646f
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- arql (0.4.15)
4
+ arql (0.4.16)
5
5
  activerecord (>= 6.1.5, < 7.1.0)
6
6
  activesupport (>= 6.1.5, < 7.1.0)
7
7
  caxlsx (~> 3.3.0)
data/README-zh_CN.org CHANGED
@@ -199,6 +199,40 @@
199
199
  建的常量别名(Arql 默认也会按照一定的规则自动为生成的模型类创建别名,如果这里指定了别名,将会使用用户提供的值作
200
200
  为别名)
201
201
  12. =ignored_columns=: 一个列名的数组,在生成模型时将忽略这些列,默认为空数组
202
+ 13. =rename_columns=: 一个 Hash,用于将数据库列名映射为新的名称。当列名与 ActiveRecord 内置方法冲突时(例如 =model_name=、
203
+ =valid=、=connection=、=type= 等),会导致 =ActiveRecord::DangerousAttributeError= 错误。使用此配置项可以重命名这些冲突列。
204
+ 重命名后,原始列名将被隐藏,只能通过新名称访问。新名称同样可用于查询(例如 =where(new_name: 'value')=
205
+ 会自动翻译为 =WHERE old_name = 'value'=)。arql 会在启动时校验新名称,确保其不与已有列名、ActiveRecord 保留方法名
206
+ 或 arql 内置方法名冲突。如果新名称是 ActiveRecord 保留方法名,arql 将在启动时报错。
207
+
208
+ =rename_columns= 配置项示例:
209
+
210
+ #+BEGIN_SRC yaml
211
+ development:
212
+ adapter: mysql2
213
+ host: localhost
214
+ database: myapp
215
+ rename_columns:
216
+ model_name: record_model_name
217
+ valid: is_valid
218
+ type: biz_type
219
+ #+END_SRC
220
+
221
+ 使用以上配置后:
222
+
223
+ #+begin_example
224
+ ARQL@dev(main) [1] ❯ Attachment.last
225
+ => #<I::Attachment:0xabc id: 1, record_model_name: "SomeModel", is_valid: true, biz_type: "image">
226
+
227
+ ARQL@dev(main) [2] ❯ Attachment.where(record_model_name: "SomeModel").to_sql
228
+ => "SELECT `attachments`.* FROM `attachments` WHERE `attachments`.`model_name` = 'SomeModel'"
229
+
230
+ ARQL@dev(main) [3] ❯ Attachment.last.is_valid
231
+ => true
232
+
233
+ ARQL@dev(main) [4] ❯ Attachment.last.model_name
234
+ => NoMethodError (原始名称已被隐藏)
235
+ #+end_example
202
236
 
203
237
  =model_names= 配置项的例子:
204
238
 
data/README.org CHANGED
@@ -226,6 +226,41 @@
226
226
  the generated model class according to certain rules, and if an alias is specified here, the user-provided value
227
227
  will be used as the alias)
228
228
  12. =ignored_columns= : An array of column names to be ignored when generating the model, default is an empty array
229
+ 13. =rename_columns= : A hash mapping original column names to new names. When a column name conflicts with an ActiveRecord
230
+ built-in method (e.g., =model_name=, =valid=, =connection=, =type=), it causes =ActiveRecord::DangerousAttributeError=.
231
+ Use this option to rename conflicting columns. The original column name will be hidden and only the new name will be
232
+ accessible. The new name also works in queries (e.g., =where(new_name: 'value')= translates to =WHERE old_name = 'value'=).
233
+ Arql validates that the new name does not conflict with existing columns, ActiveRecord reserved methods, or arql
234
+ built-in methods. If the new name is an ActiveRecord reserved method, arql will raise an error at startup.
235
+
236
+ Example of =rename_columns= configuration:
237
+
238
+ #+BEGIN_SRC yaml
239
+ development:
240
+ adapter: mysql2
241
+ host: localhost
242
+ database: myapp
243
+ rename_columns:
244
+ model_name: record_model_name
245
+ valid: is_valid
246
+ type: biz_type
247
+ #+END_SRC
248
+
249
+ With the above configuration:
250
+
251
+ #+begin_example
252
+ ARQL@dev(main) [1] ❯ Attachment.last
253
+ => #<I::Attachment:0xabc id: 1, record_model_name: "SomeModel", is_valid: true, biz_type: "image">
254
+
255
+ ARQL@dev(main) [2] ❯ Attachment.where(record_model_name: "SomeModel").to_sql
256
+ => "SELECT `attachments`.* FROM `attachments` WHERE `attachments`.`model_name` = 'SomeModel'"
257
+
258
+ ARQL@dev(main) [3] ❯ Attachment.last.is_valid
259
+ => true
260
+
261
+ ARQL@dev(main) [4] ❯ Attachment.last.model_name
262
+ => NoMethodError (original name is hidden)
263
+ #+end_example
229
264
 
230
265
  =model_names= Examples of configuration items:
231
266
 
@@ -239,14 +239,28 @@ module Arql
239
239
  end
240
240
  self.attribute_aliases = rename_columns.transform_keys(&:to_sym).invert
241
241
 
242
- original_inspect = instance_method(:inspect)
243
- rename_columns_for_inspect = rename_columns.dup
244
- define_method(:inspect) do
245
- result = original_inspect.bind(self).call
246
- rename_columns_for_inspect.each do |old_name, new_name|
247
- result = result.sub(/\b#{Regexp.escape(old_name.to_s)}(?=:)/, new_name.to_s)
242
+ rename_map = rename_columns.transform_keys(&:to_s)
243
+ define_method(:pretty_print) do |pp|
244
+ pp.object_address_group(self) do
245
+ if defined?(@attributes) && @attributes
246
+ attr_names = self.class.attribute_names.select { |name| _has_attribute?(name) }
247
+ pp.seplist(attr_names, proc { pp.text ',' }) do |attr_name|
248
+ display_name = rename_map.fetch(attr_name, attr_name)
249
+ pp.breakable ' '
250
+ pp.group(1) do
251
+ pp.text display_name
252
+ pp.text ':'
253
+ pp.breakable
254
+ value = _read_attribute(attr_name)
255
+ value = inspection_filter.filter_param(attr_name, value) unless value.nil?
256
+ pp.pp value
257
+ end
258
+ end
259
+ else
260
+ pp.breakable ' '
261
+ pp.text 'not initialized'
262
+ end
248
263
  end
249
- result
250
264
  end
251
265
  end
252
266
  end
data/lib/arql/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Arql
2
- VERSION = "0.4.15"
2
+ VERSION = "0.4.16"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: arql
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.15
4
+ version: 0.4.16
5
5
  platform: ruby
6
6
  authors:
7
7
  - Liu Xiang