Class: PryHelper::Definition

Inherits:
Object
  • Object
show all
Defined in:
lib/pry-helper/definition.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeDefinition

Returns a new instance of Definition.



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
150
151
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
# File 'lib/pry-helper/definition.rb', line 104

def initialize
  @@models = []

  ActiveRecord::Base.connection.tap do |conn|
    defined_models = ::ApplicationRecord.descendants
    tables = conn.tables
    if conn.adapter_name == 'Mysql2'
      conn.define_singleton_method(:dump) do |filename, no_create_db=false|
        PryHelper::Mysqldump.new.dump_database(filename, no_create_db)
      end
      require 'pry-helper/ext/active_record/connection_adapters/abstract_mysql_adapter'
      comments = conn.table_comment_of_tables(tables)
      primary_keys = conn.primary_keys_of_tables(tables)
    else
      comments = tables.map { |t| [t, conn.table_comment(t)] }.to_h
      primary_keys = tables.map { |t| [t, conn.primary_keys(t)] }.to_h
    end

    ApplicationRecord.include(PryHelper::Extension)

    tables.each do |table_name|
      table_comment = comments[table_name]
      primary_keys[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'
          if model_class = defined_models.find { |m| m.table_name == table_name }
            model_class.tap do |clazz|
              model_class.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, model_class
                    abbr_const = abbr
                    break
                  end
                end

                @@models << {
                  model: model_class,
                  abbr: abbr_const,
                  table: table_name,
                  comment: table_comment
                }
              end
            end
          else
            Class.new(::ApplicationRecord) do
              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
            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
end

Class Method Details

.modelsObject



99
100
101
# File 'lib/pry-helper/definition.rb', line 99

def models
  @@models ||= []
end