Class: Module

Also, modules included into Object need to be scanned and have their instance methods removed from blank slate. In theory, modules included into Kernel would have to be removed as well, but a "feature" of Ruby prevents late includes into modules from being exposed in the first place.

Aliases

Old nameNew name
const_missing rails_original_const_missing
append_features blankslate_original_append_features

Public Instance Methods


alias_attribute (new_name, old_name)

Allows you to make aliases for attributes, which includes getter, setter, and query methods.

Example:

  class Content < ActiveRecord::Base
    # has a title attribute
  end

  class Email < Content
    alias_attribute :subject, :title
  end

  e = Email.find(1)
  e.title    # => "Superstars"
  e.subject  # => "Superstars"
  e.subject? # => true
  e.subject = "Megastars"
  e.title    # => "Megastars"
    # File vendor/rails/activesupport/lib/active_support/core_ext/module/aliasing.rb, line 63
63:   def alias_attribute(new_name, old_name)
64:     module_eval "def \#{new_name}; self.\#{old_name}; end\ndef \#{new_name}?; self.\#{old_name}?; end\ndef \#{new_name}=(v); self.\#{old_name} = v; end\n", __FILE__, __LINE__+1
65:   end

alias_method_chain (target, feature) {|aliased_target, punctuation| ...}

Encapsulates the common pattern of:

  alias_method :foo_without_feature, :foo
  alias_method :foo, :foo_with_feature

With this, you simply do:

  alias_method_chain :foo, :feature

And both aliases are set up for you.

Query and bang methods (foo?, foo!) keep the same punctuation:

  alias_method_chain :foo?, :feature

is equivalent to

  alias_method :foo_without_feature?, :foo?
  alias_method :foo?, :foo_with_feature?

so you can safely chain foo, foo?, and foo! with the same feature.

    # File vendor/rails/activesupport/lib/active_support/core_ext/module/aliasing.rb, line 23
23:   def alias_method_chain(target, feature)
24:     # Strip out punctuation on predicates or bang methods since
25:     # e.g. target?_without_feature is not a valid method name.
26:     aliased_target, punctuation = target.to_s.sub(/([?!=])$/, ''), $1
27:     yield(aliased_target, punctuation) if block_given?
28:     
29:     with_method, without_method = "#{aliased_target}_with_#{feature}#{punctuation}", "#{aliased_target}_without_#{feature}#{punctuation}"
30:     
31:     alias_method without_method, target
32:     alias_method target, with_method
33:     
34:     case
35:       when public_method_defined?(without_method)
36:         public target
37:       when protected_method_defined?(without_method)
38:         protected target
39:       when private_method_defined?(without_method)
40:         private target
41:     end
42:   end

append_features (mod)

     # File vendor/rails/activesupport/lib/active_support/vendor/builder-2.1.2/blankslate.rb, line 105
105:   def append_features(mod)
106:     result = blankslate_original_append_features(mod)
107:     return result if mod != Object
108:     instance_methods.each do |name|
109:       BlankSlate.hide(name)
110:     end
111:     result
112:   end

as_load_path ()

Returns String#underscore applied to the module name minus trailing classes.

  ActiveRecord.as_load_path               # => "active_record"
  ActiveRecord::Associations.as_load_path # => "active_record/associations"
  ActiveRecord::Base.as_load_path         # => "active_record" (Base is a class)

The Kernel module gives an empty string by definition.

  Kernel.as_load_path # => ""
  Math.as_load_path   # => "math"
    # File vendor/rails/activesupport/lib/active_support/core_ext/module/loading.rb, line 12
12:   def as_load_path
13:     if self == Object || self == Kernel
14:       ''
15:     elsif is_a? Class
16:       parent == self ? '' : parent.as_load_path
17:     else
18:       name.split('::').collect do |word|
19:         word.underscore
20:       end * '/'
21:     end
22:   end

attr_accessor_with_default (sym, default = nil, &block)

Declare an attribute accessor with an initial default return value.

To give attribute :age the initial value 25:

  class Person
    attr_accessor_with_default :age, 25
  end

  some_person.age
  => 25
  some_person.age = 26
  some_person.age
  => 26

To give attribute :element_name a dynamic default value, evaluated in scope of self:

  attr_accessor_with_default(:element_name) { name.underscore }
    # File vendor/rails/activesupport/lib/active_support/core_ext/module/attr_accessor_with_default.rb, line 21
21:   def attr_accessor_with_default(sym, default = nil, &block)
22:     raise 'Default value or block required' unless !default.nil? || block
23:     define_method(sym, block_given? ? block : Proc.new { default })
24:     module_eval("def \#{sym}=(value)\nclass << self; attr_reader :\#{sym} end\n@\#{sym} = value\nend\n", __FILE__, __LINE__)
25:   end


attr_internal_accessor (*attrs)

Declare an attribute reader and writer backed by an internally-named instance variable.

    # File vendor/rails/activesupport/lib/active_support/core_ext/module/attr_internal.rb, line 18
18:   def attr_internal_accessor(*attrs)
19:     attr_internal_reader(*attrs)
20:     attr_internal_writer(*attrs)
21:   end

attr_internal_reader (*attrs)

Declare an attribute reader backed by an internally-named instance variable.

   # File vendor/rails/activesupport/lib/active_support/core_ext/module/attr_internal.rb, line 3
3:   def attr_internal_reader(*attrs)
4:     attrs.each do |attr|
5:       module_eval "def #{attr}() #{attr_internal_ivar_name(attr)} end"
6:     end
7:   end

attr_internal_writer (*attrs)

Declare an attribute writer backed by an internally-named instance variable.

    # File vendor/rails/activesupport/lib/active_support/core_ext/module/attr_internal.rb, line 10
10:   def attr_internal_writer(*attrs)
11:     attrs.each do |attr|
12:       module_eval "def #{attr}=(v) #{attr_internal_ivar_name(attr)} = v end"
13:     end
14:   end

const_missing (class_id)

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

     # File vendor/rails/activesupport/lib/active_support/dependencies.rb, line 452
452:   def const_missing(class_id)
453:     Dependencies.load_missing_constant self, class_id
454:   end

delegate (*methods)

Provides a delegate class method to easily expose contained objects’ methods as your own. Pass one or more methods (specified as symbols or strings) and the name of the target object as the final :to option (also a symbol or string). At least one method and the :to option are required.

Delegation is particularly useful with Active Record associations:

  class Greeter < ActiveRecord::Base
    def hello()   "hello"   end
    def goodbye() "goodbye" end
  end

  class Foo < ActiveRecord::Base
    belongs_to :greeter
    delegate :hello, :to => :greeter
  end

  Foo.new.hello   # => "hello"
  Foo.new.goodbye # => NoMethodError: undefined method `goodbye' for #<Foo:0x1af30c>

Multiple delegates to the same target are allowed:

  class Foo < ActiveRecord::Base
    belongs_to :greeter
    delegate :hello, :goodbye, :to => :greeter
  end

  Foo.new.goodbye # => "goodbye"

Methods can be delegated to instance variables, class variables, or constants by providing the variable as a symbol:

  class Foo
    CONSTANT_ARRAY = [0,1,2,3]
    @@class_array  = [4,5,6,7]

    def initialize
      @instance_array = [8,9,10,11]
    end
    delegate :sum, :to => :CONSTANT_ARRAY
    delegate :min, :to => :@@class_array
    delegate :max, :to => :@instance_array
  end

  Foo.new.sum # => 6
  Foo.new.min # => 4
  Foo.new.max # => 11
    # File vendor/rails/activesupport/lib/active_support/core_ext/module/delegation.rb, line 48
48:   def delegate(*methods)
49:     options = methods.pop
50:     unless options.is_a?(Hash) && to = options[:to]
51:       raise ArgumentError, "Delegation needs a target. Supply an options hash with a :to key as the last argument (e.g. delegate :hello, :to => :greeter)."
52:     end
53: 
54:     methods.each do |method|
55:       module_eval("def \#{method}(*args, &block)\n\#{to}.__send__(\#{method.inspect}, *args, &block)\nend\n", "(__DELEGATION__)", 1)
56:     end
57:   end

include_all_modules_from (parent_module)

    # File vendor/rails/railties/lib/console_with_helpers.rb, line 2
 2:   def include_all_modules_from(parent_module)
 3:     parent_module.constants.each do |const|
 4:       mod = parent_module.const_get(const)
 5:       if mod.class == Module
 6:         send(:include, mod)
 7:         include_all_modules_from(mod)
 8:       end
 9:     end
10:   end

included_in_classes ()

Returns the classes in the current ObjectSpace where this module has been mixed in according to Module#included_modules.

  module M
  end

  module N
    include M
  end

  class C
    include M
  end

  class D < C
  end

  p M.included_in_classes # => [C, D]
    # File vendor/rails/activesupport/lib/active_support/core_ext/module/inclusion.rb, line 21
21:   def included_in_classes
22:     classes = []
23:     ObjectSpace.each_object(Class) { |k| classes << k if k.included_modules.include?(self) }
24: 
25:     classes.reverse.inject([]) do |unique_classes, klass| 
26:       unique_classes << klass unless unique_classes.collect { |k| k.to_s }.include?(klass.to_s)
27:       unique_classes
28:     end
29:   end

local_constant_names ()

Returns the names of the constants defined locally rather than the constants themselves. See local_constants.

    # File vendor/rails/activesupport/lib/active_support/core_ext/module/introspection.rb, line 72
72:   def local_constant_names
73:     local_constants.map(&:to_s)
74:   end

local_constants (

    # File vendor/rails/activesupport/lib/active_support/core_ext/module/introspection.rb, line 65
65:     def local_constants #nodoc
66:       constants(false)
67:     end

local_constants ()

Returns the constants that have been defined locally by this object and not in an ancestor. This method is exact if running under Ruby 1.9. In previous versions it may miss some constants if their definition in some ancestor is identical to their definition in the receiver.

    # File vendor/rails/activesupport/lib/active_support/core_ext/module/introspection.rb, line 52
52:     def local_constants
53:       inherited = {}
54: 
55:       ancestors.each do |anc|
56:         next if anc == self
57:         anc.constants.each { |const| inherited[const] = anc.const_get(const) }
58:       end
59: 
60:       constants.select do |const|
61:         !inherited.key?(const) || inherited[const].object_id != const_get(const).object_id
62:       end
63:     end

mattr_accessor (*syms)

    # File vendor/rails/activesupport/lib/active_support/core_ext/module/attribute_accessors.rb, line 46
46:   def mattr_accessor(*syms)
47:     mattr_reader(*syms)
48:     mattr_writer(*syms)
49:   end

mattr_reader (*syms)

   # File vendor/rails/activesupport/lib/active_support/core_ext/module/attribute_accessors.rb, line 4
4:   def mattr_reader(*syms)
5:     syms.each do |sym|
6:       next if sym.is_a?(Hash)
7:       class_eval("unless defined? @@\#{sym}\n@@\#{sym} = nil\nend\n\ndef self.\#{sym}\n@@\#{sym}\nend\n\ndef \#{sym}\n@@\#{sym}\nend\n", __FILE__, __LINE__)
8:     end
9:   end

mattr_writer (*syms)

    # File vendor/rails/activesupport/lib/active_support/core_ext/module/attribute_accessors.rb, line 24
24:   def mattr_writer(*syms)
25:     options = syms.extract_options!
26:     syms.each do |sym|
27:       class_eval("unless defined? @@\#{sym}\n@@\#{sym} = nil\nend\n\ndef self.\#{sym}=(obj)\n@@\#{sym} = obj\nend\n\n\#{\"\ndef \#{sym}=(obj)\n@@\#{sym} = obj\nend\n\" unless options[:instance_writer] == false }\n", __FILE__, __LINE__)
28:     end
29:   end

parent ()

Returns the module which contains this one according to its name.

  module M
    module N
    end
  end
  X = M::N

  p M::N.parent # => M
  p X.parent    # => M

The parent of top-level and anonymous modules is Object.

  p M.parent          # => Object
  p Module.new.parent # => Object
    # File vendor/rails/activesupport/lib/active_support/core_ext/module/introspection.rb, line 18
18:   def parent
19:     parent_name = name.split('::')[0..-2] * '::'
20:     parent_name.empty? ? Object : parent_name.constantize
21:   end

parents ()

Returns all the parents of this module according to its name, ordered from nested outwards. The receiver is not contained within the result.

  module M
    module N
    end
  end
  X = M::N

  p M.parents    # => [Object]
  p M::N.parents # => [M, Object]
  p X.parents    # => [M, Object]
    # File vendor/rails/activesupport/lib/active_support/core_ext/module/introspection.rb, line 36
36:   def parents
37:     parents = []
38:     parts = name.split('::')[0..-2]
39:     until parts.empty?
40:       parents << (parts * '::').constantize
41:       parts.pop
42:     end
43:     parents << Object unless parents.include? Object
44:     parents
45:   end

unloadable (const_desc = self)

     # File vendor/rails/activesupport/lib/active_support/dependencies.rb, line 456
456:   def unloadable(const_desc = self)
457:     super(const_desc)
458:   end

Private Instance Methods


attr_internal_ivar_name (attr)

    # File vendor/rails/activesupport/lib/active_support/core_ext/module/attr_internal.rb, line 29
29:     def attr_internal_ivar_name(attr)
30:       attr_internal_naming_format % attr
31:     end