`type_cast_from_user` -> `cast`

This commit is contained in:
Sean Griffin 2015-02-17 13:39:42 -07:00
parent 1455c4c22f
commit 9ca6948f72
22 changed files with 105 additions and 105 deletions

View File

@ -63,7 +63,7 @@ module ActiveRecord
def ids_writer(ids)
pk_type = reflection.primary_key_type
ids = Array(ids).reject(&:blank?)
ids.map! { |i| pk_type.type_cast_from_user(i) }
ids.map! { |i| pk_type.cast(i) }
replace(klass.find(ids).index_by(&:id).values_at(*ids))
end

View File

@ -114,7 +114,7 @@ module ActiveRecord
class FromUser < Attribute # :nodoc:
def type_cast(value)
type.type_cast_from_user(value)
type.cast(value)
end
def came_from_user?

View File

@ -6,9 +6,9 @@ module ActiveRecord
convert_time_to_time_zone(super)
end
def type_cast_from_user(value)
def cast(value)
if value.is_a?(Array)
value.map { |v| type_cast_from_user(v) }
value.map { |v| cast(v) }
elsif value.is_a?(Hash)
set_time_zone_without_conversion(super)
elsif value.respond_to?(:in_time_zone)

View File

@ -102,14 +102,14 @@ 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
# +deserialize+ or +type_cast_from_user+ will be called on
# +deserialize+ or +cast+ 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
# from ActiveRecord::Type::Value
#
# class MoneyType < ActiveRecord::Type::Integer
# def type_cast_from_user(value)
# def cast(value)
# if value.include?('$')
# price_in_dollars = value.gsub(/\$/, '').to_f
# super(price_in_dollars * 100)
@ -144,7 +144,7 @@ module ActiveRecord
# end
#
# # value will be the result of +deserialize+ or
# # +type_cast_from_user+. Assumed to be in instance of +Money+ in
# # +cast+. Assumed to be in instance of +Money+ in
# # this case.
# def serialize(value)
# value_in_bitcoins = @currency_converter.convert_to_bitcoins(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 +deserialize+.
# +cast+ or +deserialize+.
def define_attribute(
name,
cast_type,

View File

@ -33,11 +33,11 @@ module ActiveRecord
end
end
def type_cast_from_user(value)
def cast(value)
if value.is_a?(::String)
value = parse_pg_array(value)
end
type_cast_array(value, :type_cast_from_user)
type_cast_array(value, :cast)
end
def serialize(value)

View File

@ -93,7 +93,7 @@ module ActiveRecord
@mapping = mapping
end
def type_cast_from_user(value)
def cast(value)
return if value.blank?
if mapping.has_key?(value)

View File

@ -521,7 +521,7 @@ module ActiveRecord
# Determines if a hash contains a truthy _destroy key.
def has_destroy_flag?(hash)
Type::Boolean.new.type_cast_from_user(hash['_destroy'])
Type::Boolean.new.cast(hash['_destroy'])
end
# Determines if a new record should be rejected by checking

View File

@ -3,7 +3,7 @@ module ActiveRecord
module Helpers
class AcceptsMultiparameterTime < Module # :nodoc:
def initialize(defaults: {})
define_method(:type_cast_from_user) do |value|
define_method(:cast) do |value|
if value.is_a?(Hash)
value_from_multiparameter_assignment(value)
else

View File

@ -2,7 +2,7 @@ module ActiveRecord
module Type
module Helpers
module Mutable # :nodoc:
def type_cast_from_user(value)
def cast(value)
deserialize(serialize(value))
end

View File

@ -32,7 +32,7 @@ module ActiveRecord
# Value#type_cast and Value#cast_value.
#
# +value+ The raw input, as provided to the attribute setter.
def type_cast_from_user(value)
def cast(value)
type_cast(value)
end
@ -94,7 +94,7 @@ module ActiveRecord
private
# Convenience method. If you don't need separate behavior for
# Value#deserialize and Value#type_cast_from_user, you can override
# 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:

View File

@ -89,7 +89,7 @@ class PostgresqlCompositeWithCustomOIDTest < ActiveRecord::TestCase
end
end
def type_cast_from_user(value)
def cast(value)
value
end

View File

@ -47,10 +47,10 @@ class PostgresqlMoneyTest < ActiveRecord::TestCase
def test_money_type_cast
type = PostgresqlMoney.type_for_attribute('wealth')
assert_equal(12345678.12, type.type_cast_from_user("$12,345,678.12"))
assert_equal(12345678.12, type.type_cast_from_user("$12.345.678,12"))
assert_equal(-1.15, type.type_cast_from_user("-$1.15"))
assert_equal(-2.25, type.type_cast_from_user("($2.25)"))
assert_equal(12345678.12, type.cast("$12,345,678.12"))
assert_equal(12345678.12, type.cast("$12.345.678,12"))
assert_equal(-1.15, type.cast("-$1.15"))
assert_equal(-2.25, type.cast("($2.25)"))
end
def test_schema_dumping

View File

@ -12,11 +12,11 @@ module ActiveRecord
super(delegate)
end
def type_cast_from_user(value)
def cast(value)
"#{super} #{@decoration}"
end
alias deserialize type_cast_from_user
alias deserialize cast
end
setup do
@ -102,11 +102,11 @@ module ActiveRecord
end
class Multiplier < SimpleDelegator
def type_cast_from_user(value)
def cast(value)
return if value.nil?
value * 2
end
alias deserialize type_cast_from_user
alias deserialize cast
end
test "decorating with a proc" do

View File

@ -151,7 +151,7 @@ module ActiveRecord
end
class MyType
def type_cast_from_user(value)
def cast(value)
return if value.nil?
value + " from user"
end

View File

@ -22,7 +22,7 @@ module ActiveRecord
end
test "from_user + read type casts from user" do
@type.expect(:type_cast_from_user, 'type cast from user', ['a value'])
@type.expect(:cast, 'type cast from user', ['a value'])
attribute = Attribute.from_user(nil, 'a value', @type)
type_cast_value = attribute.value
@ -68,7 +68,7 @@ module ActiveRecord
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(:cast, 'read from user', ['whatever'])
@type.expect(:serialize, 'ready for database', ['read from user'])
attribute = Attribute.from_user(nil, 'whatever', @type)
@ -102,7 +102,7 @@ module ActiveRecord
end
class MyType
def type_cast_from_user(value)
def cast(value)
value + " from user"
end

View File

@ -108,7 +108,7 @@ module ActiveRecord
test "the given default value is cast from user" do
custom_type = Class.new(Type::Value) do
def type_cast_from_user(*)
def cast(*)
"from user"
end

View File

@ -90,7 +90,7 @@ module ActiveRecord
cast_type = @connection.type_map.lookup(type)
assert_equal :decimal, cast_type.type
assert_equal 2, cast_type.type_cast_from_user(2.1)
assert_equal 2, cast_type.cast(2.1)
end
end

View File

@ -93,7 +93,7 @@ class ReflectionTest < ActiveRecord::TestCase
object = Object.new
assert_equal object, type.deserialize(object)
assert_equal object, type.type_cast_from_user(object)
assert_equal object, type.cast(object)
assert_equal object, type.serialize(object)
end

View File

@ -5,29 +5,29 @@ module ActiveRecord
class DecimalTest < ActiveRecord::TestCase
def test_type_cast_decimal
type = Decimal.new
assert_equal BigDecimal.new("0"), type.type_cast_from_user(BigDecimal.new("0"))
assert_equal BigDecimal.new("123"), type.type_cast_from_user(123.0)
assert_equal BigDecimal.new("1"), type.type_cast_from_user(:"1")
assert_equal BigDecimal.new("0"), type.cast(BigDecimal.new("0"))
assert_equal BigDecimal.new("123"), type.cast(123.0)
assert_equal BigDecimal.new("1"), type.cast(:"1")
end
def test_type_cast_decimal_from_float_with_large_precision
type = Decimal.new(precision: ::Float::DIG + 2)
assert_equal BigDecimal.new("123.0"), type.type_cast_from_user(123.0)
assert_equal BigDecimal.new("123.0"), type.cast(123.0)
end
def test_type_cast_from_float_with_unspecified_precision
type = Decimal.new
assert_equal 22.68.to_d, type.type_cast_from_user(22.68)
assert_equal 22.68.to_d, type.cast(22.68)
end
def test_type_cast_decimal_from_rational_with_precision
type = Decimal.new(precision: 2)
assert_equal BigDecimal("0.33"), type.type_cast_from_user(Rational(1, 3))
assert_equal BigDecimal("0.33"), type.cast(Rational(1, 3))
end
def test_type_cast_decimal_from_rational_without_precision_defaults_to_18_36
type = Decimal.new
assert_equal BigDecimal("0.333333333333333333E0"), type.type_cast_from_user(Rational(1, 3))
assert_equal BigDecimal("0.333333333333333333E0"), type.cast(Rational(1, 3))
end
def test_type_cast_decimal_from_object_responding_to_d
@ -36,7 +36,7 @@ module ActiveRecord
BigDecimal.new("1")
end
type = Decimal.new
assert_equal BigDecimal("1"), type.type_cast_from_user(value)
assert_equal BigDecimal("1"), type.cast(value)
end
def test_changed?

View File

@ -6,39 +6,39 @@ module ActiveRecord
class IntegerTest < ActiveRecord::TestCase
test "simple values" do
type = Type::Integer.new
assert_equal 1, type.type_cast_from_user(1)
assert_equal 1, type.type_cast_from_user('1')
assert_equal 1, type.type_cast_from_user('1ignore')
assert_equal 0, type.type_cast_from_user('bad1')
assert_equal 0, type.type_cast_from_user('bad')
assert_equal 1, type.type_cast_from_user(1.7)
assert_equal 0, type.type_cast_from_user(false)
assert_equal 1, type.type_cast_from_user(true)
assert_nil type.type_cast_from_user(nil)
assert_equal 1, type.cast(1)
assert_equal 1, type.cast('1')
assert_equal 1, type.cast('1ignore')
assert_equal 0, type.cast('bad1')
assert_equal 0, type.cast('bad')
assert_equal 1, type.cast(1.7)
assert_equal 0, type.cast(false)
assert_equal 1, type.cast(true)
assert_nil type.cast(nil)
end
test "random objects cast to nil" do
type = Type::Integer.new
assert_nil type.type_cast_from_user([1,2])
assert_nil type.type_cast_from_user({1 => 2})
assert_nil type.type_cast_from_user((1..2))
assert_nil type.cast([1,2])
assert_nil type.cast({1 => 2})
assert_nil type.cast((1..2))
end
test "casting ActiveRecord models" do
type = Type::Integer.new
firm = Firm.create(:name => 'Apple')
assert_nil type.type_cast_from_user(firm)
assert_nil type.cast(firm)
end
test "casting objects without to_i" do
type = Type::Integer.new
assert_nil type.type_cast_from_user(::Object.new)
assert_nil type.cast(::Object.new)
end
test "casting nan and infinity" do
type = Type::Integer.new
assert_nil type.type_cast_from_user(::Float::NAN)
assert_nil type.type_cast_from_user(1.0/0.0)
assert_nil type.cast(::Float::NAN)
assert_nil type.cast(1.0/0.0)
end
test "casting booleans for database" do

View File

@ -4,15 +4,15 @@ module ActiveRecord
class StringTypeTest < ActiveRecord::TestCase
test "type casting" do
type = Type::String.new
assert_equal "t", type.type_cast_from_user(true)
assert_equal "f", type.type_cast_from_user(false)
assert_equal "123", type.type_cast_from_user(123)
assert_equal "t", type.cast(true)
assert_equal "f", type.cast(false)
assert_equal "123", type.cast(123)
end
test "values are duped coming out" do
s = "foo"
type = Type::String.new
assert_not_same s, type.type_cast_from_user(s)
assert_not_same s, type.cast(s)
assert_not_same s, type.deserialize(s)
end

View File

@ -5,38 +5,38 @@ module ActiveRecord
class TypesTest < ActiveRecord::TestCase
def test_type_cast_boolean
type = Type::Boolean.new
assert type.type_cast_from_user('').nil?
assert type.type_cast_from_user(nil).nil?
assert type.cast('').nil?
assert type.cast(nil).nil?
assert type.type_cast_from_user(true)
assert type.type_cast_from_user(1)
assert type.type_cast_from_user('1')
assert type.type_cast_from_user('t')
assert type.type_cast_from_user('T')
assert type.type_cast_from_user('true')
assert type.type_cast_from_user('TRUE')
assert type.type_cast_from_user('on')
assert type.type_cast_from_user('ON')
assert type.type_cast_from_user(' ')
assert type.type_cast_from_user("\u3000\r\n")
assert type.type_cast_from_user("\u0000")
assert type.type_cast_from_user('SOMETHING RANDOM')
assert type.cast(true)
assert type.cast(1)
assert type.cast('1')
assert type.cast('t')
assert type.cast('T')
assert type.cast('true')
assert type.cast('TRUE')
assert type.cast('on')
assert type.cast('ON')
assert type.cast(' ')
assert type.cast("\u3000\r\n")
assert type.cast("\u0000")
assert type.cast('SOMETHING RANDOM')
# explicitly check for false vs nil
assert_equal false, type.type_cast_from_user(false)
assert_equal false, type.type_cast_from_user(0)
assert_equal false, type.type_cast_from_user('0')
assert_equal false, type.type_cast_from_user('f')
assert_equal false, type.type_cast_from_user('F')
assert_equal false, type.type_cast_from_user('false')
assert_equal false, type.type_cast_from_user('FALSE')
assert_equal false, type.type_cast_from_user('off')
assert_equal false, type.type_cast_from_user('OFF')
assert_equal false, type.cast(false)
assert_equal false, type.cast(0)
assert_equal false, type.cast('0')
assert_equal false, type.cast('f')
assert_equal false, type.cast('F')
assert_equal false, type.cast('false')
assert_equal false, type.cast('FALSE')
assert_equal false, type.cast('off')
assert_equal false, type.cast('OFF')
end
def test_type_cast_float
type = Type::Float.new
assert_equal 1.0, type.type_cast_from_user("1")
assert_equal 1.0, type.cast("1")
end
def test_changing_float
@ -50,54 +50,54 @@ module ActiveRecord
def test_type_cast_binary
type = Type::Binary.new
assert_equal nil, type.type_cast_from_user(nil)
assert_equal "1", type.type_cast_from_user("1")
assert_equal 1, type.type_cast_from_user(1)
assert_equal nil, type.cast(nil)
assert_equal "1", type.cast("1")
assert_equal 1, type.cast(1)
end
def test_type_cast_time
type = Type::Time.new
assert_equal nil, type.type_cast_from_user(nil)
assert_equal nil, type.type_cast_from_user('')
assert_equal nil, type.type_cast_from_user('ABC')
assert_equal nil, type.cast(nil)
assert_equal nil, type.cast('')
assert_equal nil, type.cast('ABC')
time_string = Time.now.utc.strftime("%T")
assert_equal time_string, type.type_cast_from_user(time_string).strftime("%T")
assert_equal time_string, type.cast(time_string).strftime("%T")
end
def test_type_cast_datetime_and_timestamp
type = Type::DateTime.new
assert_equal nil, type.type_cast_from_user(nil)
assert_equal nil, type.type_cast_from_user('')
assert_equal nil, type.type_cast_from_user(' ')
assert_equal nil, type.type_cast_from_user('ABC')
assert_equal nil, type.cast(nil)
assert_equal nil, type.cast('')
assert_equal nil, type.cast(' ')
assert_equal nil, type.cast('ABC')
datetime_string = Time.now.utc.strftime("%FT%T")
assert_equal datetime_string, type.type_cast_from_user(datetime_string).strftime("%FT%T")
assert_equal datetime_string, type.cast(datetime_string).strftime("%FT%T")
end
def test_type_cast_date
type = Type::Date.new
assert_equal nil, type.type_cast_from_user(nil)
assert_equal nil, type.type_cast_from_user('')
assert_equal nil, type.type_cast_from_user(' ')
assert_equal nil, type.type_cast_from_user('ABC')
assert_equal nil, type.cast(nil)
assert_equal nil, type.cast('')
assert_equal nil, type.cast(' ')
assert_equal nil, type.cast('ABC')
date_string = Time.now.utc.strftime("%F")
assert_equal date_string, type.type_cast_from_user(date_string).strftime("%F")
assert_equal date_string, type.cast(date_string).strftime("%F")
end
def test_type_cast_duration_to_integer
type = Type::Integer.new
assert_equal 1800, type.type_cast_from_user(30.minutes)
assert_equal 7200, type.type_cast_from_user(2.hours)
assert_equal 1800, type.cast(30.minutes)
assert_equal 7200, type.cast(2.hours)
end
def test_string_to_time_with_timezone
[:utc, :local].each do |zone|
with_timezone_config default: zone do
type = Type::DateTime.new
assert_equal Time.utc(2013, 9, 4, 0, 0, 0), type.type_cast_from_user("Wed, 04 Sep 2013 03:00:00 EAT")
assert_equal Time.utc(2013, 9, 4, 0, 0, 0), type.cast("Wed, 04 Sep 2013 03:00:00 EAT")
end
end
end