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