Skip to content

Commit

Permalink
Rm Type#type_cast
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
sgrif committed Feb 17, 2015
1 parent 9ca6948 commit ad127d8
Show file tree
Hide file tree
Showing 13 changed files with 20 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ def type
:bit
end

def type_cast(value)
def cast(value)
if ::String === value
case value
when /^0x/i
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ def type
:enum
end

def type_cast(value)
def cast(value)
value.to_s
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ def deserialize(value)
if value.is_a?(::String)
::ActiveSupport::JSON.decode(value) rescue nil
else
super
value
end
end

def serialize(value)
if value.is_a?(::Array) || value.is_a?(::Hash)
::ActiveSupport::JSON.encode(value)
else
super
value
end
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ def type
: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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def type
:uuid
end

def type_cast(value)
def cast(value)
value.to_s[ACCEPTABLE_UUID, 0]
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def initialize(delim, subtype)
# 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
Expand Down
2 changes: 1 addition & 1 deletion activerecord/lib/active_record/type/binary.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def binary?
true
end

def type_cast(value)
def cast(value)
if value.is_a?(Data)
value.to_s
else
Expand Down
2 changes: 1 addition & 1 deletion activerecord/lib/active_record/type/float.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ def type
:float
end

alias serialize type_cast
alias serialize cast

private

Expand Down
2 changes: 1 addition & 1 deletion activerecord/lib/active_record/type/helpers/numeric.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion activerecord/lib/active_record/type/integer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def deserialize(value)
end

def serialize(value)
result = type_cast(value)
result = cast(value)
if result
ensure_in_range(result)
end
Expand Down
20 changes: 6 additions & 14 deletions activerecord/lib/active_record/type/value.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ def type # :nodoc:

# 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
Expand All @@ -29,11 +29,11 @@ def deserialize(value)
#
# 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
Expand Down Expand Up @@ -93,16 +93,8 @@ def ==(other)

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
Expand Down
2 changes: 1 addition & 1 deletion activerecord/test/cases/base_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1427,7 +1427,7 @@ def test_column_types_typecast
attrs.delete 'id'

typecast = Class.new(ActiveRecord::Type::Value) {
def type_cast value
def cast value
"t.lo"
end
}
Expand Down
2 changes: 1 addition & 1 deletion activerecord/test/cases/dirty_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -625,7 +625,7 @@ def test_datetime_attribute_doesnt_change_if_zone_is_modified_in_string

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

Expand Down

0 comments on commit ad127d8

Please sign in to comment.