Class: Module

Inherits:
Object show all
Includes:
ActiveSupport::Deprecation::ClassMethods
Defined in:
lib/active_support/dependencies.rb,
lib/active_support/deprecation.rb,
lib/active_support/core_ext/module/loading.rb,
lib/active_support/core_ext/module/aliasing.rb,
lib/active_support/core_ext/module/inclusion.rb,
lib/active_support/core_ext/module/delegation.rb,
lib/active_support/core_ext/module/attr_internal.rb,
lib/active_support/core_ext/module/introspection.rb,
lib/active_support/vendor/builder-2.1.2/blankslate.rb,
lib/active_support/core_ext/module/attribute_accessors.rb,
lib/active_support/core_ext/module/attr_accessor_with_default.rb

Overview

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

module AppConfiguration
  mattr_accessor :google_api_key
  self.google_api_key = "123456789"

  mattr_accessor :paypal_url
  self.paypal_url = "www.sandbox.paypal.com"
end

AppConfiguration.google_api_key = "overriding the api key!"

Instance Method Summary collapse

Methods included from ActiveSupport::Deprecation::ClassMethods

#deprecate, #deprecated_method_warning, #deprecation_horizon

Instance Method Details

#alias_attribute(new_name, old_name) ⇒ Object

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"

63
64
65
66
67
68
69
# File 'lib/active_support/core_ext/module/aliasing.rb', line 63

def alias_attribute(new_name, old_name)
  module_eval <<-STR, __FILE__, __LINE__+1
    def #{new_name}; self.#{old_name}; end
    def #{new_name}?; self.#{old_name}?; end
    def #{new_name}=(v); self.#{old_name} = v; end
  STR
end

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

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.

Yields:

  • (aliased_target, punctuation)

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

def alias_method_chain(target, feature)
  # Strip out punctuation on predicates or bang methods since
  # e.g. target?_without_feature is not a valid method name.
  aliased_target, punctuation = target.to_s.sub(/([?!=])$/, ''), $1
  yield(aliased_target, punctuation) if block_given?
  
  with_method, without_method = "#{aliased_target}_with_#{feature}#{punctuation}", "#{aliased_target}_without_#{feature}#{punctuation}"
  
  alias_method without_method, target
  alias_method target, with_method
  
  case
    when public_method_defined?(without_method)
      public target
    when protected_method_defined?(without_method)
      protected target
    when private_method_defined?(without_method)
      private target
  end
end

#append_features(mod) ⇒ Object


105
106
107
108
109
110
111
112
# File 'lib/active_support/vendor/builder-2.1.2/blankslate.rb', line 105

def append_features(mod)
  result = blankslate_original_append_features(mod)
  return result if mod != Object
  instance_methods.each do |name|
    BlankSlate.hide(name)
  end
  result
end

#as_load_pathObject

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"

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

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

#attr_accessor_with_default(sym, default = nil, &block) ⇒ Object

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 }

21
22
23
24
25
26
27
28
29
30
# File 'lib/active_support/core_ext/module/attr_accessor_with_default.rb', line 21

def attr_accessor_with_default(sym, default = nil, &block)
  raise 'Default value or block required' unless !default.nil? || block
  define_method(sym, block_given? ? block : Proc.new { default })
  module_eval(<<-EVAL, __FILE__, __LINE__)
    def #{sym}=(value)
      class << self; attr_reader :#{sym} end
      @#{sym} = value
    end
  EVAL
end

#attr_internal_accessor(*attrs) ⇒ Object Also known as: attr_internal

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


18
19
20
21
# File 'lib/active_support/core_ext/module/attr_internal.rb', line 18

def attr_internal_accessor(*attrs)
  attr_internal_reader(*attrs)
  attr_internal_writer(*attrs)
end

#attr_internal_reader(*attrs) ⇒ Object

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


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

def attr_internal_reader(*attrs)
  attrs.each do |attr|
    module_eval "def #{attr}() #{attr_internal_ivar_name(attr)} end"
  end
end

#attr_internal_writer(*attrs) ⇒ Object

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


10
11
12
13
14
# File 'lib/active_support/core_ext/module/attr_internal.rb', line 10

def attr_internal_writer(*attrs)
  attrs.each do |attr|
    module_eval "def #{attr}=(v) #{attr_internal_ivar_name(attr)} = v end"
  end
end

#blankslate_original_append_featuresObject


104
# File 'lib/active_support/vendor/builder-2.1.2/blankslate.rb', line 104

alias blankslate_original_append_features append_features

#const_missing(class_id) ⇒ Object

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


466
467
468
# File 'lib/active_support/dependencies.rb', line 466

def const_missing(class_id)
  Dependencies.load_missing_constant self, class_id
end

#delegate(*methods) ⇒ Object

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

50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/active_support/core_ext/module/delegation.rb', line 50

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 as the last argument (e.g. delegate :hello, :to => :greeter)."
  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

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]

21
22
23
24
25
26
27
28
29
# File 'lib/active_support/core_ext/module/inclusion.rb', line 21

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

#local_constant_namesObject

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


72
73
74
# File 'lib/active_support/core_ext/module/introspection.rb', line 72

def local_constant_names
  local_constants.map(&:to_s)
end

#local_constantsObject

:nodoc:


65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/active_support/core_ext/module/introspection.rb', line 65

def local_constants
  inherited = {}

  ancestors.each do |anc|
    next if anc == self
    anc.constants.each { |const| inherited[const] = anc.const_get(const) }
  end

  constants.select do |const|
    !inherited.key?(const) || inherited[const].object_id != const_get(const).object_id
  end
end

#mattr_accessor(*syms) ⇒ Object


54
55
56
57
# File 'lib/active_support/core_ext/module/attribute_accessors.rb', line 54

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

#mattr_reader(*syms) ⇒ Object


14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/active_support/core_ext/module/attribute_accessors.rb', line 14

def mattr_reader(*syms)
  syms.each do |sym|
    next if sym.is_a?(Hash)
    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


33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/active_support/core_ext/module/attribute_accessors.rb', line 33

def mattr_writer(*syms)
  options = syms.extract_options!
  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
      " unless options[:instance_writer] == false }
    EOS
  end
end

#parentObject

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

18
19
20
21
# File 'lib/active_support/core_ext/module/introspection.rb', line 18

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

#parentsObject

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]

36
37
38
39
40
41
42
43
44
45
# File 'lib/active_support/core_ext/module/introspection.rb', line 36

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


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

alias :rails_original_const_missing :const_missing

#unloadable(const_desc = self) ⇒ Object


470
471
472
# File 'lib/active_support/dependencies.rb', line 470

def unloadable(const_desc = self)
  super(const_desc)
end