- #
- A
- C
- D
- E
- H
- N
- S
- T
Attributes
| [R] | limit | |
| [R] | precision | |
| [R] | scale |
类公共方法
new(precision: nil, limit: nil, scale: nil) 链接
使用三个基本配置设置初始化类型:精度(precision)、限制(limit)和刻度(scale)。Value 基类不为此类设置定义行为。它仅将它们用于相等性比较和哈希键生成。
来源: 显示 | 在 GitHub 上
# File activemodel/lib/active_model/type/value.rb, line 17 def initialize(precision: nil, limit: nil, scale: nil) super() @precision = precision @scale = scale @limit = limit end
实例公共方法
==(other) 链接
来源: 显示 | 在 GitHub 上
# File activemodel/lib/active_model/type/value.rb, line 121 def ==(other) self.class == other.class && precision == other.precision && scale == other.scale && limit == other.limit end
as_json(*) 链接
来源: 显示 | 在 GitHub 上
# File activemodel/lib/active_model/type/value.rb, line 144 def as_json(*) raise NoMethodError end
assert_valid_value(_) 链接
来源: 显示 | 在 GitHub 上
# File activemodel/lib/active_model/type/value.rb, line 133 def assert_valid_value(_) end
cast(value) 链接
Type 将用户输入的值(例如来自设置器的值)进行转换。此值可能是表单构建器的字符串,也可能是传递给设置器的 Ruby 对象。目前无法区分它们来自哪个来源。
此方法的返回值将从 ActiveRecord::AttributeMethods::Read#read_attribute 返回。另请参阅: Value#cast_value。
value 原始输入,如属性设置器所提供。
来源: 显示 | 在 GitHub 上
# File activemodel/lib/active_model/type/value.rb, line 57 def cast(value) cast_value(value) unless value.nil? end
changed?(old_value, new_value, _new_value_before_type_cast) 链接
确定值是否已更改(用于脏检查)。old_value 和 new_value 始终经过类型转换。类型通常不需要重写此方法。
来源: 显示 | 在 GitHub 上
# File activemodel/lib/active_model/type/value.rb, line 84 def changed?(old_value, new_value, _new_value_before_type_cast) old_value != new_value end
changed_in_place?(raw_old_value, new_value) 链接
确定可变值自读取以来是否已被修改。默认返回 false。如果您的类型返回一个可能被修改的对象,您应该重写此方法。您需要
-
将
new_value传递给Value#serialize并与raw_old_value进行比较
或者
-
将
raw_old_value传递给Value#deserialize并与new_value进行比较
raw_old_value 原始值,在传递给 deserialize 之前。
new_value 当前值,在类型转换后。
来源: 显示 | 在 GitHub 上
# File activemodel/lib/active_model/type/value.rb, line 105 def changed_in_place?(raw_old_value, new_value) false end
deserialize(value) 链接
将数据库输入的值转换为适当的 Ruby 类型。此方法的返回值将从 ActiveRecord::AttributeMethods::Read#read_attribute 返回。默认实现仅调用 Value#cast。
value 原始输入,如从数据库获取。
来源: 显示 | 在 GitHub 上
# File activemodel/lib/active_model/type/value.rb, line 43 def deserialize(value) cast(value) end
hash() 链接
来源: 显示 | 在 GitHub 上
# File activemodel/lib/active_model/type/value.rb, line 129 def hash [self.class, precision, scale, limit].hash end
serializable?(value, &) 链接
返回此类型是否可以将 value 转换为可供数据库使用的类型。例如,布尔类型如果 value 参数是 Ruby 布尔值,则可以返回 true,但如果 value 参数是其他对象,则可能返回 false。
来源: 显示 | 在 GitHub 上
# File activemodel/lib/active_model/type/value.rb, line 28 def serializable?(value, &) true end
serialize(value) 链接
来源: 显示 | 在 GitHub 上
# File activemodel/lib/active_model/type/value.rb, line 65 def serialize(value) value end
type() 链接
返回唯一的类型名称(作为 Symbol)。子类应重写此方法。
来源: 显示 | 在 GitHub 上
# File activemodel/lib/active_model/type/value.rb, line 34 def type end
实例私有方法
cast_value(value) 链接
对于不需要区分用户输入和数据库输入类型转换行为的类型,这是一个方便的方法。对于 nil 以外的值,由 Value#cast 调用。
来源: 显示 | 在 GitHub 上
# File activemodel/lib/active_model/type/value.rb, line 152 def cast_value(value) # :doc: value end