aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/type
diff options
context:
space:
mode:
authorSean Griffin <sean@thoughtbot.com>2014-05-29 13:34:23 -0700
committerSean Griffin <sean@thoughtbot.com>2014-05-29 13:34:47 -0700
commit912904caee1e037e2f71d6ede35a3758c15f276d (patch)
tree2384c2a13db76ae767f96e114870178fb93debb1 /activerecord/lib/active_record/type
parent214423f40171bf7d3165d70507ab17803b2fbf07 (diff)
downloadrails-912904caee1e037e2f71d6ede35a3758c15f276d.tar.gz
rails-912904caee1e037e2f71d6ede35a3758c15f276d.tar.bz2
rails-912904caee1e037e2f71d6ede35a3758c15f276d.zip
Move `type_cast_for_write` behavior over to the serialized type object
Diffstat (limited to 'activerecord/lib/active_record/type')
-rw-r--r--activerecord/lib/active_record/type/serialized.rb35
-rw-r--r--activerecord/lib/active_record/type/value.rb11
2 files changed, 38 insertions, 8 deletions
diff --git a/activerecord/lib/active_record/type/serialized.rb b/activerecord/lib/active_record/type/serialized.rb
index cc7513ca2a..4052ac0fa0 100644
--- a/activerecord/lib/active_record/type/serialized.rb
+++ b/activerecord/lib/active_record/type/serialized.rb
@@ -1,11 +1,12 @@
module ActiveRecord
module Type
class Serialized < SimpleDelegator # :nodoc:
- attr_reader :subtype
+ attr_reader :subtype, :coder
- def initialize(subtype)
+ def initialize(subtype, coder)
@subtype = subtype
- super
+ @coder = coder
+ super(subtype)
end
def type_cast(value)
@@ -16,6 +17,14 @@ module ActiveRecord
end
end
+ def type_cast_for_write(value)
+ Attribute.new(coder, value, :unserialized)
+ end
+
+ def raw_type_cast_for_write(value)
+ Attribute.new(coder, value, :serialized)
+ end
+
def serialized?
true
end
@@ -23,6 +32,26 @@ module ActiveRecord
def accessor
ActiveRecord::Store::IndifferentHashAccessor
end
+
+ class Attribute < Struct.new(:coder, :value, :state) # :nodoc:
+ def unserialized_value(v = value)
+ state == :serialized ? unserialize(v) : value
+ end
+
+ def serialized_value
+ state == :unserialized ? serialize : value
+ end
+
+ def unserialize(v)
+ self.state = :unserialized
+ self.value = coder.load(v)
+ end
+
+ def serialize
+ self.state = :serialized
+ self.value = coder.dump(value)
+ end
+ end
end
end
end
diff --git a/activerecord/lib/active_record/type/value.rb b/activerecord/lib/active_record/type/value.rb
index a5493be8f2..9a4adc60cc 100644
--- a/activerecord/lib/active_record/type/value.rb
+++ b/activerecord/lib/active_record/type/value.rb
@@ -23,10 +23,6 @@ module ActiveRecord
cast_value(value) unless value.nil?
end
- def type_cast_for_write(value)
- value
- end
-
def type_cast_for_database(value)
type_cast_for_write(value)
end
@@ -47,10 +43,15 @@ module ActiveRecord
false
end
- def klass
+ def klass # :nodoc:
::Object
end
+ def type_cast_for_write(value) # :nodoc:
+ value
+ end
+ alias_method :raw_type_cast_for_write, :type_cast_for_write # :internal:
+
private
# Responsible for casting values from external sources to the appropriate