Module: ActiveSupport::Dependencies::ModuleConstMissing

Defined in:
activesupport/lib/active_support/dependencies.rb

Overview

Module includes this module

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.append_features(base) ⇒ Object



145
146
147
148
149
150
151
152
153
# File 'activesupport/lib/active_support/dependencies.rb', line 145

def self.append_features(base)
  base.class_eval do
    # Emulate #exclude via an ivar
    return if defined?(@_const_missing) && @_const_missing
    @_const_missing = instance_method(:const_missing)
    remove_method(:const_missing)
  end
  super
end

.exclude_from(base) ⇒ Object



155
156
157
158
159
160
# File 'activesupport/lib/active_support/dependencies.rb', line 155

def self.exclude_from(base)
  base.class_eval do
    define_method :const_missing, @_const_missing
    @_const_missing = nil
  end
end

Instance Method Details

#const_missing(const_name, nesting = nil) ⇒ Object

Use const_missing to autoload associations so we don't have to require_association when using single-table inheritance.



164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'activesupport/lib/active_support/dependencies.rb', line 164

def const_missing(const_name, nesting = nil)
  klass_name = name.presence || "Object"

  unless nesting
    # We'll assume that the nesting of Foo::Bar is ["Foo::Bar", "Foo"]
    # even though it might not be, such as in the case of
    # class Foo::Bar; Baz; end
    nesting = []
    klass_name.to_s.scan(/::|$/) { nesting.unshift $` }
  end

  # If there are multiple levels of nesting to search under, the top
  # level is the one we want to report as the lookup fail.
  error = nil

  nesting.each do |namespace|
    begin
      return Dependencies.load_missing_constant Inflector.constantize(namespace), const_name
    rescue NoMethodError then raise
    rescue NameError => e
      error ||= e
    end
  end

  # Raise the first error for this set. If this const_missing came from an
  # earlier const_missing, this will result in the real error bubbling
  # all the way up
  raise error
end

#unloadable(const_desc = self) ⇒ Object



194
195
196
# File 'activesupport/lib/active_support/dependencies.rb', line 194

def unloadable(const_desc = self)
  super(const_desc)
end