Class Module
In: vendor/rails/activesupport/lib/active_support/deprecation.rb
vendor/rails/activesupport/lib/active_support/core_ext/module.rb
vendor/rails/activesupport/lib/active_support/core_ext/module/loading.rb
vendor/rails/activesupport/lib/active_support/core_ext/module/synchronization.rb
vendor/rails/activesupport/lib/active_support/core_ext/module/attribute_accessors.rb
vendor/rails/activesupport/lib/active_support/core_ext/module/attr_accessor_with_default.rb
vendor/rails/activesupport/lib/active_support/core_ext/module/attr_internal.rb
vendor/rails/activesupport/lib/active_support/core_ext/module/delegation.rb
vendor/rails/activesupport/lib/active_support/core_ext/module/inclusion.rb
vendor/rails/activesupport/lib/active_support/vendor/builder-2.1.2/blankslate.rb
Parent: Object

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.

Methods

Included Modules

ActiveSupport::Deprecation::ClassMethods ActiveSupport::CoreExtensions::Module

External Aliases

append_features -> blankslate_original_append_features

Public Instance methods

[Source]

     # 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

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"

[Source]

    # 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

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 }

[Source]

    # 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)                        # def age=(value)\nclass << self; attr_reader :\#{sym} end  #   class << self; attr_reader :age end\n@\#{sym} = value                         #   @age = value\nend                                       # end\n", __FILE__, __LINE__)
25:   end
attr_internal(*attrs)

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

[Source]

    # 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

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

[Source]

   # 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

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

[Source]

    # 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

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 them as a symbols:

  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

Delegates can optionally be prefixed using the :prefix option. If the value is true, the delegate methods are prefixed with the name of the object being delegated to.

  Person = Struct.new(:name, :address)

  class Invoice < Struct.new(:client)
    delegate :name, :address, :to => :client, :prefix => true
  end

  john_doe = Person.new("John Doe", "Vimmersvej 13")
  invoice = Invoice.new(john_doe)
  invoice.client_name    # => "John Doe"
  invoice.client_address # => "Vimmersvej 13"

It is also possible to supply a custom prefix.

  class Invoice < Struct.new(:client)
    delegate :name, :address, :to => :client, :prefix => :customer
  end

  invoice = Invoice.new(john_doe)
  invoice.customer_name    # => "John Doe"
  invoice.customer_address # => "Vimmersvej 13"

If the object to which you delegate can be nil, you may want to use the :allow_nil option. In that case, it returns nil instead of raising a NoMethodError exception:

 class Foo
   attr_accessor :bar
   def initialize(bar = nil)
     @bar = bar
   end
   delegate :zoo, :to => :bar
 end

 Foo.new.zoo   # raises NoMethodError exception (you called nil.zoo)

 class Foo
   attr_accessor :bar
   def initialize(bar = nil)
     @bar = bar
   end
   delegate :zoo, :to => :bar, :allow_nil => true
 end

 Foo.new.zoo   # returns nil

[Source]

     # File vendor/rails/activesupport/lib/active_support/core_ext/module/delegation.rb, line 99
 99:   def delegate(*methods)
100:     options = methods.pop
101:     unless options.is_a?(Hash) && to = options[:to]
102:       raise ArgumentError, "Delegation needs a target. Supply an options hash with a :to key as the last argument (e.g. delegate :hello, :to => :greeter)."
103:     end
104: 
105:     if options[:prefix] == true && options[:to].to_s =~ /^[^a-z_]/
106:       raise ArgumentError, "Can only automatically set the delegation prefix when delegating to a method."
107:     end
108: 
109:     prefix = options[:prefix] && "#{options[:prefix] == true ? to : options[:prefix]}_"
110: 
111:     file, line = caller.first.split(':', 2)
112:     line = line.to_i
113: 
114:     methods.each do |method|
115:       on_nil =
116:         if options[:allow_nil]
117:           'return'
118:         else
119:           %(raise "#{prefix}#{method} delegated to #{to}.#{method}, but #{to} is nil: \#{self.inspect}")
120:         end
121: 
122:       module_eval("def \#{prefix}\#{method}(*args, &block)               # def customer_name(*args, &block)\n\#{to}.__send__(\#{method.inspect}, *args, &block)  #   client.__send__(:name, *args, &block)\nrescue NoMethodError                                # rescue NoMethodError\nif \#{to}.nil?                                     #   if client.nil?\n\#{on_nil}\nelse                                              #   else\nraise                                           #     raise\nend                                               #   end\nend                                                 # end\n", file, line)
123:     end
124:   end

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]

[Source]

    # 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

[Source]

    # File vendor/rails/activesupport/lib/active_support/core_ext/module/attribute_accessors.rb, line 58
58:   def mattr_accessor(*syms)
59:     mattr_reader(*syms)
60:     mattr_writer(*syms)
61:   end

[Source]

    # File vendor/rails/activesupport/lib/active_support/core_ext/module/attribute_accessors.rb, line 16
16:   def mattr_reader(*syms)
17:     syms.each do |sym|
18:       next if sym.is_a?(Hash)
19:       class_eval("unless defined? @@\#{sym}  # unless defined? @@pagination_options\n@@\#{sym} = nil          #   @@pagination_options = nil\nend                       # end\n#\ndef self.\#{sym}           # def self.pagination_options\n@@\#{sym}                #   @@pagination_options\nend                       # end\n#\ndef \#{sym}                # def pagination_options\n@@\#{sym}                #   @@pagination_options\nend                       # end\n", __FILE__, __LINE__)
20:     end
21:   end

[Source]

    # File vendor/rails/activesupport/lib/active_support/core_ext/module/attribute_accessors.rb, line 36
36:   def mattr_writer(*syms)
37:     options = syms.extract_options!
38:     syms.each do |sym|
39:       class_eval("unless defined? @@\#{sym}                       # unless defined? @@pagination_options\n@@\#{sym} = nil                               #   @@pagination_options = nil\nend                                            # end\n#\ndef self.\#{sym}=(obj)                          # def self.pagination_options=(obj)\n@@\#{sym} = obj                               #   @@pagination_options = obj\nend                                            # end\n#\n\#{\"                                            #\ndef \#{sym}=(obj)                               # def pagination_options=(obj)\n@@\#{sym} = obj                               #   @@pagination_options = obj\nend                                            # end\n\" unless options[:instance_writer] == false }  # # instance writer above is generated unless options[:instance_writer] == false\n", __FILE__, __LINE__)
40:     end
41:   end

Synchronize access around a method, delegating synchronization to a particular mutex. A mutex (either a Mutex, or any object that responds to synchronize and yields to a block) must be provided as a final :with option. The :with option should be a symbol or string, and can represent a method, constant, or instance or class variable. Example:

  class SharedCache
    @@lock = Mutex.new
    def expire
      ...
    end
    synchronize :expire, :with => :@@lock
  end

[Source]

    # File vendor/rails/activesupport/lib/active_support/core_ext/module/synchronization.rb, line 15
15:   def synchronize(*methods)
16:     options = methods.extract_options!
17:     unless options.is_a?(Hash) && with = options[:with]
18:       raise ArgumentError, "Synchronization needs a mutex. Supply an options hash with a :with key as the last argument (e.g. synchronize :hello, :with => :@mutex)."
19:     end
20: 
21:     methods.each do |method|
22:       aliased_method, punctuation = method.to_s.sub(/([?!=])$/, ''), $1
23: 
24:       if method_defined?("#{aliased_method}_without_synchronization#{punctuation}")
25:         raise ArgumentError, "#{method} is already synchronized. Double synchronization is not currently supported."
26:       end
27: 
28:       module_eval("def \#{aliased_method}_with_synchronization\#{punctuation}(*args, &block)     # def expire_with_synchronization(*args, &block)\n\#{with}.synchronize do                                                    #   @@lock.synchronize do\n\#{aliased_method}_without_synchronization\#{punctuation}(*args, &block)  #     expire_without_synchronization(*args, &block)\nend                                                                       #   end\nend                                                                         # end\n", __FILE__, __LINE__)
29: 
30:       alias_method_chain method, :synchronization
31:     end
32:   end

[Validate]