mongoid_silo 0.2.3 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -5,8 +5,8 @@ module MongoidSilo
5
5
  class UpdateSiloWorker
6
6
  include Sidekiq::Worker
7
7
 
8
- def perform(item_id, item_class, name, mode="save", generator=nil)
9
- @item_id, @item_class, @generator = item_id, item_class, generator
8
+ def perform(item_id, item_class, name, mode="save", generator=nil, callback=nil)
9
+ @item_id, @item_class, @generator, @callback = item_id, item_class, generator, callback
10
10
  mode.to_s == "save" ? update_silo(name, generator) : destroy_silo(name)
11
11
  end
12
12
 
@@ -21,6 +21,9 @@ module MongoidSilo
21
21
  else
22
22
  @silo = Silo.create(item_class: @item_class, item_id: @item_id, bag: @content, silo_type: name)
23
23
  end
24
+ if @callback
25
+ @item.__send__(@callback).call(@item, :updated)
26
+ end
24
27
  end
25
28
 
26
29
  def destroy_silo name
@@ -28,6 +31,9 @@ module MongoidSilo
28
31
  if @silo
29
32
  @silo.destroy
30
33
  end
34
+ if @callback
35
+ @item.__send__(@callback).call(@item, :destroyed)
36
+ end
31
37
  end
32
38
 
33
39
  def item_class
data/lib/mongoid/silo.rb CHANGED
@@ -9,17 +9,19 @@ module Mongoid
9
9
  opts = if block_given?
10
10
  opts = ActiveSupport::OrderedOptions.new
11
11
  opts.dependents = []
12
+ opts.callback = nil
12
13
  yield(opts)
13
14
  opts.generator ||= "MongoidSilo::GrainBelt"
14
15
  opts
15
16
  else
16
17
  opts[:generator] ||= "MongoidSilo::GrainBelt"
17
18
  opts[:dependents] = []
19
+ opts[:callback] = nil
18
20
  opts
19
21
  end
20
22
  setup_own_silo name, opts
21
23
  if opts[:dependents].length != 0
22
- setup_listeners name, opts[:dependents], opts[:generator]
24
+ setup_listeners name, opts[:dependents], opts[:generator], opts[:callback]
23
25
  end
24
26
  end
25
27
 
@@ -32,18 +34,18 @@ module Mongoid
32
34
  end
33
35
 
34
36
  set_callback :save, :after do
35
- update_silo name, opts[:generator]
37
+ update_silo name, opts[:generator], opts[:callback]
36
38
  end
37
39
 
38
40
  set_callback :destroy, :after do
39
- destroy_silo name
41
+ destroy_silo name, opts[:callback]
40
42
  end
41
43
  end
42
44
 
43
- def setup_listeners name, dependents, generator
45
+ def setup_listeners name, dependents, generator, callback
44
46
  registry = dependents.each_with_object([]) do |dep, arr|
45
47
  next unless dep[:class_name]
46
- out = {class_name: dep[:class_name], parent_class: self.to_s, silo_name: name.to_s, generator: generator}
48
+ out = {class_name: dep[:class_name], parent_class: self.to_s, silo_name: name.to_s, generator: generator, callback: callback}
47
49
  if dep[:foreign_key]
48
50
  out[:foreign_key] = dep[:foreign_key]
49
51
  else
@@ -57,24 +59,24 @@ module Mongoid
57
59
  key[:class_name].classify.constantize.class_eval <<-EOS, __FILE__, __LINE__+1
58
60
  set_callback :save, :after do
59
61
  ident = key[:foreign_key].to_sym
60
- MongoidSilo::UpdateSiloWorker.perform_async(self.__send__(ident), "#{key[:parent_class]}", "#{key[:silo_name]}", :save, "#{key[:generator]}")
62
+ MongoidSilo::UpdateSiloWorker.perform_async(self.__send__(ident), "#{key[:parent_class]}", "#{key[:silo_name]}", :save, "#{key[:generator]}", #{key[:callback]})
61
63
  end
62
64
 
63
65
  set_callback :destroy, :after do
64
66
  ident = key[:foreign_key].to_sym
65
- MongoidSilo::UpdateSiloWorker.perform_async(self.__send__(ident), "#{key[:parent_class]}", "#{key[:silo_name]}", :destroy)
67
+ MongoidSilo::UpdateSiloWorker.perform_async(self.__send__(ident), "#{key[:parent_class]}", "#{key[:silo_name]}", :destroy, #{key[:callback]})
66
68
  end
67
69
  EOS
68
70
  end
69
71
  end
70
72
  end
71
73
 
72
- def update_silo name, generator
73
- MongoidSilo::UpdateSiloWorker.perform_async(self.id.to_s, self.class.to_s, name, :save, generator)
74
+ def update_silo name, generator, callback
75
+ MongoidSilo::UpdateSiloWorker.perform_async(self.id.to_s, self.class.to_s, name, :save, generator, callback)
74
76
  end
75
77
 
76
- def destroy_silo name
77
- MongoidSilo::UpdateSiloWorker.perform_async(self.id.to_s, self.class.to_s, name, :destroy)
78
+ def destroy_silo name, callback
79
+ MongoidSilo::UpdateSiloWorker.perform_async(self.id.to_s, self.class.to_s, name, :destroy, callback)
78
80
  end
79
81
 
80
82
  def from_silo name="default"
@@ -1,3 +1,3 @@
1
1
  module MongoidSilo
2
- VERSION = "0.2.3"
2
+ VERSION = "0.3.0"
3
3
  end
@@ -0,0 +1,5 @@
1
+ FactoryGirl.define do
2
+ factory :callback_project do
3
+ name { Faker::Name.name }
4
+ end
5
+ end
@@ -113,12 +113,10 @@ describe Silo do
113
113
  project_item3 = create(:block_project_item, block_project_id: block_project.id)
114
114
  block_project.block_silo["items"].length.should eq(3)
115
115
 
116
- puts "Name was: " + project_item2.name
117
116
 
118
117
  project_item2.name = Faker::Name.name
119
118
  project_item2.save
120
119
 
121
- puts project_item2._save_callbacks.inspect
122
120
  bp = BlockProject.find block_project.id
123
121
 
124
122
  bp.block_silo["items"].map{|i| i["name"]}.sort.should eq(
@@ -173,5 +171,16 @@ describe Silo do
173
171
  Silo.count.should eq(0)
174
172
  end
175
173
  end
174
+
175
+ context "Callbacks", focus: true do
176
+ subject(:project) { build(:callback_project) }
177
+
178
+ it "should trigger a named callback" do
179
+ CallbackProject.should_receive(:triggered)
180
+ project.save
181
+ end
182
+
183
+
184
+ end
176
185
 
177
186
  end
@@ -0,0 +1,20 @@
1
+ class CallbackProject
2
+ include Mongoid::Document
3
+ include Mongoid::Silo
4
+
5
+ field :name
6
+
7
+ silo :basic do |config|
8
+ config.callback = :triggered
9
+ end
10
+
11
+ def triggered
12
+ Proc.new{|x|
13
+ x.class.triggered
14
+ }
15
+ end
16
+
17
+ def self.triggered
18
+ true
19
+ end
20
+ end
metadata CHANGED
@@ -1,64 +1,64 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mongoid_silo
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.3
5
- prerelease:
4
+ version: 0.3.0
5
+ prerelease:
6
6
  platform: ruby
7
7
  authors:
8
8
  - John Maxwell
9
- autorequire:
9
+ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-01-20 00:00:00.000000000 Z
12
+ date: 2013-01-22 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activesupport
16
- version_requirements: !ruby/object:Gem::Requirement
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
17
18
  requirements:
18
- - - "~>"
19
+ - - ~>
19
20
  - !ruby/object:Gem::Version
20
21
  version: 3.2.9
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
21
25
  none: false
22
- requirement: !ruby/object:Gem::Requirement
23
26
  requirements:
24
- - - "~>"
27
+ - - ~>
25
28
  - !ruby/object:Gem::Version
26
29
  version: 3.2.9
27
- none: false
28
- prerelease: false
29
- type: :runtime
30
30
  - !ruby/object:Gem::Dependency
31
31
  name: mongoid
32
- version_requirements: !ruby/object:Gem::Requirement
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
33
34
  requirements:
34
- - - "~>"
35
+ - - ~>
35
36
  - !ruby/object:Gem::Version
36
37
  version: 3.0.16
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
37
41
  none: false
38
- requirement: !ruby/object:Gem::Requirement
39
42
  requirements:
40
- - - "~>"
43
+ - - ~>
41
44
  - !ruby/object:Gem::Version
42
45
  version: 3.0.16
43
- none: false
44
- prerelease: false
45
- type: :runtime
46
46
  - !ruby/object:Gem::Dependency
47
47
  name: sidekiq
48
- version_requirements: !ruby/object:Gem::Requirement
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
49
50
  requirements:
50
- - - "~>"
51
+ - - ~>
51
52
  - !ruby/object:Gem::Version
52
53
  version: 2.6.1
54
+ type: :runtime
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
53
57
  none: false
54
- requirement: !ruby/object:Gem::Requirement
55
58
  requirements:
56
- - - "~>"
59
+ - - ~>
57
60
  - !ruby/object:Gem::Version
58
61
  version: 2.6.1
59
- none: false
60
- prerelease: false
61
- type: :runtime
62
62
  description: MongoidSilo gives a simple way to create static representations of models
63
63
  email:
64
64
  - john@musicglue.com
@@ -66,9 +66,9 @@ executables: []
66
66
  extensions: []
67
67
  extra_rdoc_files: []
68
68
  files:
69
- - ".gitignore"
70
- - ".rspec"
71
- - ".travis.yml"
69
+ - .gitignore
70
+ - .rspec
71
+ - .travis.yml
72
72
  - Gemfile
73
73
  - Guardfile
74
74
  - LICENSE.txt
@@ -84,6 +84,7 @@ files:
84
84
  - mongoid_silo.gemspec
85
85
  - spec/factories/block_project_factory.rb
86
86
  - spec/factories/block_project_item_factory.rb
87
+ - spec/factories/callback_project_factory.rb
87
88
  - spec/factories/complex_project_factory.rb
88
89
  - spec/factories/multi_silo_project_factory.rb
89
90
  - spec/factories/project_factory.rb
@@ -93,6 +94,7 @@ files:
93
94
  - spec/spec_helper.rb
94
95
  - spec/support/models/block_project.rb
95
96
  - spec/support/models/block_project_item.rb
97
+ - spec/support/models/callback_project.rb
96
98
  - spec/support/models/complex_project.rb
97
99
  - spec/support/models/multi_silo_project.rb
98
100
  - spec/support/models/project.rb
@@ -102,33 +104,32 @@ files:
102
104
  - spec/support/silovators/make_name_grain_belt.rb
103
105
  homepage: https://github.com/musicglue/mongoid_silo
104
106
  licenses: []
105
- post_install_message:
107
+ post_install_message:
106
108
  rdoc_options: []
107
109
  require_paths:
108
110
  - lib
109
111
  required_ruby_version: !ruby/object:Gem::Requirement
112
+ none: false
110
113
  requirements:
111
- - - ">="
114
+ - - ! '>='
112
115
  - !ruby/object:Gem::Version
113
- version: !binary |-
114
- MA==
115
- none: false
116
+ version: '0'
116
117
  required_rubygems_version: !ruby/object:Gem::Requirement
118
+ none: false
117
119
  requirements:
118
- - - ">="
120
+ - - ! '>='
119
121
  - !ruby/object:Gem::Version
120
- version: !binary |-
121
- MA==
122
- none: false
122
+ version: '0'
123
123
  requirements: []
124
- rubyforge_project:
124
+ rubyforge_project:
125
125
  rubygems_version: 1.8.24
126
- signing_key:
126
+ signing_key:
127
127
  specification_version: 3
128
128
  summary: MongoidSilo is a bit like a Grain Elevator, but without the grain.
129
129
  test_files:
130
130
  - spec/factories/block_project_factory.rb
131
131
  - spec/factories/block_project_item_factory.rb
132
+ - spec/factories/callback_project_factory.rb
132
133
  - spec/factories/complex_project_factory.rb
133
134
  - spec/factories/multi_silo_project_factory.rb
134
135
  - spec/factories/project_factory.rb
@@ -138,6 +139,7 @@ test_files:
138
139
  - spec/spec_helper.rb
139
140
  - spec/support/models/block_project.rb
140
141
  - spec/support/models/block_project_item.rb
142
+ - spec/support/models/callback_project.rb
141
143
  - spec/support/models/complex_project.rb
142
144
  - spec/support/models/multi_silo_project.rb
143
145
  - spec/support/models/project.rb
@@ -145,3 +147,4 @@ test_files:
145
147
  - spec/support/silovators/make_complex_grain_belt.rb
146
148
  - spec/support/silovators/make_location_grain_belt.rb
147
149
  - spec/support/silovators/make_name_grain_belt.rb
150
+ has_rdoc: