Class: Module

Inherits:
Object show all
Defined in:
lib/active_support/dependencies.rb,
lib/active_support/core_ext/module/loading.rb,
lib/active_support/core_ext/module/inclusion.rb,
lib/active_support/core_ext/module/delegation.rb,
lib/active_support/core_ext/module/introspection.rb,
lib/active_support/core_ext/module/attribute_accessors.rb

Overview

Extends the module object with module and instance accessors for class attributes, just like the native attr* accessors for instance attributes.

Instance Method Summary collapse

Instance Method Details

#as_load_pathObject



2
3
4
5
6
7
8
9
10
11
12
# File 'lib/active_support/core_ext/module/loading.rb', line 2

def as_load_path
  if self == Object || self == Kernel
    ''
  elsif is_a? Class
    parent == self ? '' : parent.as_load_path
  else
    name.split('::').collect do |word|
      word.underscore
    end * '/'
  end
end

#const_missing(class_id) ⇒ Object

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



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/active_support/dependencies.rb', line 94

def const_missing(class_id)
  file_name = class_id.to_s.demodulize.underscore
  file_path = as_load_path.empty? ? file_name : "#{as_load_path}/#{file_name}"
  begin
    require_dependency(file_path)
    brief_name = self == Object ? '' : "#{name}::"
    raise NameError.new("uninitialized constant #{brief_name}#{class_id}") unless const_defined?(class_id)
    return const_get(class_id)
  rescue MissingSourceFile => e
    # Re-raise the error if it does not concern the file we were trying to load.
    raise unless e.is_missing? file_path
    
    # Look for a directory in the load path that we ought to load.
    if $LOAD_PATH.any? { |base| File.directory? "#{base}/#{file_path}" }
      mod = Module.new
      const_set class_id, mod # Create the new module
      return mod
    end
    
    # Attempt to access the name from the parent, unless we don't have a valid
    # parent, or the constant is already defined in the parent. If the latter
    # is the case, then we are being queried via self::class_id, and we should
    # avoid returning the constant from the parent if possible.
    if parent && parent != self && ! parents.any? { |p| p.const_defined?(class_id) }
      suppress(NameError) do
        return parent.send(:const_missing, class_id)
      end
    end
    
    raise NameError.new("uninitialized constant #{class_id}").copy_blame!(e)
  end
end

#delegate(*methods) ⇒ Object



2
3
4
5
6
7
8
9
10
11
12
13
14
15
# File 'lib/active_support/core_ext/module/delegation.rb', line 2

def delegate(*methods)
  options = methods.pop
  unless options.is_a?(Hash) && to = options[:to]
    raise ArgumentError, "Delegation needs a target. Supply an options hash with a :to key"
  end

  methods.each do |method|
    module_eval(<<-EOS, "(__DELEGATION__)", 1)
      def #{method}(*args, &block)
        #{to}.__send__(#{method.inspect}, *args, &block)
      end
    EOS
  end
end

#included_in_classesObject



2
3
4
5
6
7
8
9
10
# File 'lib/active_support/core_ext/module/inclusion.rb', line 2

def included_in_classes
  classes = []
  ObjectSpace.each_object(Class) { |k| classes << k if k.included_modules.include?(self) }

  classes.reverse.inject([]) do |unique_classes, klass| 
    unique_classes << klass unless unique_classes.collect { |k| k.to_s }.include?(klass.to_s)
    unique_classes
  end
end

#mattr_accessor(*syms) ⇒ Object



40
41
42
43
# File 'lib/active_support/core_ext/module/attribute_accessors.rb', line 40

def mattr_accessor(*syms)
  mattr_reader(*syms)
  mattr_writer(*syms)
end

#mattr_reader(*syms) ⇒ Object

:nodoc:



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/active_support/core_ext/module/attribute_accessors.rb', line 4

def mattr_reader(*syms)
  syms.each do |sym|
    class_eval(<<-EOS, __FILE__, __LINE__)
      unless defined? @@#{sym}
        @@#{sym} = nil
      end
      
      def self.#{sym}
        @@#{sym}
      end

      def #{sym}
        @@#{sym}
      end
    EOS
  end
end

#mattr_writer(*syms) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/active_support/core_ext/module/attribute_accessors.rb', line 22

def mattr_writer(*syms)
  syms.each do |sym|
    class_eval(<<-EOS, __FILE__, __LINE__)
      unless defined? @@#{sym}
        @@#{sym} = nil
      end
      
      def self.#{sym}=(obj)
        @@#{sym} = obj
      end

      def #{sym}=(obj)
        @@#{sym} = obj
      end
    EOS
  end
end

#parentObject

Return the module which contains this one; if this is a root module, such as ::MyModule, then Object is returned.



4
5
6
7
# File 'lib/active_support/core_ext/module/introspection.rb', line 4

def parent
  parent_name = name.split('::')[0..-2] * '::'
  parent_name.empty? ? Object : parent_name.constantize
end

#parentsObject

Return all the parents of this module, ordered from nested outwards. The receiver is not contained within the result.



11
12
13
14
15
16
17
18
19
20
# File 'lib/active_support/core_ext/module/introspection.rb', line 11

def parents
  parents = []
  parts = name.split('::')[0..-2]
  until parts.empty?
    parents << (parts * '::').constantize
    parts.pop
  end
  parents << Object unless parents.include? Object
  parents
end

#rails_original_const_missingObject

Rename the original handler so we can chain it to the new one



90
# File 'lib/active_support/dependencies.rb', line 90

alias :rails_original_const_missing :const_missing