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

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

@ -133,7 +133,7 @@ module ClassMethods
#
# 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 ClassMethods
# # 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

@ -52,7 +52,7 @@ def type_cast(value, column = nil)
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 @@ def quote_table_name_for_assignment(table, attr)
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

@ -926,7 +926,7 @@ def create_table_definition(name, temporary = false, options = nil, as = nil) #
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"

@ -40,7 +40,7 @@ def type_cast_from_user(value)
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 @@ def cast_value_for_database(value)
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

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

@ -18,7 +18,7 @@ def type_cast_for_schema(value)
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

@ -21,7 +21,7 @@ def deserialize(value)
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

@ -17,7 +17,7 @@ def deserialize(value)
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

@ -13,7 +13,7 @@ def changed_in_place?(raw_old_value, new_value)
# 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

@ -23,7 +23,7 @@ def type_cast(value)
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

@ -30,7 +30,7 @@ def cast_value(value)
::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 @@ def type_cast_single(value)
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)

@ -5,7 +5,7 @@ module OID # :nodoc:
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

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

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

@ -75,7 +75,7 @@ def expand_hash_conditions_for_aggregates(attrs)
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

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

@ -10,7 +10,7 @@ def type
: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

@ -7,7 +7,7 @@ def type
:float
end
alias type_cast_for_database type_cast
alias serialize type_cast
private

@ -3,14 +3,14 @@ module Type
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

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

@ -19,7 +19,7 @@ def deserialize(value)
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 @@ def type_cast_for_database(value)
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

@ -11,7 +11,7 @@ def changed_in_place?(raw_old_value, new_value)
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)

@ -40,7 +40,7 @@ def type_cast_from_user(value)
# 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 @@ def changed?(old_value, new_value, _new_value_before_type_cast)
# 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

@ -8,7 +8,7 @@ def initialize(types)
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

@ -61,7 +61,7 @@ def build_relation(klass, table, attribute, value) #:nodoc:
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]

@ -206,7 +206,7 @@ def test_string_quoting_rules_match_pg_behavior
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 @@ def test_quoting_non_standard_delimiters
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

@ -10,14 +10,14 @@ class CidrTest < ActiveRecord::TestCase
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

@ -93,7 +93,7 @@ def type_cast_from_user(value)
value
end
def type_cast_for_database(value)
def serialize(value)
return if value.nil?
"(#{value.city},#{value.street})"
end

@ -165,19 +165,19 @@ def test_changes_in_place
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

@ -30,13 +30,13 @@ def test_quote_float_infinity
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

@ -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

@ -87,7 +87,7 @@ def test_quoting_binary_strings
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

@ -59,22 +59,22 @@ class AttributeTest < ActiveRecord::TestCase
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

@ -623,13 +623,13 @@ def test_datetime_attribute_doesnt_change_if_zone_is_modified_in_string
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

@ -94,7 +94,7 @@ def test_non_existent_types_are_identity_types
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

@ -259,7 +259,7 @@ def deserialize(value)
"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

@ -43,8 +43,8 @@ class IntegerTest < ActiveRecord::TestCase
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 @@ class IntegerTest < ActiveRecord::TestCase
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

@ -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

@ -110,7 +110,7 @@ def test_type_equality
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