Class: Module

Inherits:
Object
  • Object
show all
Defined in:
lib/dependencies.rb

Overview

:nodoc:

Instance Method Summary collapse

Instance Method Details

#base_const_missingObject



3
# File 'lib/dependencies.rb', line 3

alias :base_const_missing :const_missing

#const_missing(class_id) ⇒ Object

If a non-existant class is specified, define it as a subclass of ActiveRecord::Base here This idea was inspired by the Magic Models project: magicmodels.rubyforge.org/

I added the modifications of on-demand generation and simple ActiveRecord::Base subclassing

If a table can be found that matches the class_id, the class is defined in memory. If no matching table can be found, a NameError is thrown



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/dependencies.rb', line 12

def const_missing(class_id)
  begin
    base_const_missing(class_id)
  rescue NameError
    class_def = "      class \#{class_id} < ActiveRecord::Base   \n      end\n    end_class_def\n    begin\n      if ActiveRecord::Base.table_exists?(class_id)\n        eval(class_def, TOPLEVEL_BINDING)     \n        ActiveRecord::Base.logger.info(\"DRYSQL >> GENERATED CLASS: \#{class_id} < ActiveRecord::Base\")     \n      else \n        ActiveRecord::Base.logger.error(\"DRYSQL >> No matching table could be found for class: \#{class_id}\")\n        raise NameError     \n      end  \n    rescue ActiveRecord::ConnectionNotEstablished\n   throw \"Unable to search for matching table for class \#{class_id}: no database connection has been established\"\n    end    \n  end\n  const_get(class_id)\nend\n"