Sha256: b3d3ff5545d4b91a1fb5cd2326430fd72330c29eb3d18062b17b8f8864317b4d

Contents?: true

Size: 1.6 KB

Versions: 4

Compression:

Stored size: 1.6 KB

Contents

require File.dirname(__FILE__) + '/module_attribute_accessors'

module Dependencies
  extend self
  
  @@loaded = [ ]
  mattr_accessor :loaded

  @@mechanism = :load
  mattr_accessor :mechanism
  
  def depend_on(file_name, swallow_load_errors = false)
    if !loaded.include?(file_name)
      loaded << file_name

      begin
        require_or_load(file_name)
      rescue LoadError
        raise unless swallow_load_errors
      end
    end
  end

  def associate_with(file_name)
    depend_on(file_name, true)
  end
  
  def clear
    self.loaded = [ ]
  end
  
  private
    def require_or_load(file_name)
      mechanism == :load ? silence_warnings { load("#{file_name}.rb") } : require(file_name)
    end
end

Object.send(:define_method, :require_dependency)  { |file_name| Dependencies.depend_on(file_name) } unless Object.respond_to?(:require_dependency)
Object.send(:define_method, :require_association) { |file_name| Dependencies.associate_with(file_name) } unless Object.respond_to?(:require_association)

class Object #:nodoc:
  class << self
    # Use const_missing to autoload associations so we don't have to
    # require_association when using single-table inheritance.
    unless respond_to?(:pre_dependency_const_missing)
      alias_method :pre_dependency_const_missing, :const_missing

      def const_missing(class_id)
        begin
          require_dependency(Inflector.underscore(Inflector.demodulize(class_id.to_s)))
          return Object.const_get(class_id) if Object.const_defined?(class_id)
        rescue LoadError
          pre_dependency_const_missing(class_id)
        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 2 rubygems

Version Path
actionpack-1.3.1 lib/action_controller/support/dependencies.rb
actionpack-1.3.0 lib/action_controller/support/dependencies.rb
activerecord-1.5.1 lib/active_record/support/dependencies.rb
activerecord-1.5.0 lib/active_record/support/dependencies.rb