diff options
Diffstat (limited to 'activerecord/lib/active_record/type/value.rb')
-rw-r--r-- | activerecord/lib/active_record/type/value.rb | 32 |
1 files changed, 12 insertions, 20 deletions
diff --git a/activerecord/lib/active_record/type/value.rb b/activerecord/lib/active_record/type/value.rb index 7338920f3b..fc3ef5e83b 100644 --- a/activerecord/lib/active_record/type/value.rb +++ b/activerecord/lib/active_record/type/value.rb @@ -14,12 +14,12 @@ module ActiveRecord # 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 type_cast_from_database(value) - type_cast(value) + def deserialize(value) + cast(value) end # Type casts a value from user input (e.g. from a setter). This value may @@ -29,18 +29,18 @@ module ActiveRecord # # 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 type_cast_from_user(value) - type_cast(value) + def cast(value) + cast_value(value) unless value.nil? end # Cast a value from the ruby type to a type that the database knows how # 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,16 +68,16 @@ 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 # - # - 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) @@ -93,16 +93,8 @@ 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 - # 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 |