Skip to content

Commit

Permalink
Rework yaml serialization to make it less obtrusive
Browse files Browse the repository at this point in the history
  • Loading branch information
bkeepers committed Nov 12, 2010
1 parent e090d3f commit cbde340
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 12 deletions.
2 changes: 1 addition & 1 deletion lib/delayed/backend/base.rb
Expand Up @@ -68,7 +68,7 @@ def name

def payload_object=(object)
@payload_object = object
self.handler = object.to_yaml
self.handler = object.to_delayed_yaml
end

def payload_object
Expand Down
4 changes: 2 additions & 2 deletions lib/delayed/backend/shared_spec.rb
Expand Up @@ -296,15 +296,15 @@ def create_job(opts = {})
describe "yaml serialization" do
it "should reload changed attributes" do
job = described_class.enqueue SimpleJob.new
yaml = job.to_yaml
yaml = job.to_delayed_yaml
job.priority = 99
job.save
YAML.load(yaml).priority.should == 99
end

it "should raise deserialization error for destroyed records" do
job = described_class.enqueue SimpleJob.new
yaml = job.to_yaml
yaml = job.to_delayed_yaml
job.destroy
lambda { YAML.load(yaml) }.should raise_error(Delayed::DeserializationError)
end
Expand Down
19 changes: 12 additions & 7 deletions lib/delayed/serialization/active_record.rb
@@ -1,13 +1,18 @@
class ActiveRecord::Base
yaml_as "tag:ruby.yaml.org,2002:ActiveRecord"
def to_delayed_yaml( opts = {} )
YAML::quick_emit( self, opts ) do |out|
out.map("tag:delayed_job.com,2010:ActiveRecord:#{self.class.name}", to_yaml_style) do |map|
map.add('id', id)
end
end
end
end

def self.yaml_new(klass, tag, val)
klass.find(val['attributes']['id'])
YAML.add_domain_type('delayed_job.com,2010', 'ActiveRecord') do |tag, value|
begin
type, model = YAML.read_type_class(tag, Kernel)
model.find(value['id'])
rescue ActiveRecord::RecordNotFound
raise Delayed::DeserializationError
end

def to_yaml_properties
['@attributes']
end
end
10 changes: 8 additions & 2 deletions lib/delayed/yaml_ext.rb
Expand Up @@ -15,14 +15,14 @@ def to_yaml( opts = {} )
out.scalar(taguri, self.name, :plain)
}
end

def yaml_tag_read_class(name)
# Constantize the object so that ActiveSupport can attempt
# its auto loading magic. Will raise LoadError if not successful.
name.constantize
name
end

end

class Class
Expand All @@ -38,3 +38,9 @@ def self.yaml_tag_read_class(name)
"Struct::#{ name }"
end
end

class Object
def to_delayed_yaml
to_yaml
end
end

0 comments on commit cbde340

Please sign in to comment.