`Type#type_cast_from_database` -> `Type#deserialize`

This commit is contained in:
Sean Griffin 2015-02-17 11:29:51 -07:00
parent b3fdf9c596
commit 4a3cb840b0
36 changed files with 71 additions and 71 deletions

View File

@ -108,7 +108,7 @@ module ActiveRecord
class FromDatabase < Attribute # :nodoc:
def type_cast(value)
type.type_cast_from_database(value)
type.deserialize(value)
end
end

View File

@ -2,7 +2,7 @@ module ActiveRecord
module AttributeMethods
module TimeZoneConversion
class TimeZoneConverter < DelegateClass(Type::Value) # :nodoc:
def type_cast_from_database(value)
def deserialize(value)
convert_time_to_time_zone(super)
end

View File

@ -102,7 +102,7 @@ module ActiveRecord
#
# Users may also define their own custom types, as long as they respond
# to the methods defined on the value type. The method
# +type_cast_from_database+ or +type_cast_from_user+ will be called on
# +deserialize+ or +type_cast_from_user+ will be called on
# your type object, with raw input from the database or from your
# controllers. See ActiveRecord::Type::Value for the expected API. It is
# recommended that your type objects inherit from an existing type, or
@ -143,7 +143,7 @@ module ActiveRecord
# @currency_converter = currency_converter
# end
#
# # value will be the result of +type_cast_from_database+ or
# # value will be the result of +deserialize+ or
# # +type_cast_from_user+. Assumed to be in instance of +Money+ in
# # this case.
# def type_cast_for_database(value)
@ -195,7 +195,7 @@ module ActiveRecord
# Otherwise, the default will be +nil+.
#
# +user_provided_default+ Whether the default value should be cast using
# +type_cast_from_user+ or +type_cast_from_database+.
# +type_cast_from_user+ or +deserialize+.
def define_attribute(
name,
cast_type,

View File

@ -47,7 +47,7 @@ module ActiveRecord
def schema_default(column)
type = lookup_cast_type_from_column(column)
default = type.type_cast_from_database(column.default)
default = type.deserialize(column.default)
unless default.nil?
type.type_cast_for_schema(default)
end

View File

@ -25,9 +25,9 @@ module ActiveRecord
@delimiter = delimiter
end
def type_cast_from_database(value)
def deserialize(value)
if value.is_a?(::String)
type_cast_array(parse_pg_array(value), :type_cast_from_database)
type_cast_array(parse_pg_array(value), :deserialize)
else
super
end

View File

@ -3,7 +3,7 @@ module ActiveRecord
module PostgreSQL
module OID # :nodoc:
class Bytea < Type::Binary # :nodoc:
def type_cast_from_database(value)
def deserialize(value)
return if value.nil?
return value.to_s if value.is_a?(Type::Binary::Data)
PGconn.unescape_bytea(super)

View File

@ -9,7 +9,7 @@ module ActiveRecord
:hstore
end
def type_cast_from_database(value)
def deserialize(value)
if value.is_a?(::String)
::Hash[value.scan(HstorePair).map { |k, v|
v = v.upcase == 'NULL' ? nil : v.gsub(/\A"(.*)"\Z/m,'\1').gsub(/\\(.)/, '\1')

View File

@ -9,7 +9,7 @@ module ActiveRecord
:json
end
def type_cast_from_database(value)
def deserialize(value)
if value.is_a?(::String)
::ActiveSupport::JSON.decode(value) rescue nil
else

View File

@ -13,7 +13,7 @@ module ActiveRecord
# the comparison here. Therefore, we need to parse and re-dump the
# raw value here to ensure the insignificant whitespaces are
# consistent with our encoder's output.
raw_old_value = type_cast_for_database(type_cast_from_database(raw_old_value))
raw_old_value = type_cast_for_database(deserialize(raw_old_value))
super(raw_old_value, new_value)
end
end

View File

@ -49,7 +49,7 @@ module ActiveRecord
private
def type_cast_single(value)
infinity?(value) ? value : @subtype.type_cast_from_database(value)
infinity?(value) ? value : @subtype.deserialize(value)
end
def type_cast_single_for_database(value)

View File

@ -5,7 +5,7 @@ module ActiveRecord
class Uuid < Type::Value # :nodoc:
ACCEPTABLE_UUID = %r{\A\{?([a-fA-F0-9]{4}-?){8}\}?\z}x
alias_method :type_cast_for_database, :type_cast_from_database
alias_method :type_cast_for_database, :deserialize
def type
:uuid

View File

@ -105,7 +105,7 @@ module ActiveRecord
end
end
def type_cast_from_database(value)
def deserialize(value)
return if value.nil?
mapping.key(value.to_i)
end

View File

@ -185,7 +185,7 @@ module ActiveRecord
end
class LockingType < DelegateClass(Type::Value) # :nodoc:
def type_cast_from_database(value)
def deserialize(value)
# `nil` *should* be changed to 0
super.to_i
end

View File

@ -353,9 +353,9 @@ module ActiveRecord
def type_cast_calculated_value(value, type, operation = nil)
case operation
when 'count' then value.to_i
when 'sum' then type.type_cast_from_database(value || 0)
when 'sum' then type.deserialize(value || 0)
when 'average' then value.respond_to?(:to_d) ? value.to_d : value
else type.type_cast_from_database(value)
else type.deserialize(value)
end
end

View File

@ -81,7 +81,7 @@ module ActiveRecord
def cast_values(type_overrides = {}) # :nodoc:
types = columns.map { |name| column_type(name, type_overrides) }
result = rows.map do |values|
types.zip(values).map { |type, value| type.type_cast_from_database(value) }
types.zip(values).map { |type, value| type.deserialize(value) }
end
columns.one? ? result.map!(&:first) : result

View File

@ -23,7 +23,7 @@ module ActiveRecord
end
def changed_in_place?(raw_old_value, value)
old_value = type_cast_from_database(raw_old_value)
old_value = deserialize(raw_old_value)
old_value != value
end

View File

@ -3,7 +3,7 @@ module ActiveRecord
module Helpers
module Mutable # :nodoc:
def type_cast_from_user(value)
type_cast_from_database(type_cast_for_database(value))
deserialize(type_cast_for_database(value))
end
# +raw_old_value+ will be the `_before_type_cast` version of the

View File

@ -16,7 +16,7 @@ module ActiveRecord
:integer
end
def type_cast_from_database(value)
def deserialize(value)
return if value.nil?
value.to_i
end

View File

@ -11,7 +11,7 @@ module ActiveRecord
super(subtype)
end
def type_cast_from_database(value)
def deserialize(value)
if default_value?(value)
value
else

View File

@ -18,7 +18,7 @@ module ActiveRecord
# Value#type_cast and Value#cast_value.
#
# +value+ The raw input, as provided from the database.
def type_cast_from_database(value)
def deserialize(value)
type_cast(value)
end
@ -73,11 +73,11 @@ module ActiveRecord
#
# or
#
# - pass +raw_old_value+ to Value#type_cast_from_database and compare it to
# - pass +raw_old_value+ to Value#deserialize and compare it to
# +new_value+
#
# +raw_old_value+ The original value, before being passed to
# +type_cast_from_database+.
# +deserialize+.
#
# +new_value+ The current value, after type casting.
def changed_in_place?(raw_old_value, new_value)
@ -94,7 +94,7 @@ module ActiveRecord
private
# Convenience method. If you don't need separate behavior for
# Value#type_cast_from_database and Value#type_cast_from_user, you can override
# Value#deserialize and Value#type_cast_from_user, 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:

View File

@ -110,7 +110,7 @@ module ActiveRecord
result = @conn.exec_query('SELECT status FROM ex')
assert_equal 2, result.column_types['status'].type_cast_from_database(result.last['status'])
assert_equal 2, result.column_types['status'].deserialize(result.last['status'])
end
end

View File

@ -92,9 +92,9 @@ class PostgresqlArrayTest < ActiveRecord::TestCase
end
def test_type_cast_array
assert_equal(['1', '2', '3'], @type.type_cast_from_database('{1,2,3}'))
assert_equal([], @type.type_cast_from_database('{}'))
assert_equal([nil], @type.type_cast_from_database('{NULL}'))
assert_equal(['1', '2', '3'], @type.deserialize('{1,2,3}'))
assert_equal([], @type.deserialize('{}'))
assert_equal([nil], @type.deserialize('{NULL}'))
end
def test_type_cast_integers

View File

@ -40,16 +40,16 @@ class PostgresqlByteaTest < ActiveRecord::TestCase
data = "\u001F\x8B"
assert_equal('UTF-8', data.encoding.name)
assert_equal('ASCII-8BIT', @type.type_cast_from_database(data).encoding.name)
assert_equal('ASCII-8BIT', @type.deserialize(data).encoding.name)
end
def test_type_cast_binary_value
data = "\u001F\x8B".force_encoding("BINARY")
assert_equal(data, @type.type_cast_from_database(data))
assert_equal(data, @type.deserialize(data))
end
def test_type_case_nil
assert_equal(nil, @type.type_cast_from_database(nil))
assert_equal(nil, @type.deserialize(nil))
end
def test_read_value

View File

@ -83,7 +83,7 @@ class PostgresqlCompositeWithCustomOIDTest < ActiveRecord::TestCase
class FullAddressType < ActiveRecord::Type::Value
def type; :full_address end
def type_cast_from_database(value)
def deserialize(value)
if value =~ /\("?([^",]*)"?,"?([^",]*)"?\)/
FullAddress.new($1, $2)
end

View File

@ -131,7 +131,7 @@ module ActiveRecord
name = @subscriber.payloads.last[:statement_name]
assert name
res = @connection.exec_query("EXPLAIN (FORMAT JSON) EXECUTE #{name}(1)")
plan = res.column_types['QUERY PLAN'].type_cast_from_database res.rows.first.first
plan = res.column_types['QUERY PLAN'].deserialize res.rows.first.first
assert_operator plan.length, :>, 0
end

View File

@ -110,10 +110,10 @@ if ActiveRecord::Base.connection.supports_extensions?
end
def test_type_cast_hstore
assert_equal({'1' => '2'}, @type.type_cast_from_database("\"1\"=>\"2\""))
assert_equal({}, @type.type_cast_from_database(""))
assert_equal({'key'=>nil}, @type.type_cast_from_database('key => NULL'))
assert_equal({'c'=>'}','"a"'=>'b "a b'}, @type.type_cast_from_database(%q(c=>"}", "\"a\""=>"b \"a b")))
assert_equal({'1' => '2'}, @type.deserialize("\"1\"=>\"2\""))
assert_equal({}, @type.deserialize(""))
assert_equal({'key'=>nil}, @type.deserialize('key => NULL'))
assert_equal({'c'=>'}','"a"'=>'b "a b'}, @type.deserialize(%q(c=>"}", "\"a\""=>"b \"a b")))
end
def test_with_store_accessors
@ -181,31 +181,31 @@ if ActiveRecord::Base.connection.supports_extensions?
end
def test_parse1
assert_equal({'a'=>nil,'b'=>nil,'c'=>'NuLl','null'=>'c'}, @type.type_cast_from_database('a=>null,b=>NuLl,c=>"NuLl",null=>c'))
assert_equal({'a'=>nil,'b'=>nil,'c'=>'NuLl','null'=>'c'}, @type.deserialize('a=>null,b=>NuLl,c=>"NuLl",null=>c'))
end
def test_parse2
assert_equal({" " => " "}, @type.type_cast_from_database("\\ =>\\ "))
assert_equal({" " => " "}, @type.deserialize("\\ =>\\ "))
end
def test_parse3
assert_equal({"=" => ">"}, @type.type_cast_from_database("==>>"))
assert_equal({"=" => ">"}, @type.deserialize("==>>"))
end
def test_parse4
assert_equal({"=a"=>"q=w"}, @type.type_cast_from_database('\=a=>q=w'))
assert_equal({"=a"=>"q=w"}, @type.deserialize('\=a=>q=w'))
end
def test_parse5
assert_equal({"=a"=>"q=w"}, @type.type_cast_from_database('"=a"=>q\=w'))
assert_equal({"=a"=>"q=w"}, @type.deserialize('"=a"=>q\=w'))
end
def test_parse6
assert_equal({"\"a"=>"q>w"}, @type.type_cast_from_database('"\"a"=>q>w'))
assert_equal({"\"a"=>"q>w"}, @type.deserialize('"\"a"=>q>w'))
end
def test_parse7
assert_equal({"\"a"=>"q\"w"}, @type.type_cast_from_database('\"a=>q"w'))
assert_equal({"\"a"=>"q\"w"}, @type.deserialize('\"a=>q"w'))
end
def test_rewrite

View File

@ -80,13 +80,13 @@ module PostgresqlJSONSharedTestCases
type = JsonDataType.type_for_attribute("payload")
data = "{\"a_key\":\"a_value\"}"
hash = type.type_cast_from_database(data)
hash = type.deserialize(data)
assert_equal({'a_key' => 'a_value'}, hash)
assert_equal({'a_key' => 'a_value'}, type.type_cast_from_database(data))
assert_equal({'a_key' => 'a_value'}, type.deserialize(data))
assert_equal({}, type.type_cast_from_database("{}"))
assert_equal({'key'=>nil}, type.type_cast_from_database('{"key": null}'))
assert_equal({'c'=>'}','"a"'=>'b "a b'}, type.type_cast_from_database(%q({"c":"}", "\"a\"":"b \"a b"})))
assert_equal({}, type.deserialize("{}"))
assert_equal({'key'=>nil}, type.deserialize('{"key": null}'))
assert_equal({'c'=>'}','"a"'=>'b "a b'}, type.deserialize(%q({"c":"}", "\"a\"":"b \"a b"})))
end
def test_rewrite

View File

@ -16,7 +16,7 @@ module ActiveRecord
"#{super} #{@decoration}"
end
alias type_cast_from_database type_cast_from_user
alias deserialize type_cast_from_user
end
setup do
@ -106,7 +106,7 @@ module ActiveRecord
return if value.nil?
value * 2
end
alias type_cast_from_database type_cast_from_user
alias deserialize type_cast_from_user
end
test "decorating with a proc" do

View File

@ -156,7 +156,7 @@ module ActiveRecord
value + " from user"
end
def type_cast_from_database(value)
def deserialize(value)
return if value.nil?
value + " from database"
end

View File

@ -13,7 +13,7 @@ module ActiveRecord
end
test "from_database + read type casts from database" do
@type.expect(:type_cast_from_database, 'type cast from database', ['a value'])
@type.expect(:deserialize, 'type cast from database', ['a value'])
attribute = Attribute.from_database(nil, 'a value', @type)
type_cast_value = attribute.value
@ -31,7 +31,7 @@ module ActiveRecord
end
test "reading memoizes the value" do
@type.expect(:type_cast_from_database, 'from the database', ['whatever'])
@type.expect(:deserialize, 'from the database', ['whatever'])
attribute = Attribute.from_database(nil, 'whatever', @type)
type_cast_value = attribute.value
@ -42,7 +42,7 @@ module ActiveRecord
end
test "reading memoizes falsy values" do
@type.expect(:type_cast_from_database, false, ['whatever'])
@type.expect(:deserialize, false, ['whatever'])
attribute = Attribute.from_database(nil, 'whatever', @type)
attribute.value
@ -58,7 +58,7 @@ module ActiveRecord
end
test "from_database + read_for_database type casts to and from database" do
@type.expect(:type_cast_from_database, 'read from database', ['whatever'])
@type.expect(:deserialize, 'read from database', ['whatever'])
@type.expect(:type_cast_for_database, 'ready for database', ['read from database'])
attribute = Attribute.from_database(nil, 'whatever', @type)
@ -78,7 +78,7 @@ module ActiveRecord
end
test "duping dups the value" do
@type.expect(:type_cast_from_database, 'type cast', ['a value'])
@type.expect(:deserialize, 'type cast', ['a value'])
attribute = Attribute.from_database(nil, 'a value', @type)
value_from_orig = attribute.value
@ -90,7 +90,7 @@ module ActiveRecord
end
test "duping does not dup the value if it is not dupable" do
@type.expect(:type_cast_from_database, false, ['a value'])
@type.expect(:deserialize, false, ['a value'])
attribute = Attribute.from_database(nil, 'a value', @type)
assert_same attribute.value, attribute.dup.value
@ -106,7 +106,7 @@ module ActiveRecord
value + " from user"
end
def type_cast_from_database(value)
def deserialize(value)
value + " from database"
end
end

View File

@ -112,7 +112,7 @@ module ActiveRecord
"from user"
end
def type_cast_from_database(*)
def deserialize(*)
"from database"
end
end

View File

@ -68,8 +68,8 @@ module ActiveRecord
five = columns.detect { |c| c.name == "five" } unless mysql
assert_equal "hello", one.default
assert_equal true, connection.lookup_cast_type_from_column(two).type_cast_from_database(two.default)
assert_equal false, connection.lookup_cast_type_from_column(three).type_cast_from_database(three.default)
assert_equal true, connection.lookup_cast_type_from_column(two).deserialize(two.default)
assert_equal false, connection.lookup_cast_type_from_column(three).deserialize(three.default)
assert_equal '1', four.default
assert_equal "hello", five.default unless mysql
end

View File

@ -196,7 +196,7 @@ module ActiveRecord
old_columns = connection.columns(TestModel.table_name)
assert old_columns.find { |c|
default = connection.lookup_cast_type_from_column(c).type_cast_from_database(c.default)
default = connection.lookup_cast_type_from_column(c).deserialize(c.default)
c.name == 'approved' && c.type == :boolean && default == true
}
@ -204,11 +204,11 @@ module ActiveRecord
new_columns = connection.columns(TestModel.table_name)
assert_not new_columns.find { |c|
default = connection.lookup_cast_type_from_column(c).type_cast_from_database(c.default)
default = connection.lookup_cast_type_from_column(c).deserialize(c.default)
c.name == 'approved' and c.type == :boolean and default == true
}
assert new_columns.find { |c|
default = connection.lookup_cast_type_from_column(c).type_cast_from_database(c.default)
default = connection.lookup_cast_type_from_column(c).deserialize(c.default)
c.name == 'approved' and c.type == :boolean and default == false
}
change_column :test_models, :approved, :boolean, :default => true

View File

@ -92,7 +92,7 @@ class ReflectionTest < ActiveRecord::TestCase
type = @first.type_for_attribute("attribute_that_doesnt_exist")
object = Object.new
assert_equal object, type.type_cast_from_database(object)
assert_equal object, type.deserialize(object)
assert_equal object, type.type_cast_from_user(object)
assert_equal object, type.type_cast_for_database(object)
end

View File

@ -254,7 +254,7 @@ module ActiveRecord
:string
end
def type_cast_from_database(value)
def deserialize(value)
raise value unless value == "type cast for database"
"type cast from database"
end

View File

@ -13,7 +13,7 @@ module ActiveRecord
s = "foo"
type = Type::String.new
assert_not_same s, type.type_cast_from_user(s)
assert_not_same s, type.type_cast_from_database(s)
assert_not_same s, type.deserialize(s)
end
test "string mutations are detected" do