
[Patch] Adds serialize to Base
Reported by Adrian Madrid | November 14th, 2007 @ 10:52 PM
Adds a method that allows to save arbitratry Ruby objects into the database transparently. The idea was stolen from AR but the implementation is original (?).
Example:
class Serializer < DataMapper::Base
property :name, :string
property :content, :text, :lazy => false
serialize :content
end
Comments and changes to this ticket
-
Josh H November 15th, 2007 @ 01:52 AM
How about:
class Serializer < DataMapper::Base property :name, :string property :content, :serialize, :lazy => false end
or
class Serializer < DataMapper::Base property :name, :string property :content, :text, :serialize => true, :lazy => false end
-
Adrian Madrid November 15th, 2007 @ 09:16 AM
That would be nice too although I'm not quite sure how to add those options to property. Any pointers?
AEM
-
Sam Smoot November 15th, 2007 @ 10:41 AM
- State changed from new to open
Adrian: I think I'd prefer something like:
class Serializer < DataMapper::Base property :name, :string property :content, :object, :lazy => false end
Seems clean. Both DO.rb and DM would be modified. In coersion.rb for DM, and in Command#quote_value in DO.rb.
Thanks for the patch!
-
Adrian Madrid November 15th, 2007 @ 10:53 AM
Sam:
I'd look into it. the way Sequel does it is pretty interesting. the implement a transform array of procs [1] for the desired methods so you can choose which serializer to use (YAML/Marshall/etc). I might be out of my league in playing with DO and DM at the same time but I will give it a try.
Adrian Madrid
[1] Sequel:
sequel/base.rb, self.serialize, line 63
sequel/dataset.rb, transform, line 279
-
Adrian Madrid November 15th, 2007 @ 02:26 PM
Sam,
I spent some time looking at the code in those two files and coersion.rb seems pretty straight forward to change:
def type_cast_object(raw_value) return nil if raw_value.blank? begin if raw_value.responds_to? :to_yaml raw_value.to_yaml.freeze else YAML.dump(raw_value).freeze end rescue raise CoersionError.new("Can't type-cast #{raw_value.inspect} to an object") end end def type_cast_value(type, raw_value) return nil if raw_value.blank? case type when :string then type_cast_string(raw_value) when :text then type_cast_text(raw_value) when :boolean then type_cast_boolean(raw_value) when :class then type_cast_class(raw_value) when :integer then type_cast_integer(raw_value) when :decimal then type_cast_decimal(raw_value) when :float then type_cast_float(raw_value) when :datetime then type_cast_datetime(raw_value) when :date then type_cast_date(raw_value) when :object then type_cast_object(raw_value) else if respond_to?("type_cast_#{type}") send("type_cast_#{type}", raw_value) else raise "Don't know how to type-cast #{{ type => raw_value }.inspect }" end end end
But on the do.rb side I'm confused. It seems to me that it would be a performance penalty to work it out there since I don't know which columns should be yaml and I would have to check every string to see if it is yaml or just a plain string. Am I missing something?
Sincerely,
Adrian Madrid
-
Adrian Madrid November 15th, 2007 @ 09:10 PM
Sam,
I have implemented as discussed in irc the changes to my previous patch.
class Serializer < DataMapper::Base property :content, :object, :lazy => false end
I have simplified and updated the specs as well and they are running perfectly against trunk.
Please let me know if this will work.
Sincerely,
Adrian Madrid
-
Adrian Madrid November 16th, 2007 @ 11:28 PM
Sam,
Thanks for the commit rights. I have committed my changes and retested on a fresh svn and it is passing.
Thanks again,
Adrian Madrid
-
Sam Smoot December 17th, 2007 @ 06:33 PM
- State changed from open to resolved
Please Sign in or create a free account to add a new ticket.
With your very own profile, you can contribute to projects, track your activity, watch tickets, receive and update tickets through your email and much more.
Create your profile
Help contribute to this project by taking a few moments to create your personal profile. Create your profile »