Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Changeset 9143

Show
Ignore:
Timestamp:
03/30/08 05:02:25 (5 months ago)
Author:
bitsweat
Message:

Don't need to explicitly pass the same params to super. Remove trailing whitespace.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/activesupport/lib/active_support/dependencies.rb

    r8482 r9143  
    2222  mattr_accessor :mechanism 
    2323  self.mechanism = :load 
    24    
     24 
    2525  # The set of directories from which we may automatically load files. Files 
    2626  # under these directories will be reloaded on each request in development mode, 
     
    2828  mattr_accessor :load_paths 
    2929  self.load_paths = [] 
    30    
     30 
    3131  # The set of directories from which automatically loaded constants are loaded 
    3232  # only once. All directories in this set must also be present in +load_paths+. 
    3333  mattr_accessor :load_once_paths 
    3434  self.load_once_paths = [] 
    35    
     35 
    3636  # An array of qualified constant names that have been loaded. Adding a name to 
    3737  # this array will cause it to be unloaded the next time Dependencies are cleared. 
    3838  mattr_accessor :autoloaded_constants 
    3939  self.autoloaded_constants = [] 
    40    
     40 
    4141  # An array of constant names that need to be unloaded on every request. Used 
    4242  # to allow arbitrary constants to be marked for unloading. 
    4343  mattr_accessor :explicitly_unloadable_constants 
    4444  self.explicitly_unloadable_constants = [] 
    45    
     45 
    4646  # Set to true to enable logging of const_missing and file loads 
    4747  mattr_accessor :log_activity 
    4848  self.log_activity = false 
    49    
     49 
    5050  # An internal stack used to record which constants are loaded by any block. 
    5151  mattr_accessor :constant_watch_stack 
    5252  self.constant_watch_stack = [] 
    53    
     53 
    5454  def load? 
    5555    mechanism == :load 
     
    8282    # infinite loop with mutual dependencies. 
    8383    loaded << expanded 
    84      
     84 
    8585    if load? 
    8686      log "loading #{file_name}" 
     
    9090        load_args = ["#{file_name}.rb"] 
    9191        load_args << const_path unless const_path.nil? 
    92          
     92 
    9393        if !warnings_on_first_load or history.include?(expanded) 
    9494          result = load_file(*load_args) 
     
    109109    return result 
    110110  end 
    111    
     111 
    112112  # Is the provided constant path defined? 
    113113  def qualified_const_defined?(path) 
    114114    raise NameError, "#{path.inspect} is not a valid constant name!" unless 
    115115      /^(::)?([A-Z]\w*)(::[A-Z]\w*)*$/ =~ path 
    116      
     116 
    117117    names = path.to_s.split('::') 
    118118    names.shift if names.first.empty? 
    119      
     119 
    120120    # We can't use defined? because it will invoke const_missing for the parent 
    121121    # of the name we are checking. 
     
    126126    return true 
    127127  end 
    128    
     128 
    129129  # Given +path+, a filesystem path to a ruby file, return an array of constant 
    130130  # paths which would cause Dependencies to attempt to load this file. 
    131   #  
     131  # 
    132132  def loadable_constants_for_path(path, bases = load_paths) 
    133133    path = $1 if path =~ /\A(.*)\.rb\Z/ 
    134134    expanded_path = File.expand_path(path) 
    135      
     135 
    136136    bases.collect do |root| 
    137137      expanded_root = File.expand_path(root) 
    138138      next unless %r{\A#{Regexp.escape(expanded_root)}(/|\\)} =~ expanded_path 
    139        
     139 
    140140      nesting = expanded_path[(expanded_root.size)..-1] 
    141141      nesting = nesting[1..-1] if nesting && nesting[0] == ?/ 
    142142      next if nesting.blank? 
    143        
     143 
    144144      [ 
    145145        nesting.camelize, 
     
    149149    end.flatten.compact.uniq 
    150150  end 
    151    
     151 
    152152  # Search for a file in load_paths matching the provided suffix. 
    153153  def search_for_file(path_suffix) 
     
    159159    nil # Gee, I sure wish we had first_match ;-) 
    160160  end 
    161    
     161 
    162162  # Does the provided path_suffix correspond to an autoloadable module? 
    163   # Instead of returning a boolean, the autoload base for this module is returned.  
     163  # Instead of returning a boolean, the autoload base for this module is returned. 
    164164  def autoloadable_module?(path_suffix) 
    165165    load_paths.each do |load_path| 
     
    168168    nil 
    169169  end 
    170    
     170 
    171171  def load_once_path?(path) 
    172172    load_once_paths.any? { |base| path.starts_with? base } 
    173173  end 
    174    
     174 
    175175  # Attempt to autoload the provided module name by searching for a directory 
    176176  # matching the expect path suffix. If found, the module is created and assigned 
     
    185185    return mod 
    186186  end 
    187    
     187 
    188188  # Load the file at the provided path. +const_paths+ is a set of qualified 
    189189  # constant names. When loading the file, Dependencies will watch for the 
    190190  # addition of these constants. Each that is defined will be marked as 
    191191  # autoloaded, and will be removed when Dependencies.clear is next called. 
    192   #  
     192  # 
    193193  # If the second parameter is left off, then Dependencies will construct a set 
    194194  # of names that the file at +path+ may define. See 
     
    198198    const_paths = [const_paths].compact unless const_paths.is_a? Array 
    199199    parent_paths = const_paths.collect { |const_path| /(.*)::[^:]+\Z/ =~ const_path ? $1 : :Object } 
    200      
     200 
    201201    result = nil 
    202202    newly_defined_paths = new_constants_in(*parent_paths) do 
    203203      result = load_without_new_constant_marking path 
    204204    end 
    205      
     205 
    206206    autoloaded_constants.concat newly_defined_paths unless load_once_path?(path) 
    207207    autoloaded_constants.uniq! 
     
    209209    return result 
    210210  end 
    211    
     211 
    212212  # Return the constant path for the provided parent and constant name. 
    213213  def qualified_name_for(mod, name) 
     
    215215    (%w(Object Kernel).include? mod_name) ? name.to_s : "#{mod_name}::#{name}" 
    216216  end 
    217    
     217 
    218218  # Load the constant named +const_name+ which is missing from +from_mod+. If 
    219219  # it is not possible to load the constant into from_mod, try its parent module 
     
    239239 
    240240    raise ArgumentError, "#{from_mod} is not missing constant #{const_name}!" if from_mod.const_defined?(const_name) 
    241      
     241 
    242242    qualified_name = qualified_name_for from_mod, const_name 
    243243    path_suffix = qualified_name.underscore 
    244244    name_error = NameError.new("uninitialized constant #{qualified_name}") 
    245      
     245 
    246246    file_path = search_for_file(path_suffix) 
    247247    if file_path && ! loaded.include?(File.expand_path(file_path)) # We found a matching file to load 
     
    267267    end 
    268268  end 
    269    
     269 
    270270  # Remove the constants that have been autoloaded, and those that have been 
    271271  # marked for unloading. 
     
    275275    explicitly_unloadable_constants.each { |const| remove_constant const } 
    276276  end 
    277    
     277 
    278278  # Determine if the given constant has been automatically loaded. 
    279279  def autoloaded?(desc) 
     
    284284    return autoloaded_constants.include?(name) 
    285285  end 
    286    
     286 
    287287  # Will the provided constant descriptor be unloaded? 
    288288  def will_unload?(const_desc) 
     
    290290      explicitly_unloadable_constants.include?(to_constant_name(const_desc)) 
    291291  end 
    292    
     292 
    293293  # Mark the provided constant name for unloading. This constant will be 
    294294  # unloaded on each request, not just the next one. 
     
    302302    end 
    303303  end 
    304    
     304 
    305305  # Run the provided block and detect the new constants that were loaded during 
    306306  # its execution. Constants may only be regarded as 'new' once -- so if the 
    307307  # block calls +new_constants_in+ again, then the constants defined within the 
    308308  # inner call will not be reported in this one. 
    309   #  
     309  # 
    310310  # If the provided block does not run to completion, and instead raises an 
    311311  # exception, any new constants are regarded as being only partially defined 
     
    313313  def new_constants_in(*descs) 
    314314    log_call(*descs) 
    315      
     315 
    316316    # Build the watch frames. Each frame is a tuple of 
    317317    #   [module_name_as_string, constants_defined_elsewhere] 
     
    322322      elsif desc.is_a?(String) || desc.is_a?(Symbol) 
    323323        mod_name = desc.to_s 
    324          
     324 
    325325        # Handle the case where the module has yet to be defined. 
    326326        initial_constants = if qualified_const_defined?(mod_name) 
     
    332332        raise Argument, "#{desc.inspect} does not describe a module!" 
    333333      end 
    334        
     334 
    335335      [mod_name, initial_constants] 
    336336    end 
    337      
     337 
    338338    constant_watch_stack.concat watch_frames 
    339      
     339 
    340340    aborting = true 
    341341    begin 
     
    347347        # Module still doesn't exist? Treat it as if it has no constants. 
    348348        next [] unless qualified_const_defined?(mod_name) 
    349          
     349 
    350350        mod = mod_name.constantize 
    351351        next [] unless mod.is_a? Module 
    352352        new_constants = mod.local_constant_names - prior_constants 
    353          
     353 
    354354        # Make sure no other frames takes credit for these constants. 
    355355        constant_watch_stack.each do |frame_name, constants| 
    356356          constants.concat new_constants if frame_name == mod_name 
    357357        end 
    358          
     358 
    359359        new_constants.collect do |suffix| 
    360360          mod_name == "Object" ? suffix : "#{mod_name}::#{suffix}" 
    361361        end 
    362362      end.flatten 
    363        
     363 
    364364      log "New constants: #{new_constants * ', '}" 
    365        
     365 
    366366      if aborting 
    367367        log "Error during loading, removing partially loaded constants " 
     
    370370      end 
    371371    end 
    372      
     372 
    373373    return new_constants 
    374374  ensure 
     
    381381    end 
    382382  end 
    383    
     383 
    384384  class LoadingModule #:nodoc: 
    385385    # Old style environment.rb referenced this method directly.  Please note, it doesn't 
     
    426426    arg_str = args.collect(&:inspect) * ', ' 
    427427    /in `([a-z_\?\!]+)'/ =~ caller(1).first 
    428     selector = $1 || '<unknown>'  
     428    selector = $1 || '<unknown>' 
    429429    log "called #{selector}(#{arg_str})" 
    430430  end 
    431    
     431 
    432432  def log(msg) 
    433433    if defined?(RAILS_DEFAULT_LOGGER) && RAILS_DEFAULT_LOGGER && log_activity 
     
    435435    end 
    436436  end 
    437    
     437 
    438438end 
    439439 
     
    447447  # Rename the original handler so we can chain it to the new one 
    448448  alias :rails_original_const_missing :const_missing 
    449    
     449 
    450450  # Use const_missing to autoload associations so we don't have to 
    451451  # require_association when using single-table inheritance. 
     
    453453    Dependencies.load_missing_constant self, class_id 
    454454  end 
    455    
     455 
    456456  def unloadable(const_desc = self) 
    457457    super(const_desc) 
    458458  end 
    459    
     459 
    460460end 
    461461 
     
    483483 
    484484class Object 
    485    
    486485  alias_method :load_without_new_constant_marking, :load 
    487    
     486 
    488487  def load(file, *extras) #:nodoc: 
    489     Dependencies.new_constants_in(Object) { super(file, *extras)
     488    Dependencies.new_constants_in(Object) { super
    490489  rescue Exception => exception  # errors from loading file 
    491490    exception.blame_file! file 
     
    494493 
    495494  def require(file, *extras) #:nodoc: 
    496     Dependencies.new_constants_in(Object) { super(file, *extras)
     495    Dependencies.new_constants_in(Object) { super
    497496  rescue Exception => exception  # errors from required file 
    498497    exception.blame_file! file 
     
    502501  # Mark the given constant as unloadable. Unloadable constants are removed each 
    503502  # time dependencies are cleared. 
    504   #  
     503  # 
    505504  # Note that marking a constant for unloading need only be done once. Setup 
    506505  # or init scripts may list each unloadable constant that may need unloading; 
    507506  # each constant will be removed for every subsequent clear, as opposed to for 
    508507  # the first clear. 
    509   #  
     508  # 
    510509  # The provided constant descriptor may be a (non-anonymous) module or class, 
    511510  # or a qualified constant name as a string or symbol. 
    512   #  
     511  # 
    513512  # Returns true if the constant was not previously marked for unloading, false 
    514513  # otherwise. 
     
    516515    Dependencies.mark_for_unload const_desc 
    517516  end 
    518  
    519517end 
    520518