Configurable

实例方法

config 用 @_config 实例变量来保存配置信息。

使用举例:

require 'active_support/configurable'

class User
  include ActiveSupport::Configurable
end

user = User.new

# 写 config 对象
user.config.allowed_access = true
user.config.level = 1

# 读 config 对象
user.config.allowed_access # => true
user.config.level          # => 1

类方法

config_accessor 以声明的形式,同时定义类方法和实例方法。 实例对象的值默认继承于类对象,修改某个实例对象的值不影响类对象和其它实例对象的值。

使用举例:

再次举例:

除此之外,还有类方法:

Note: 它和 railties 目录下的 Configurable 和 Configuration 都没有关系。目前只发现有 AbstractController::Base 引用到它(其子类由于继承关系,也可以使用)。

Configuration

另外,它继承于 ActiveSupport::InheritableOptions

Note: 不对 module Configurable 之外提供接口,只有这里使用到它。

最后更新于

这有帮助吗?