Module: RailsDevelopmentBoost::DependenciesPatch

Defined in:
lib/rails_development_boost/dependencies_patch.rb

Defined Under Namespace

Modules: LoadablePatch

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.apply!Object



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

def self.apply!
  # retain the original method in case the application overwrites it on its modules/klasses
  Module.send :alias_method, :_mod_name, :name
  
  patch = self
  ActiveSupport::Dependencies.module_eval do
    alias_method :local_const_defined?, :uninherited_const_defined? unless method_defined?(:local_const_defined?) # pre 4da45060 compatibility
    remove_possible_method :remove_unloadable_constants!
    remove_possible_method :clear
    include patch
    alias_method_chain :load_file, 'constant_tracking'
    alias_method_chain :remove_constant, 'handling_of_connections'
    extend patch
  end
  
  ActiveSupport::Dependencies::Loadable.module_eval do
    include LoadablePatch
    alias_method_chain :require_dependency, 'constant_tracking'
  end

  InstrumentationPatch.apply! if @do_instrument
  
  ActiveSupport::Dependencies.handle_already_autoloaded_constants!
end

.debug!Object



36
37
38
39
40
41
42
# File 'lib/rails_development_boost/dependencies_patch.rb', line 36

def self.debug!
  if ActiveSupport::Dependencies < DependenciesPatch
    InstrumentationPatch.apply!
  else
    @do_instrument = true
  end
end

Instance Method Details

#add_explicit_dependency(parent, child) ⇒ Object



126
127
128
# File 'lib/rails_development_boost/dependencies_patch.rb', line 126

def add_explicit_dependency(parent, child)
  (explicit_dependencies[parent._mod_name] ||= []) << child._mod_name
end

#associate_constants_to_file(constants, file_path) ⇒ Object



96
97
98
99
100
101
102
# File 'lib/rails_development_boost/dependencies_patch.rb', line 96

def associate_constants_to_file(constants, file_path)
  # freezing strings before using them as Hash keys is slightly more memory efficient
  constants.map!(&:freeze)
  file_path.freeze
  
  loaded_file_for(file_path).add_constants(constants)
end

#clearObject

Overridden.



70
71
# File 'lib/rails_development_boost/dependencies_patch.rb', line 70

def clear
end

#handle_already_autoloaded_constants!Object

we might be late to the party and other gems/plugins might have already triggered autoloading of some constants



130
131
132
133
134
# File 'lib/rails_development_boost/dependencies_patch.rb', line 130

def handle_already_autoloaded_constants! # we might be late to the party and other gems/plugins might have already triggered autoloading of some constants
  loaded.each do |require_path|
    associate_constants_to_file(autoloaded_constants, "#{require_path}.rb") # slightly heavy-handed..
  end
end

#load_file_with_constant_tracking(path, *args, &block) ⇒ Object

Augmented ‘load_file’.



74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/rails_development_boost/dependencies_patch.rb', line 74

def load_file_with_constant_tracking(path, *args, &block)
  result = now_loading(path) { load_file_without_constant_tracking(path, *args, &block) }
  
  unless load_once_path?(path)
    new_constants = autoloaded_constants - file_map.values.map(&:constants).flatten
  
    # Associate newly loaded constants to the file just loaded
    associate_constants_to_file(new_constants, path)
  end

  result
end

#loaded_file_for(file_path) ⇒ Object



104
105
106
# File 'lib/rails_development_boost/dependencies_patch.rb', line 104

def loaded_file_for(file_path)
  file_map[file_path] ||= LoadedFile.new(file_path)
end

#now_loading(path) ⇒ Object



87
88
89
90
91
92
93
94
# File 'lib/rails_development_boost/dependencies_patch.rb', line 87

def now_loading(path)
  @currently_loading, old_currently_loading = path, @currently_loading
  yield
rescue Exception => e
  error_loading_file(@currently_loading, e)
ensure
  @currently_loading = old_currently_loading
end

#remove_constant_with_handling_of_connections(const_name) ⇒ Object

Augmented ‘remove_constant’.



109
110
111
112
113
114
115
# File 'lib/rails_development_boost/dependencies_patch.rb', line 109

def remove_constant_with_handling_of_connections(const_name)
  fetch_module_cache do
    prevent_further_removal_of(const_name) do
      unprotected_remove_constant(const_name)
    end
  end
end

#remove_explicitely_unloadable_constants!Object



65
66
67
# File 'lib/rails_development_boost/dependencies_patch.rb', line 65

def remove_explicitely_unloadable_constants!
  explicitly_unloadable_constants.each { |const| remove_constant(const) }
end

#required_dependency(file_name) ⇒ Object



117
118
119
120
121
122
123
124
# File 'lib/rails_development_boost/dependencies_patch.rb', line 117

def required_dependency(file_name)
  # Rails uses require_dependency for loading helpers, we are however dealing with the helper problem elsewhere, so we can skip them
  if @currently_loading && @currently_loading !~ /_controller(?:\.rb)?\Z/ && file_name !~ /_helper(?:\.rb)?\Z/
    if full_path = ActiveSupport::Dependencies.search_for_file(file_name)
      loaded_file_for(@currently_loading).associate_with(loaded_file_for(full_path))
    end
  end
end

#unload_modified_files!Object



58
59
60
61
62
63
# File 'lib/rails_development_boost/dependencies_patch.rb', line 58

def unload_modified_files!
  log_call
  file_map.values.each do |file|
    unload_modified_file(file) if file.changed?
  end
end