方法
- A
- D
- F
- M
- N
- S
常量
| CALLBACKS_OPTIONS | = | [:if, :unless, :on, :allow_nil, :allow_blank, :strict] |
| MESSAGE_OPTIONS | = | [:message] |
Attributes
| [R] | attribute | 错误所属的 |
| [R] | base | 错误所属的对象 |
| [R] | options | 调用 |
| [R] | raw_type | 调用 |
| [R] | type | 错误的类型,默认为 |
类公共方法
new(base, attribute, type = :invalid, **options) 链接
来源: 显示 | 在 GitHub 上
# File activemodel/lib/active_model/error.rb, line 102 def initialize(base, attribute, type = :invalid, **options) @base = base @attribute = attribute @raw_type = type @type = type || :invalid @options = options end
实例公共方法
details() 链接
返回错误详情。
error = ActiveModel::Error.new(person, :name, :too_short, count: 5) error.details # => { error: :too_short, count: 5 }
也别名为: detail
来源: 显示 | 在 GitHub 上
# File activemodel/lib/active_model/error.rb, line 148 def details { error: raw_type }.merge(options.except(*CALLBACKS_OPTIONS + MESSAGE_OPTIONS)) end
full_message() 链接
返回完整的错误消息。
error = ActiveModel::Error.new(person, :name, :too_short, count: 5) error.full_message # => "Name is too short (minimum is 5 characters)"
来源: 显示 | 在 GitHub 上
# File activemodel/lib/active_model/error.rb, line 158 def full_message self.class.full_message(attribute, message, @base) end
match?(attribute, type = nil, **options) 链接
检查错误是否与提供的 attribute、type 和 options 匹配。
省略的参数不用于匹配检查。
来源: 显示 | 在 GitHub 上
# File activemodel/lib/active_model/error.rb, line 165 def match?(attribute, type = nil, **options) if @attribute != attribute || (type && @type != type) return false end options.each do |key, value| if @options[key] != value return false end end true end
message() 链接
返回错误消息。
error = ActiveModel::Error.new(person, :name, :too_short, count: 5) error.message # => "is too short (minimum is 5 characters)"
来源: 显示 | 在 GitHub 上
# File activemodel/lib/active_model/error.rb, line 134 def message case raw_type when Symbol self.class.generate_message(attribute, raw_type, @base, options.except(*CALLBACKS_OPTIONS)) else raw_type end end
strict_match?(attribute, type, **options) 链接
检查错误是否与提供的 attribute、type 和 options 完全匹配。
所有参数都必须等于 Error 自身的属性才能被视为严格匹配。
来源: 显示 | 在 GitHub 上
# File activemodel/lib/active_model/error.rb, line 183 def strict_match?(attribute, type, **options) return false unless match?(attribute, type) options == @options.except(*CALLBACKS_OPTIONS + MESSAGE_OPTIONS) end
实例保护方法
attributes_for_hash() 链接
来源: 显示 | 在 GitHub 上
# File activemodel/lib/active_model/error.rb, line 203 def attributes_for_hash [@base, @attribute, @raw_type, @options.except(*CALLBACKS_OPTIONS)] end