delayed_job_shallow_mongoid 0.2.6 → 0.2.7

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.6
1
+ 0.2.7
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "delayed_job_shallow_mongoid"
8
- s.version = "0.2.6"
8
+ s.version = "0.2.7"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Joey Aghion"]
@@ -16,7 +16,7 @@ module Delayed
16
16
 
17
17
  def display_name
18
18
  if object.is_a?(ShallowMongoid::DocumentStub)
19
- return "#{object.klass}[#{object.id}]##{method_name}"
19
+ "#{object.description}##{method_name}"
20
20
  else
21
21
  "#{object.class}##{method_name}"
22
22
  end
@@ -1,18 +1,32 @@
1
1
  module Delayed
2
2
  module ShallowMongoid
3
3
  def self.dump(arg)
4
- if arg.is_a?(::Mongoid::Document) && !arg.embedded?
5
- ShallowMongoid::DocumentStub.new(arg.class, arg._id.to_s)
4
+ return arg unless arg.is_a?(::Mongoid::Document)
5
+ if arg.embedded?
6
+ ShallowMongoid::DocumentStub.new(arg._root.class, arg._root._id.to_s, selector_from(arg))
6
7
  else
7
- arg
8
+ ShallowMongoid::DocumentStub.new(arg.class, arg._id.to_s)
8
9
  end
9
10
  end
10
11
 
11
12
  def self.load(arg)
12
- if arg.is_a?(ShallowMongoid::DocumentStub)
13
- arg.klass.find(arg.id)
14
- else
15
- arg
13
+ return arg unless arg.is_a?(ShallowMongoid::DocumentStub)
14
+ result = arg.klass.find(arg.id)
15
+ (arg.selector || []).each do |message|
16
+ result = result.send(*message)
17
+ end
18
+ result
19
+ end
20
+
21
+ # The chain of relations allowing us to locate an embedded document.
22
+ # E.g., ['images', ['find', '4eef..678'], 'width']
23
+ def self.selector_from(doc)
24
+ [].tap do |selector|
25
+ while doc._parent do
26
+ selector.unshift ['find', doc._id.to_s] if doc.metadata.macro == :embeds_many
27
+ selector.unshift doc.metadata.key
28
+ doc = doc._parent
29
+ end
16
30
  end
17
31
  end
18
32
  end
@@ -1,7 +1,11 @@
1
1
  module Delayed::ShallowMongoid
2
- class DocumentStub < Struct.new(:klass, :id)
3
- def display_name
4
- "#{klass}[#{id}]##{method_name}"
2
+ class DocumentStub < Struct.new(:klass, :id, :selector)
3
+ def description
4
+ "#{klass}[#{id}]".tap do |desc|
5
+ desc << "." + selector.map{ |s|
6
+ s.is_a?(Array) ? "#{s.first}(#{s[1..-1].map(&:inspect).join(', ')})" : s
7
+ }.join(".") if selector && selector.any?
8
+ end
5
9
  end
6
10
  end
7
11
  end
@@ -1,25 +1,36 @@
1
1
  require 'spec_helper'
2
2
 
3
- class TestModel
3
+ class GrandchildModel
4
4
  include ::Mongoid::Document
5
- embeds_many :child_models
5
+ embedded_in :child_model, :inverse_of => :grandchild_models
6
6
  end
7
-
8
7
  class ChildModel
9
8
  include ::Mongoid::Document
10
- embedded_in :test_model, inverse_of: :child_models
9
+ embedded_in :test_model, :inverse_of => :child_models
10
+ embeds_many :grandchild_models
11
+ end
12
+ class TestModel
13
+ include ::Mongoid::Document
14
+ embeds_many :child_models
11
15
  end
12
16
 
13
17
  describe Delayed::ShallowMongoid::DocumentStub do
14
- before(:each) do
15
- @shallow = Delayed::ShallowMongoid::DocumentStub.new(TestModel, 111)
16
- end
17
18
  it "should have a klass" do
19
+ @shallow = Delayed::ShallowMongoid::DocumentStub.new(TestModel, 111)
18
20
  @shallow.klass.should == TestModel
19
21
  end
20
22
  it "should have an id" do
23
+ @shallow = Delayed::ShallowMongoid::DocumentStub.new(TestModel, 111)
21
24
  @shallow.id.should == 111
22
25
  end
26
+ it "should have an optional selector" do
27
+ @shallow = Delayed::ShallowMongoid::DocumentStub.new(TestModel, 111)
28
+ @shallow.selector.should == nil
29
+ end
30
+ it "can have an actual selector" do
31
+ @shallow = Delayed::ShallowMongoid::DocumentStub.new(TestModel, 111, [:a, :b])
32
+ @shallow.selector.should == [:a, :b]
33
+ end
23
34
  end
24
35
 
25
36
  describe ::Delayed::PerformableMethod do
@@ -38,37 +49,64 @@ describe ::Delayed::PerformableMethod do
38
49
  method.args.first.should be_a_kind_of(Delayed::ShallowMongoid::DocumentStub)
39
50
  method.args.first.id.should == @model._id.to_s
40
51
  end
41
- it "should not transform embedded document into shallow version" do
42
- child = ChildModel.new
43
- @model.child_models << child
44
- method = ::Delayed::PerformableMethod.new(child, :to_s, [])
45
- method.object.should_not be_a_kind_of(Delayed::ShallowMongoid::DocumentStub)
52
+ context "with an embedded document" do
53
+ before(:each) do
54
+ @child = ChildModel.new(:_id => ::BSON::ObjectId.new)
55
+ @model.child_models << @child
56
+ end
57
+ after(:each) do
58
+ @method.object.should be_a_kind_of(Delayed::ShallowMongoid::DocumentStub)
59
+ @method.object.id.should == @model.id.to_s
60
+ @method.object.klass.should == @model.class
61
+ end
62
+ it "should store the selector" do
63
+ @method = ::Delayed::PerformableMethod.new(@child, :to_s, [])
64
+ @method.object.selector.should == ['child_models', ['find', @child._id.to_s]]
65
+ end
66
+ it "should store the deeply nested selector" do
67
+ @grandchild = GrandchildModel.new(:_id => ::BSON::ObjectId.new)
68
+ @model.child_models.first.grandchild_models << @grandchild
69
+ @method = ::Delayed::PerformableMethod.new(@grandchild, :to_s, [])
70
+ @method.object.selector.should == ['child_models', ['find', @child._id.to_s], 'grandchild_models', ['find', @grandchild._id.to_s]]
71
+ end
46
72
  end
47
73
  end
48
74
 
49
75
  context 'when running job' do
50
- before(:each) do
51
- @method = ::Delayed::PerformableMethod.new(@model, :to_s, [])
52
- end
53
76
  it "should look up document" do
77
+ method = ::Delayed::PerformableMethod.new(@model, :to_s, [])
54
78
  TestModel.should_receive(:find).with(@model._id.to_s).and_return(@model)
55
- @method.perform
79
+ method.perform
56
80
  end
57
81
  it "should do nothing if document not found" do
82
+ method = ::Delayed::PerformableMethod.new(@model, :to_s, [])
58
83
  error = ::Mongoid::Errors::DocumentNotFound.new(TestModel, @model._id)
59
84
  TestModel.should_receive(:find).with(@model._id.to_s).and_raise(error)
60
- @method.perform.should be_true
85
+ method.perform.should be_true
86
+ end
87
+ it "should find embedded document" do
88
+ child = ChildModel.new(:_id => ::BSON::ObjectId.new)
89
+ @model.child_models << child
90
+ method = ::Delayed::PerformableMethod.new(child, :to_s, [])
91
+ TestModel.should_receive(:find).with(@model._id.to_s).and_return(@model)
92
+ method.perform
61
93
  end
62
94
  end
63
95
 
64
96
  context "display_name" do
65
97
  it "should return underlying class when a stub is being used" do
66
- @method = ::Delayed::PerformableMethod.new(@model, :to_s, [])
67
- @method.display_name.should == "TestModel[#{@model._id}]#to_s"
98
+ method = ::Delayed::PerformableMethod.new(@model, :to_s, [])
99
+ method.display_name.should == "TestModel[#{@model._id}]#to_s"
68
100
  end
69
101
  it "should return usual name when no stub is involved" do
70
- @method = ::Delayed::PerformableMethod.new(:test, :to_s, [])
71
- @method.display_name.should == "Symbol#to_s"
102
+ method = ::Delayed::PerformableMethod.new(:test, :to_s, [])
103
+ method.display_name.should == "Symbol#to_s"
104
+ end
105
+ it "should include selector when document is embedded" do
106
+ child = ChildModel.new(:_id => ::BSON::ObjectId.new)
107
+ @model.child_models << child
108
+ method = ::Delayed::PerformableMethod.new(child, :to_s, [])
109
+ method.display_name.should == "TestModel[#{@model._id}].child_models.find(\"#{child._id}\")#to_s"
72
110
  end
73
111
  end
74
112
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: delayed_job_shallow_mongoid
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.6
4
+ version: 0.2.7
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -13,7 +13,7 @@ date: 2011-12-19 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: delayed_job
16
- requirement: &2157589320 !ruby/object:Gem::Requirement
16
+ requirement: &2168767700 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ~>
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: 2.1.1
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *2157589320
24
+ version_requirements: *2168767700
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: mongoid
27
- requirement: &2157588680 !ruby/object:Gem::Requirement
27
+ requirement: &2168767040 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ~>
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: 2.0.2
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *2157588680
35
+ version_requirements: *2168767040
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: actionmailer
38
- requirement: &2157588060 !ruby/object:Gem::Requirement
38
+ requirement: &2168766380 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: '0'
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *2157588060
46
+ version_requirements: *2168766380
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: shoulda
49
- requirement: &2157587500 !ruby/object:Gem::Requirement
49
+ requirement: &2168765780 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ! '>='
@@ -54,10 +54,10 @@ dependencies:
54
54
  version: '0'
55
55
  type: :development
56
56
  prerelease: false
57
- version_requirements: *2157587500
57
+ version_requirements: *2168765780
58
58
  - !ruby/object:Gem::Dependency
59
59
  name: bundler
60
- requirement: &2157586840 !ruby/object:Gem::Requirement
60
+ requirement: &2168765040 !ruby/object:Gem::Requirement
61
61
  none: false
62
62
  requirements:
63
63
  - - ~>
@@ -65,10 +65,10 @@ dependencies:
65
65
  version: 1.0.0
66
66
  type: :development
67
67
  prerelease: false
68
- version_requirements: *2157586840
68
+ version_requirements: *2168765040
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: jeweler
71
- requirement: &2157586280 !ruby/object:Gem::Requirement
71
+ requirement: &2168764440 !ruby/object:Gem::Requirement
72
72
  none: false
73
73
  requirements:
74
74
  - - ~>
@@ -76,10 +76,10 @@ dependencies:
76
76
  version: 1.6.4
77
77
  type: :development
78
78
  prerelease: false
79
- version_requirements: *2157586280
79
+ version_requirements: *2168764440
80
80
  - !ruby/object:Gem::Dependency
81
81
  name: rspec
82
- requirement: &2157585740 !ruby/object:Gem::Requirement
82
+ requirement: &2168763800 !ruby/object:Gem::Requirement
83
83
  none: false
84
84
  requirements:
85
85
  - - ! '>='
@@ -87,10 +87,10 @@ dependencies:
87
87
  version: '0'
88
88
  type: :development
89
89
  prerelease: false
90
- version_requirements: *2157585740
90
+ version_requirements: *2168763800
91
91
  - !ruby/object:Gem::Dependency
92
92
  name: ruby-debug19
93
- requirement: &2157585060 !ruby/object:Gem::Requirement
93
+ requirement: &2168763160 !ruby/object:Gem::Requirement
94
94
  none: false
95
95
  requirements:
96
96
  - - ! '>='
@@ -98,7 +98,7 @@ dependencies:
98
98
  version: '0'
99
99
  type: :development
100
100
  prerelease: false
101
- version_requirements: *2157585060
101
+ version_requirements: *2168763160
102
102
  description: When the object or arg to a delayed_job is a Mongoid document, store
103
103
  only a small stub of the object instead of the full serialization.
104
104
  email: joey@aghion.com
@@ -138,7 +138,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
138
138
  version: '0'
139
139
  segments:
140
140
  - 0
141
- hash: -3402765443251860913
141
+ hash: 265545571011826092
142
142
  required_rubygems_version: !ruby/object:Gem::Requirement
143
143
  none: false
144
144
  requirements: