Changeset 1332

Show
Ignore:
Timestamp:
01/28/08 14:38:52 (9 months ago)
Author:
iv..@gweezlebur.com
Message:

better (non-magic) model/controller loading for dependencies -- closes #466 -- Thanks to all who worked on this one: coryodaniel, oliver, hassox

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/lib/merb/boot_loader.rb

    r1260 r1332  
    139139       
    140140      def load_application 
    141         $LOAD_PATH.unshift( File.join(Merb.root , '/app/models') ) 
    142         $LOAD_PATH.unshift( File.join(Merb.root , '/app/controllers') ) 
     141     
     142        #Magic Class Loading => Not failing on missing parent due to alphabetical loading 
     143        # Does a reverse alphaebetical search if classes failed in first pass 
     144        # Will continue to reverse the list of failures until the size does change between 
     145        # two passes 
     146 
     147        orphaned_paths = [] 
     148        $LOAD_PATH.unshift( File.join(Merb.root,'/app/models') ) 
     149        $LOAD_PATH.unshift( File.join(Merb.root,'/app/controllers') ) 
    143150        $LOAD_PATH.unshift( File.join(Merb.root , '/lib') ) 
    144151        Merb.load_paths.each do |glob| 
    145           Dir[Merb.root + glob].each { |m| require m } 
    146         end 
     152          Dir[Merb.root + glob].each do |m|  
     153            begin 
     154              require m  
     155            rescue NameError 
     156              orphaned_paths.unshift(m) 
     157            end 
     158          end 
     159        end 
     160         
     161        load_classes_with_requirements(orphaned_paths) 
     162         
    147163        load_action_arguments 
    148164        load_controller_template_path_cache 
     
    152168        (@after_app_blocks || []).each { |b| b.call } 
    153169      end 
     170       
     171       
     172      def load_classes_with_requirements(orphaned_paths) 
     173         
     174        #Make the list unique 
     175        orphaned_paths.uniq! 
     176         
     177        while orphaned_paths.size > 0 
     178          #Take the size for comparison later 
     179          size_at_start = orphaned_paths.size 
     180 
     181          fail_list = [] #List of failures 
     182           
     183          # Try to load each path again, this time the order is reversed 
     184          (orphaned_paths).each do |m| 
     185            # Remove the path from the list 
     186            orphaned_paths.delete(m) 
     187            begin 
     188              require m 
     189            rescue NameError 
     190              # Add it back on if it failed to load due to NameError 
     191              fail_list.push(m) 
     192            end 
     193          end 
     194 
     195          orphaned_paths.concat(fail_list) 
     196 
     197          # Stop processing if everything loaded (size == 0) or if the size didn't change  
     198          # (ie something couldn't be loaded) 
     199          break if(orphaned_paths.size == size_at_start || orphaned_paths.size == 0) 
     200        end 
     201         
     202        return orphaned_paths 
     203      end 
     204       
    154205       
    155206      def load_libraries