提交 ad127d88 编写于 作者: S Sean Griffin

Rm `Type#type_cast`

This helper no longer makes sense as a separate method. Instead I'll
just have `deserialize` call `cast` by default. This led to a random
infinite loop in the `JSON` pg type, when it called `super` from
`deserialize`. Not really a great way to fix that other than not calling
super, or continuing to have the separate method, which makes the public
API differ from what we say it is.
上级 9ca6948f
......@@ -7,7 +7,7 @@ def type
:bit
end
def type_cast(value)
def cast(value)
if ::String === value
case value
when /^0x/i
......
......@@ -7,7 +7,7 @@ def type
:enum
end
def type_cast(value)
def cast(value)
value.to_s
end
end
......
......@@ -13,7 +13,7 @@ def deserialize(value)
if value.is_a?(::String)
::ActiveSupport::JSON.decode(value) rescue nil
else
super
value
end
end
......@@ -21,7 +21,7 @@ def serialize(value)
if value.is_a?(::Array) || value.is_a?(::Hash)
::ActiveSupport::JSON.encode(value)
else
super
value
end
end
......
......@@ -9,13 +9,13 @@ def type
:point
end
def type_cast(value)
def cast(value)
case value
when ::String
if value[0] == '(' && value[-1] == ')'
value = value[1...-1]
end
type_cast(value.split(','))
cast(value.split(','))
when ::Array
value.map { |v| Float(v) }
else
......
......@@ -11,7 +11,7 @@ def type
:uuid
end
def type_cast(value)
def cast(value)
value.to_s[ACCEPTABLE_UUID, 0]
end
end
......
......@@ -16,7 +16,7 @@ def initialize(delim, subtype)
# FIXME: this should probably split on +delim+ and use +subtype+
# to cast the values. Unfortunately, the current Rails behavior
# is to just return the string.
def type_cast(value)
def cast(value)
value
end
end
......
......@@ -9,7 +9,7 @@ def binary?
true
end
def type_cast(value)
def cast(value)
if value.is_a?(Data)
value.to_s
else
......
......@@ -7,7 +7,7 @@ def type
:float
end
alias serialize type_cast
alias serialize cast
private
......
......@@ -2,7 +2,7 @@ module ActiveRecord
module Type
module Helpers
module Numeric # :nodoc:
def type_cast(value)
def cast(value)
value = case value
when true then 1
when false then 0
......
......@@ -22,7 +22,7 @@ def deserialize(value)
end
def serialize(value)
result = type_cast(value)
result = cast(value)
if result
ensure_in_range(result)
end
......
......@@ -14,12 +14,12 @@ def type # :nodoc:
# Convert a value from database input to the appropriate ruby type. The
# return value of this method will be returned from
# ActiveRecord::AttributeMethods::Read#read_attribute. See also
# Value#type_cast and Value#cast_value.
# ActiveRecord::AttributeMethods::Read#read_attribute. The default
# implementation just calls Value#cast.
#
# +value+ The raw input, as provided from the database.
def deserialize(value)
type_cast(value)
cast(value)
end
# Type casts a value from user input (e.g. from a setter). This value may
......@@ -29,11 +29,11 @@ def deserialize(value)
#
# The return value of this method will be returned from
# ActiveRecord::AttributeMethods::Read#read_attribute. See also:
# Value#type_cast and Value#cast_value.
# Value#cast_value.
#
# +value+ The raw input, as provided to the attribute setter.
def cast(value)
type_cast(value)
cast_value(value) unless value.nil?
end
# Cast a value from the ruby type to a type that the database knows how
......@@ -93,16 +93,8 @@ def ==(other)
private
# Convenience method. If you don't need separate behavior for
# Value#deserialize and Value#cast, you can override
# this method instead. The default behavior of both methods is to call
# this one. See also Value#cast_value.
def type_cast(value) # :doc:
cast_value(value) unless value.nil?
end
# Convenience method for types which do not need separate type casting
# behavior for user and database inputs. Called by Value#type_cast for
# behavior for user and database inputs. Called by Value#cast for
# values except +nil+.
def cast_value(value) # :doc:
value
......
......@@ -1427,7 +1427,7 @@ def test_column_types_typecast
attrs.delete 'id'
typecast = Class.new(ActiveRecord::Type::Value) {
def type_cast value
def cast value
"t.lo"
end
}
......
......@@ -625,7 +625,7 @@ def test_datetime_attribute_doesnt_change_if_zone_is_modified_in_string
test "defaults with type that implements `serialize`" do
type = Class.new(ActiveRecord::Type::Value) do
def type_cast(value)
def cast(value)
value.to_i
end
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册