Class Module
In: lib/active_support/core_ext/module/attr_accessor_with_default.rb
lib/active_support/core_ext/module/attr_internal.rb
lib/active_support/core_ext/module/attribute_accessors.rb
lib/active_support/core_ext/module/delegation.rb
lib/active_support/core_ext/module/inclusion.rb
lib/active_support/core_ext/module/loading.rb
lib/active_support/core_ext/module/synchronization.rb
lib/active_support/core_ext/module.rb
lib/active_support/deprecation.rb
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::CoreExtensions::Module ActiveSupport::Deprecation::ClassMethods

External Aliases

append_features -> blankslate_original_append_features

Public Instance methods

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"

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 }
attr_internal(*attrs)

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

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

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

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"

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]

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

[Validate]