1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

type_cast_for_database -> serialize

This commit is contained in:
Sean Griffin 2015-02-17 13:35:23 -07:00
parent 4a3cb840b0
commit 1455c4c22f
40 changed files with 79 additions and 79 deletions

View file

@ -43,7 +43,7 @@ module ActiveRecord
end
def value_for_database
type.type_cast_for_database(value)
type.serialize(value)
end
def changed_from?(old_value)

View file

@ -133,7 +133,7 @@ module ActiveRecord
#
# When ActiveRecord::QueryMethods#where is called, it will
# use the type defined by the model class to convert the value to SQL,
# calling +type_cast_for_database+ on your type object. For example:
# calling +serialize+ on your type object. For example:
#
# class Money < Struct.new(:amount, :currency)
# end
@ -146,7 +146,7 @@ module ActiveRecord
# # 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)
# def serialize(value)
# value_in_bitcoins = @currency_converter.convert_to_bitcoins(value)
# value_in_bitcoins.amount
# end

View file

@ -52,7 +52,7 @@ module ActiveRecord
def type_cast_from_column(column, value) # :nodoc:
if column
type = lookup_cast_type_from_column(column)
type.type_cast_for_database(value)
type.serialize(value)
else
value
end
@ -103,7 +103,7 @@ module ActiveRecord
end
def quote_default_expression(value, column) #:nodoc:
value = lookup_cast_type(column.sql_type).type_cast_for_database(value)
value = lookup_cast_type(column.sql_type).serialize(value)
quote(value)
end

View file

@ -926,7 +926,7 @@ module ActiveRecord
end
class MysqlString < Type::String # :nodoc:
def type_cast_for_database(value)
def serialize(value)
case value
when true then "1"
when false then "0"

View file

@ -40,7 +40,7 @@ module ActiveRecord
type_cast_array(value, :type_cast_from_user)
end
def type_cast_for_database(value)
def serialize(value)
if value.is_a?(::Array)
cast_value_for_database(value)
else
@ -69,7 +69,7 @@ module ActiveRecord
casted_values = value.map { |item| cast_value_for_database(item) }
"{#{casted_values.join(delimiter)}}"
else
quote_and_escape(subtype.type_cast_for_database(value))
quote_and_escape(subtype.serialize(value))
end
end

View file

@ -20,7 +20,7 @@ module ActiveRecord
end
end
def type_cast_for_database(value)
def serialize(value)
Data.new(super) if value
end

View file

@ -18,7 +18,7 @@ module ActiveRecord
end
end
def type_cast_for_database(value)
def serialize(value)
if IPAddr === value
"#{value}/#{value.instance_variable_get(:@mask_addr).to_s(2).count('1')}"
else

View file

@ -21,7 +21,7 @@ module ActiveRecord
end
end
def type_cast_for_database(value)
def serialize(value)
if value.is_a?(::Hash)
value.map { |k, v| "#{escape_hstore(k)}=>#{escape_hstore(v)}" }.join(', ')
else

View file

@ -17,7 +17,7 @@ module ActiveRecord
end
end
def type_cast_for_database(value)
def serialize(value)
if value.is_a?(::Array) || value.is_a?(::Hash)
::ActiveSupport::JSON.encode(value)
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(deserialize(raw_old_value))
raw_old_value = serialize(deserialize(raw_old_value))
super(raw_old_value, new_value)
end
end

View file

@ -23,7 +23,7 @@ module ActiveRecord
end
end
def type_cast_for_database(value)
def serialize(value)
if value.is_a?(::Array)
"(#{number_for_point(value[0])},#{number_for_point(value[1])})"
else

View file

@ -30,7 +30,7 @@ module ActiveRecord
::Range.new(from, to, extracted[:exclude_end])
end
def type_cast_for_database(value)
def serialize(value)
if value.is_a?(::Range)
from = type_cast_single_for_database(value.begin)
to = type_cast_single_for_database(value.end)
@ -53,7 +53,7 @@ module ActiveRecord
end
def type_cast_single_for_database(value)
infinity?(value) ? '' : @subtype.type_cast_for_database(value)
infinity?(value) ? '' : @subtype.serialize(value)
end
def extract_bounds(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, :deserialize
alias_method :serialize, :deserialize
def type
:uuid

View file

@ -7,7 +7,7 @@ module ActiveRecord
:xml
end
def type_cast_for_database(value)
def serialize(value)
return unless value
Data.new(super)
end

View file

@ -110,7 +110,7 @@ module ActiveRecord
mapping.key(value.to_i)
end
def type_cast_for_database(value)
def serialize(value)
mapping.fetch(value, value)
end

View file

@ -75,7 +75,7 @@ module ActiveRecord
def sanitize_sql_hash_for_assignment(attrs, table)
c = connection
attrs.map do |attr, value|
value = type_for_attribute(attr.to_s).type_cast_for_database(value)
value = type_for_attribute(attr.to_s).serialize(value)
"#{c.quote_table_name_for_assignment(table, attr)} = #{c.quote(value)}"
end.join(', ')
end

View file

@ -17,7 +17,7 @@ module ActiveRecord
end
end
def type_cast_for_database(value)
def serialize(value)
return if value.nil?
Data.new(super)
end

View file

@ -10,7 +10,7 @@ module ActiveRecord
:datetime
end
def type_cast_for_database(value)
def serialize(value)
if precision && value.respond_to?(:usec)
number_of_insignificant_digits = 6 - precision
round_power = 10 ** number_of_insignificant_digits

View file

@ -7,7 +7,7 @@ module ActiveRecord
:float
end
alias type_cast_for_database type_cast
alias serialize type_cast
private

View file

@ -3,14 +3,14 @@ module ActiveRecord
module Helpers
module Mutable # :nodoc:
def type_cast_from_user(value)
deserialize(type_cast_for_database(value))
deserialize(serialize(value))
end
# +raw_old_value+ will be the `_before_type_cast` version of the
# value (likely a string). +new_value+ will be the current, type
# cast value.
def changed_in_place?(raw_old_value, new_value)
raw_old_value != type_cast_for_database(new_value)
raw_old_value != serialize(new_value)
end
end
end

View file

@ -21,7 +21,7 @@ module ActiveRecord
value.to_i
end
def type_cast_for_database(value)
def serialize(value)
result = type_cast(value)
if result
ensure_in_range(result)

View file

@ -19,7 +19,7 @@ module ActiveRecord
end
end
def type_cast_for_database(value)
def serialize(value)
return if value.nil?
unless default_value?(value)
super coder.dump(value)
@ -28,7 +28,7 @@ module ActiveRecord
def changed_in_place?(raw_old_value, value)
return false if value.nil?
subtype.changed_in_place?(raw_old_value, type_cast_for_database(value))
subtype.changed_in_place?(raw_old_value, serialize(value))
end
def accessor

View file

@ -11,7 +11,7 @@ module ActiveRecord
end
end
def type_cast_for_database(value)
def serialize(value)
case value
when ::Numeric, ActiveSupport::Duration then value.to_s
when ::String then ::String.new(value)

View file

@ -40,7 +40,7 @@ module ActiveRecord
# to understand. The returned value from this method should be a
# +String+, +Numeric+, +Date+, +Time+, +Symbol+, +true+, +false+, or
# +nil+.
def type_cast_for_database(value)
def serialize(value)
value
end
@ -68,7 +68,7 @@ module ActiveRecord
# which could be mutated, you should override this method. You will need
# to either:
#
# - pass +new_value+ to Value#type_cast_for_database and compare it to
# - pass +new_value+ to Value#serialize and compare it to
# +raw_old_value+
#
# or

View file

@ -8,7 +8,7 @@ module ActiveRecord
def type_cast_for_database(attr_name, value)
return value if value.is_a?(Arel::Nodes::BindParam)
type = types.type_for_attribute(attr_name.to_s)
type.type_cast_for_database(value)
type.serialize(value)
end
protected

View file

@ -61,7 +61,7 @@ module ActiveRecord
column = klass.columns_hash[attribute_name]
cast_type = klass.type_for_attribute(attribute_name)
value = cast_type.type_cast_for_database(value)
value = cast_type.serialize(value)
value = klass.connection.type_cast(value)
if value.is_a?(String) && column.limit
value = value.to_s[0, column.limit]

View file

@ -206,7 +206,7 @@ class PostgresqlArrayTest < ActiveRecord::TestCase
x = PgArray.create!(tags: tags)
x.reload
assert_equal x.tags_before_type_cast, PgArray.type_for_attribute('tags').type_cast_for_database(tags)
assert_equal x.tags_before_type_cast, PgArray.type_for_attribute('tags').serialize(tags)
end
def test_quoting_non_standard_delimiters
@ -214,8 +214,8 @@ class PostgresqlArrayTest < ActiveRecord::TestCase
comma_delim = OID::Array.new(ActiveRecord::Type::String.new, ',')
semicolon_delim = OID::Array.new(ActiveRecord::Type::String.new, ';')
assert_equal %({"hello,",world;}), comma_delim.type_cast_for_database(strings)
assert_equal %({hello,;"world;"}), semicolon_delim.type_cast_for_database(strings)
assert_equal %({"hello,",world;}), comma_delim.serialize(strings)
assert_equal %({hello,;"world;"}), semicolon_delim.serialize(strings)
end
def test_mutate_array

View file

@ -10,14 +10,14 @@ module ActiveRecord
ip = IPAddr.new("255.0.0.0/8")
ip2 = IPAddr.new("127.0.0.1")
assert_equal "255.0.0.0/8", type.type_cast_for_database(ip)
assert_equal "127.0.0.1/32", type.type_cast_for_database(ip2)
assert_equal "255.0.0.0/8", type.serialize(ip)
assert_equal "127.0.0.1/32", type.serialize(ip2)
end
test "casting does nothing with non-IPAddr objects" do
type = OID::Cidr.new
assert_equal "foo", type.type_cast_for_database("foo")
assert_equal "foo", type.serialize("foo")
end
end
end

View file

@ -93,7 +93,7 @@ class PostgresqlCompositeWithCustomOIDTest < ActiveRecord::TestCase
value
end
def type_cast_for_database(value)
def serialize(value)
return if value.nil?
"(#{value.city},#{value.street})"
end

View file

@ -165,19 +165,19 @@ if ActiveRecord::Base.connection.supports_extensions?
end
def test_gen1
assert_equal(%q(" "=>""), @type.type_cast_for_database({' '=>''}))
assert_equal(%q(" "=>""), @type.serialize({' '=>''}))
end
def test_gen2
assert_equal(%q(","=>""), @type.type_cast_for_database({','=>''}))
assert_equal(%q(","=>""), @type.serialize({','=>''}))
end
def test_gen3
assert_equal(%q("="=>""), @type.type_cast_for_database({'='=>''}))
assert_equal(%q("="=>""), @type.serialize({'='=>''}))
end
def test_gen4
assert_equal(%q(">"=>""), @type.type_cast_for_database({'>'=>''}))
assert_equal(%q(">"=>""), @type.serialize({'>'=>''}))
end
def test_parse1

View file

@ -30,13 +30,13 @@ module ActiveRecord
def test_quote_range
range = "1,2]'; SELECT * FROM users; --".."a"
type = OID::Range.new(Type::Integer.new, :int8range)
assert_equal "'[1,0]'", @conn.quote(type.type_cast_for_database(range))
assert_equal "'[1,0]'", @conn.quote(type.serialize(range))
end
def test_quote_bit_string
value = "'); SELECT * FROM users; /*\n01\n*/--"
type = OID::Bit.new
assert_equal nil, @conn.quote(type.type_cast_for_database(value))
assert_equal nil, @conn.quote(type.serialize(value))
end
end
end

View file

@ -18,8 +18,8 @@ class PostgresqlTypeLookupTest < ActiveRecord::TestCase
bigint_array = @connection.type_map.lookup(1016, -1, "bigint[]")
big_array = [123456789123456789]
assert_raises(RangeError) { int_array.type_cast_for_database(big_array) }
assert_equal "{123456789123456789}", bigint_array.type_cast_for_database(big_array)
assert_raises(RangeError) { int_array.serialize(big_array) }
assert_equal "{123456789123456789}", bigint_array.serialize(big_array)
end
test "range types correctly respect registration of subtypes" do
@ -27,7 +27,7 @@ class PostgresqlTypeLookupTest < ActiveRecord::TestCase
bigint_range = @connection.type_map.lookup(3926, -1, "int8range")
big_range = 0..123456789123456789
assert_raises(RangeError) { int_range.type_cast_for_database(big_range) }
assert_equal "[0,123456789123456789]", bigint_range.type_cast_for_database(big_range)
assert_raises(RangeError) { int_range.serialize(big_range) }
assert_equal "[0,123456789123456789]", bigint_range.serialize(big_range)
end
end

View file

@ -87,7 +87,7 @@ module ActiveRecord
value = "hello".encode('ascii-8bit')
type = Type::String.new
assert_equal "'hello'", @conn.quote(type.type_cast_for_database(value))
assert_equal "'hello'", @conn.quote(type.serialize(value))
end
end
end

View file

@ -59,22 +59,22 @@ module ActiveRecord
test "from_database + read_for_database type casts to and from database" do
@type.expect(:deserialize, 'read from database', ['whatever'])
@type.expect(:type_cast_for_database, 'ready for database', ['read from database'])
@type.expect(:serialize, 'ready for database', ['read from database'])
attribute = Attribute.from_database(nil, 'whatever', @type)
type_cast_for_database = attribute.value_for_database
serialize = attribute.value_for_database
assert_equal 'ready for database', type_cast_for_database
assert_equal 'ready for database', serialize
end
test "from_user + read_for_database type casts from the user to the database" do
@type.expect(:type_cast_from_user, 'read from user', ['whatever'])
@type.expect(:type_cast_for_database, 'ready for database', ['read from user'])
@type.expect(:serialize, 'ready for database', ['read from user'])
attribute = Attribute.from_user(nil, 'whatever', @type)
type_cast_for_database = attribute.value_for_database
serialize = attribute.value_for_database
assert_equal 'ready for database', type_cast_for_database
assert_equal 'ready for database', serialize
end
test "duping dups the value" do

View file

@ -623,13 +623,13 @@ class DirtyTest < ActiveRecord::TestCase
end
end
test "defaults with type that implements `type_cast_for_database`" do
test "defaults with type that implements `serialize`" do
type = Class.new(ActiveRecord::Type::Value) do
def type_cast(value)
value.to_i
end
def type_cast_for_database(value)
def serialize(value)
value.to_s
end
end

View file

@ -94,7 +94,7 @@ class ReflectionTest < ActiveRecord::TestCase
assert_equal object, type.deserialize(object)
assert_equal object, type.type_cast_from_user(object)
assert_equal object, type.type_cast_for_database(object)
assert_equal object, type.serialize(object)
end
def test_reflection_klass_for_nested_class_name

View file

@ -259,7 +259,7 @@ module ActiveRecord
"type cast from database"
end
def type_cast_for_database(value)
def serialize(value)
raise value unless value == "value from user"
"type cast for database"
end

View file

@ -43,8 +43,8 @@ module ActiveRecord
test "casting booleans for database" do
type = Type::Integer.new
assert_equal 1, type.type_cast_for_database(true)
assert_equal 0, type.type_cast_for_database(false)
assert_equal 1, type.serialize(true)
assert_equal 0, type.serialize(false)
end
test "changed?" do
@ -60,53 +60,53 @@ module ActiveRecord
test "values below int min value are out of range" do
assert_raises(::RangeError) do
Integer.new.type_cast_for_database(-2147483649)
Integer.new.serialize(-2147483649)
end
end
test "values above int max value are out of range" do
assert_raises(::RangeError) do
Integer.new.type_cast_for_database(2147483648)
Integer.new.serialize(2147483648)
end
end
test "very small numbers are out of range" do
assert_raises(::RangeError) do
Integer.new.type_cast_for_database(-9999999999999999999999999999999)
Integer.new.serialize(-9999999999999999999999999999999)
end
end
test "very large numbers are out of range" do
assert_raises(::RangeError) do
Integer.new.type_cast_for_database(9999999999999999999999999999999)
Integer.new.serialize(9999999999999999999999999999999)
end
end
test "normal numbers are in range" do
type = Integer.new
assert_equal(0, type.type_cast_for_database(0))
assert_equal(-1, type.type_cast_for_database(-1))
assert_equal(1, type.type_cast_for_database(1))
assert_equal(0, type.serialize(0))
assert_equal(-1, type.serialize(-1))
assert_equal(1, type.serialize(1))
end
test "int max value is in range" do
assert_equal(2147483647, Integer.new.type_cast_for_database(2147483647))
assert_equal(2147483647, Integer.new.serialize(2147483647))
end
test "int min value is in range" do
assert_equal(-2147483648, Integer.new.type_cast_for_database(-2147483648))
assert_equal(-2147483648, Integer.new.serialize(-2147483648))
end
test "columns with a larger limit have larger ranges" do
type = Integer.new(limit: 8)
assert_equal(9223372036854775807, type.type_cast_for_database(9223372036854775807))
assert_equal(-9223372036854775808, type.type_cast_for_database(-9223372036854775808))
assert_equal(9223372036854775807, type.serialize(9223372036854775807))
assert_equal(-9223372036854775808, type.serialize(-9223372036854775808))
assert_raises(::RangeError) do
type.type_cast_for_database(-9999999999999999999999999999999)
type.serialize(-9999999999999999999999999999999)
end
assert_raises(::RangeError) do
type.type_cast_for_database(9999999999999999999999999999999)
type.serialize(9999999999999999999999999999999)
end
end

View file

@ -4,12 +4,12 @@ module ActiveRecord
module Type
class UnsignedIntegerTest < ActiveRecord::TestCase
test "unsigned int max value is in range" do
assert_equal(4294967295, UnsignedInteger.new.type_cast_for_database(4294967295))
assert_equal(4294967295, UnsignedInteger.new.serialize(4294967295))
end
test "minus value is out of range" do
assert_raises(::RangeError) do
UnsignedInteger.new.type_cast_for_database(-1)
UnsignedInteger.new.serialize(-1)
end
end
end

View file

@ -110,7 +110,7 @@ module ActiveRecord
def test_attributes_which_are_invalid_for_database_can_still_be_reassigned
type_which_cannot_go_to_the_database = Type::Value.new
def type_which_cannot_go_to_the_database.type_cast_for_database(*)
def type_which_cannot_go_to_the_database.serialize(*)
raise
end
klass = Class.new(ActiveRecord::Base) do