Module

ActiveSupport::CoreExtensions::Module

Inheritance

Various extensions for the Ruby core Module class.

Methods

Instance

Visibility Signature
public alias_attribute (new_name, old_name)
public alias_method_chain (target, feature) {|aliased_target, punctuation| ...}
public local_constant_names ()
public local_constants ()
public model_name ()
public parent ()
public parent_name ()
public parents ()

Instance Method Detail

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"

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.

local_constant_names()

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

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.

model_name()

Returns an ActiveSupport::ModelName object for module. It can be used to retrieve all kinds of naming-related information.

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

parent_name()

Returns the name of the module containing this one.

  p M::N.parent_name # => "M"

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]