mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
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.
This commit is contained in:
parent
9ca6948f72
commit
ad127d8836
13 changed files with 20 additions and 28 deletions
|
@ -7,7 +7,7 @@ module ActiveRecord
|
|||
:bit
|
||||
end
|
||||
|
||||
def type_cast(value)
|
||||
def cast(value)
|
||||
if ::String === value
|
||||
case value
|
||||
when /^0x/i
|
||||
|
|
|
@ -7,7 +7,7 @@ module ActiveRecord
|
|||
:enum
|
||||
end
|
||||
|
||||
def type_cast(value)
|
||||
def cast(value)
|
||||
value.to_s
|
||||
end
|
||||
end
|
||||
|
|
|
@ -13,7 +13,7 @@ module ActiveRecord
|
|||
if value.is_a?(::String)
|
||||
::ActiveSupport::JSON.decode(value) rescue nil
|
||||
else
|
||||
super
|
||||
value
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -21,7 +21,7 @@ module ActiveRecord
|
|||
if value.is_a?(::Array) || value.is_a?(::Hash)
|
||||
::ActiveSupport::JSON.encode(value)
|
||||
else
|
||||
super
|
||||
value
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
@ -9,13 +9,13 @@ module ActiveRecord
|
|||
: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 @@ module ActiveRecord
|
|||
:uuid
|
||||
end
|
||||
|
||||
def type_cast(value)
|
||||
def cast(value)
|
||||
value.to_s[ACCEPTABLE_UUID, 0]
|
||||
end
|
||||
end
|
||||
|
|
|
@ -16,7 +16,7 @@ module ActiveRecord
|
|||
# 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 @@ module ActiveRecord
|
|||
true
|
||||
end
|
||||
|
||||
def type_cast(value)
|
||||
def cast(value)
|
||||
if value.is_a?(Data)
|
||||
value.to_s
|
||||
else
|
||||
|
|
|
@ -7,7 +7,7 @@ module ActiveRecord
|
|||
: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 @@ module ActiveRecord
|
|||
end
|
||||
|
||||
def serialize(value)
|
||||
result = type_cast(value)
|
||||
result = cast(value)
|
||||
if result
|
||||
ensure_in_range(result)
|
||||
end
|
||||
|
|
|
@ -14,12 +14,12 @@ module ActiveRecord
|
|||
|
||||
# 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 @@ module ActiveRecord
|
|||
#
|
||||
# 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 @@ module ActiveRecord
|
|||
|
||||
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 @@ class BasicsTest < ActiveRecord::TestCase
|
|||
attrs.delete 'id'
|
||||
|
||||
typecast = Class.new(ActiveRecord::Type::Value) {
|
||||
def type_cast value
|
||||
def cast value
|
||||
"t.lo"
|
||||
end
|
||||
}
|
||||
|
|
|
@ -625,7 +625,7 @@ class DirtyTest < ActiveRecord::TestCase
|
|||
|
||||
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
|
||||
|
||||
|
|
Loading…
Reference in a new issue