Class: Arql::Definition

Inherits:
Object show all
Defined in:
lib/arql/definition.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Definition

Returns a new instance of Definition.



152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
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
# File 'lib/arql/definition.rb', line 152

def initialize(options)
  @@options = options
  @@models = []
  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,
                             Comment: idx.comment
                           }
                         end.t
                       end
                     end)
    conn.tables.each do |table_name|
      table_comment = conn.table_comment(table_name)
      conn.primary_key(table_name).tap do |pkey|
        table_name.camelize.tap do |const_name|
          const_name = 'Modul' if const_name == 'Module'
          const_name = 'Clazz' if const_name == 'Class'
          Class.new(::ArqlModel) do
            include Arql::Extension
            if pkey.is_a?(Array)
              self.primary_keys = pkey
            else
              self.primary_key = pkey
            end
            self.table_name = table_name
            self.inheritance_column = nil
            self.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: table_comment
                }
              end
            end
          end
        end
      end
    end
  end
end

Class Method Details

.modelsObject



82
83
84
# File 'lib/arql/definition.rb', line 82

def models
  @@models ||= []
end

.redefineObject



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/arql/definition.rb', line 86

def redefine
  options = @@options
  @@models.each do |model|
    Object.send :remove_const, model[:model].name.to_sym if model[:model]
    Object.send :remove_const, model[:abbr].to_sym if model[:abbr]
  end
  @@models = []
  ActiveRecord::Base.connection.tap do |conn|
    conn.tables.each do |table_name|
      table_comment = conn.table_comment(table_name)
      conn.primary_key(table_name).tap do |pkey|
        table_name.camelize.tap do |const_name|
          const_name = 'Modul' if const_name == 'Module'
          const_name = 'Clazz' if const_name == 'Class'
          Class.new(::ArqlModel) do
            include Arql::Extension
            if pkey.is_a?(Array)
              self.primary_keys = pkey
            else
              self.primary_key = pkey
            end
            self.table_name = table_name
            self.inheritance_column = nil
            self.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: table_comment
                }
              end
            end
          end
        end
      end
    end
  end

  App.instance&.load_initializer!
end