Skip to content

Commit

Permalink
specs and tweaks for yaml serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
bkeepers committed May 4, 2010
1 parent e22ac4e commit 0885b19
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 4 deletions.
2 changes: 2 additions & 0 deletions lib/delayed/backend/active_record.rb
Expand Up @@ -5,6 +5,8 @@ class ActiveRecord::Base

def self.yaml_new(klass, tag, val)
klass.find(val['attributes']['id'])
rescue ActiveRecord::RecordNotFound
nil
end

def to_yaml_properties
Expand Down
13 changes: 12 additions & 1 deletion lib/delayed/backend/data_mapper.rb
@@ -1,7 +1,18 @@
require 'dm-core'
require 'dm-observer'
require 'dm-aggregates'
require 'dm-serializer'

DataMapper::Resource.class_eval do
yaml_as "tag:ruby.yaml.org,2002:DataMapper"

def self.yaml_new(klass, tag, val)
klass.find(val['id'])
end

def to_yaml_properties
['@id']
end
end

module Delayed
module Backend
Expand Down
4 changes: 1 addition & 3 deletions lib/delayed/backend/mongo_mapper.rb
Expand Up @@ -4,9 +4,7 @@
yaml_as "tag:ruby.yaml.org,2002:MongoMapper"

def self.yaml_new(klass, tag, val)
klass.find!(val['_id'])
rescue MongoMapper::DocumentNotFound
nil
klass.find(val['_id'])
end

def to_yaml_properties
Expand Down
18 changes: 18 additions & 0 deletions spec/backend/shared_backend_spec.rb
Expand Up @@ -270,4 +270,22 @@ def create_job(opts = {})
@job.id.should_not be_nil
end
end

describe "yaml serialization" do
it "should reload changed attributes" do
job = @backend.enqueue SimpleJob.new
yaml = job.to_yaml
job.priority = 99
job.save
YAML.load(yaml).priority.should == 99
end

it "should ignore destroyed records" do
job = @backend.enqueue SimpleJob.new
yaml = job.to_yaml
job.destroy
lambda { YAML.load(yaml).should be_nil }.should_not raise_error
end
end

end

0 comments on commit 0885b19

Please sign in to comment.