177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
|
# File 'lib/arql/definition.rb', line 177
def initialize(options)
@@options = options
@@models = []
@@classify_method = if @@options[:singularized_table_names]
:camelize
else
:classify
end
ActiveRecord::Base.connection.tap do |conn|
Object.const_set('ArqlModel', Class.new(ActiveRecord::Base) do
include ::Arql::Concerns::TableDataDefinition
self.abstract_class = true
define_singleton_method(:indexes) do
conn.indexes(table_name).map do |idx|
{
Table: idx.table,
Name: idx.name,
Columns: idx.columns.join(', '),
Unique: idx.unique,
: idx.
}
end.t
end
end)
tables = conn.tables
if conn.adapter_name == 'Mysql2'
require 'arql/ext/active_record/connection_adapters/abstract_mysql_adapter'
= conn.(tables)
primary_keys = conn.primary_keys_of_tables(tables)
else
= tables.map { |t| [t, conn.(t)] }.to_h
primary_keys = tables.map { |t| [t, conn.primary_keys(t)] }.to_h
end
tables.each do |table_name|
= [table_name]
primary_keys[table_name].tap do |pkey|
table_name.send(@@classify_method).tap do |const_name|
const_name = 'Modul' if const_name == 'Module'
const_name = 'Clazz' if const_name == 'Class'
if const_name !~ /^[A-Z][A-Za-z0-9_]*$/
puts "Invalid class name: #{const_name}, skipped."
next
end
Class.new(::ArqlModel) do
include Arql::Extension
if pkey.is_a?(Array) && pkey.size > 1
self.primary_keys = pkey
else
self.primary_key = pkey&.first
end
self.table_name = table_name
self.inheritance_column = nil
ActiveRecord.default_timezone = :local
if options[:created_at].present?
define_singleton_method :timestamp_attributes_for_create do
options[:created_at]
end
end
if options[:updated_at].present?
define_singleton_method :timestamp_attributes_for_update do
options[:updated_at]
end
end
end.tap do |clazz|
Object.const_set(const_name, clazz).tap do |const|
const_name.gsub(/[a-z]*/, '').tap do |bare_abbr|
abbr_const = nil
9.times do |idx|
abbr = idx.zero? ? bare_abbr : "#{bare_abbr}#{idx+1}"
unless Object.const_defined?(abbr)
Object.const_set abbr, const
abbr_const = abbr
break
end
end
@@models << {
model: const,
abbr: abbr_const,
table: table_name,
comment:
}
end
end
end
end
end
end
end
end
|