mongoid_silo 0.1.2 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile CHANGED
@@ -8,7 +8,7 @@ group :test do
8
8
  gem 'factory_girl'
9
9
  gem 'database_cleaner'
10
10
  gem 'fuubar'
11
- gem 'faker'
11
+ gem 'ffaker'
12
12
  gem 'spork'
13
13
  end
14
14
 
data/Guardfile CHANGED
@@ -5,9 +5,19 @@ guard :bundler do
5
5
  watch("Gemfile")
6
6
  end
7
7
 
8
- guard :rspec do
9
- watch(%r{^app/models/(.+)\.rb$}) { |m| "spec/models/#{m[1]}_spec.rb" }
8
+ guard :spork, :rspec_env => { 'RAILS_ENV' => 'test', 'RACK_ENV' => 'test' } do
9
+ watch('Gemfile')
10
+ watch('Gemfile.lock')
11
+ watch('spec/spec_helper.rb') { :rspec }
12
+ watch(%r{spec/support/}) { :spork }
13
+ end
14
+
15
+ guard :rspec, cli: "--drb --color --fail-fast" do
16
+ watch(%r{^app/models/(.+)\.rb$}) { |m| "spec/models/#{m[1]}_spec.rb" }
10
17
  watch(%r{^app/workers/(.+)\.rb$}) { "spec" }
18
+ watch(%r{^lib/(.+)\.rb$}) { |m| "spec/models/silo_spec.rb" }
11
19
  watch(%r{^spec/.+_spec\.rb$})
12
- watch('spec/spec_helper.rb') { "spec" }
13
- end
20
+ watch('spec/spec_helper.rb') { "spec" }
21
+ end
22
+
23
+
@@ -7,7 +7,6 @@ module MongoidSilo
7
7
 
8
8
  def perform(item_id, item_class, name, mode="save", generator=nil)
9
9
  @item_id, @item_class, @generator = item_id, item_class, generator
10
-
11
10
  mode.to_s == "save" ? update_silo(name, generator) : destroy_silo(name)
12
11
  end
13
12
 
data/lib/mongoid/silo.rb CHANGED
@@ -6,7 +6,27 @@ module Mongoid
6
6
 
7
7
  module ClassMethods
8
8
  def silo name=:default, opts={}
9
- opts[:generator] ||= "MongoidSilo::GrainBelt"
9
+ opts = if block_given?
10
+ opts = ActiveSupport::OrderedOptions.new
11
+ opts.dependents = []
12
+ yield(opts)
13
+ opts.generator ||= "MongoidSilo::GrainBelt"
14
+ opts
15
+ else
16
+ opts[:generator] ||= "MongoidSilo::GrainBelt"
17
+ opts[:dependents] = []
18
+ opts
19
+ end
20
+ setup_own_silo name, opts
21
+ if opts[:dependents].length != 0
22
+ setup_listeners name, opts[:dependents], opts[:generator]
23
+ end
24
+ end
25
+
26
+
27
+
28
+ protected
29
+ def setup_own_silo name, opts
10
30
  define_method "#{name}_silo" do
11
31
  from_silo name
12
32
  end
@@ -19,6 +39,35 @@ module Mongoid
19
39
  destroy_silo name
20
40
  end
21
41
  end
42
+
43
+ def setup_listeners name, dependents, generator
44
+ registry = dependents.each_with_object([]) do |dep, arr|
45
+ next unless dep[:class_name]
46
+ out = {class_name: dep[:class_name], parent_class: self.to_s, silo_name: name.to_s, generator: generator}
47
+ if dep[:foreign_key]
48
+ out[:foreign_key] = dep[:foreign_key]
49
+ else
50
+ out[:foreign_key] = self.to_s.underscore.singularize + "_id"
51
+ end
52
+ arr << out
53
+ end
54
+
55
+
56
+ registry.each do |key|
57
+ puts key.inspect
58
+ key[:class_name].classify.constantize.class_eval <<-EOS, __FILE__, __LINE__+1
59
+ set_callback :save, :after do
60
+ ident = key[:foreign_key].to_sym
61
+ MongoidSilo::UpdateSiloWorker.perform_async(self.__send__(ident), "#{key[:parent_class]}", "#{key[:silo_name]}", :save, "#{key[:generator]}")
62
+ end
63
+
64
+ set_callback :destroy, :after do
65
+ ident = key[:foreign_key].to_sym
66
+ MongoidSilo::UpdateSiloWorker.perform_async(self.__send__(ident), "#{key[:parent_class]}", "#{key[:silo_name]}", :destroy)
67
+ end
68
+ EOS
69
+ end
70
+ end
22
71
  end
23
72
 
24
73
  def update_silo name, generator
@@ -1,3 +1,3 @@
1
1
  module MongoidSilo
2
- VERSION = "0.1.2"
2
+ VERSION = "0.2.0"
3
3
  end
@@ -0,0 +1,5 @@
1
+ FactoryGirl.define do
2
+ factory :block_project do
3
+ name { Faker::Name.name }
4
+ end
5
+ end
@@ -0,0 +1,6 @@
1
+ FactoryGirl.define do
2
+ factory :block_project_item do
3
+ name { Faker::Name.name }
4
+ block_project_id { nil }
5
+ end
6
+ end
@@ -2,6 +2,6 @@ FactoryGirl.define do
2
2
  factory :multi_silo_project do
3
3
  name { Faker::Name.name }
4
4
  city { Faker::Address.city }
5
- country { Faker::Address.country }
5
+ county { Faker::AddressUK.county }
6
6
  end
7
7
  end
@@ -83,6 +83,51 @@ describe Silo do
83
83
  end
84
84
  end
85
85
 
86
+ context "Block based constructors:" do
87
+
88
+ subject(:block_project) { create(:block_project) }
89
+
90
+ it "defines an accessor based on the declared silo name" do
91
+ block_project.should respond_to(:block_silo)
92
+ end
93
+
94
+ it "should create the required silo" do
95
+ block_project.block_silo["name"].should eq(block_project.name)
96
+ end
97
+
98
+ it "updates the parent when a child is saved" do
99
+ project_item = create(:block_project_item, block_project_id: block_project.id)
100
+ block_project.block_silo["items"][0]["name"].should eq(project_item.name)
101
+ end
102
+
103
+ it "updates the parent when a child is updated" do
104
+ project_item = create(:block_project_item, block_project_id: block_project.id)
105
+ project_item.name = Faker::Name.name
106
+ project_item.save
107
+ block_project.block_silo["items"][0]["name"].should eq(project_item.name)
108
+ end
109
+
110
+ it "updates when there are many children" do
111
+ project_item1 = create(:block_project_item, block_project_id: block_project.id)
112
+ project_item2 = create(:block_project_item, block_project_id: block_project.id)
113
+ project_item3 = create(:block_project_item, block_project_id: block_project.id)
114
+ block_project.block_silo["items"].length.should eq(3)
115
+
116
+ puts "Name was: " + project_item2.name
117
+
118
+ project_item2.name = Faker::Name.name
119
+ project_item2.save
120
+
121
+ puts project_item2._save_callbacks.inspect
122
+ bp = BlockProject.find block_project.id
123
+
124
+ bp.block_silo["items"].map{|i| i["name"]}.sort.should eq(
125
+ [project_item1.name, project_item2.name, project_item3.name].sort
126
+ )
127
+ end
128
+
129
+ end
130
+
86
131
  context "Multiple silo constructors:" do
87
132
  before do
88
133
  @multi_silo_project = create(:multi_silo_project)
@@ -102,14 +147,14 @@ describe Silo do
102
147
  it "Correctly populates the Location silo" do
103
148
  _expectation = {
104
149
  "city" => @multi_silo_project.city,
105
- "country" => @multi_silo_project.country
150
+ "county" => @multi_silo_project.county
106
151
  }
107
152
  @multi_silo_project.location_silo.should eq(_expectation)
108
153
  end
109
154
 
110
155
  it "Correctly updates both silos" do
111
156
  @multi_silo_project.city = Faker::Address.city
112
- @multi_silo_project.country = Faker::Address.country
157
+ @multi_silo_project.county = Faker::AddressUK.county
113
158
  @multi_silo_project.name = Faker::Name.name
114
159
  @multi_silo_project.save
115
160
  _name_silo_expectation = {
@@ -117,7 +162,7 @@ describe Silo do
117
162
  }
118
163
  _location_silo_expectation = {
119
164
  "city" => @multi_silo_project.city,
120
- "country" => @multi_silo_project.country
165
+ "county" => @multi_silo_project.county
121
166
  }
122
167
  @multi_silo_project.name_silo.should eq(_name_silo_expectation)
123
168
  @multi_silo_project.location_silo.should eq(_location_silo_expectation)
data/spec/spec_helper.rb CHANGED
@@ -5,15 +5,16 @@ require 'guard/rspec'
5
5
  require 'rspec'
6
6
 
7
7
  Spork.prefork do
8
- require 'mongoid_silo'
8
+ require 'sidekiq'
9
+ require 'mongoid'
9
10
  require 'factory_girl'
10
11
  require 'database_cleaner'
11
- require 'faker'
12
+ require 'ffaker'
12
13
  require 'sidekiq/testing/inline'
13
14
  # Loading more in this block will cause your tests to run faster. However,
14
15
  # if you change any configuration or code from libraries loaded here, you'll
15
16
  # need to restart spork for it take effect.
16
- FactoryGirl.find_definitions
17
+
17
18
 
18
19
  Mongoid.load!(File.expand_path("../mongoid.yml", __FILE__), :test)
19
20
 
@@ -21,6 +22,9 @@ Spork.prefork do
21
22
 
22
23
  RSpec.configure do |config|
23
24
  config.include FactoryGirl::Syntax::Methods
25
+ config.filter_run :focus => true
26
+ config.run_all_when_everything_filtered = true
27
+ config.backtrace_clean_patterns << /gems\//
24
28
 
25
29
  config.before(:suite) do
26
30
  DatabaseCleaner.strategy = :truncation
@@ -32,11 +36,14 @@ Spork.prefork do
32
36
  end
33
37
  end
34
38
 
35
- Dir['./spec/support/**/*.rb'].each{ |file| require file }
39
+
36
40
  end
37
41
 
38
42
  Spork.each_run do
39
- Dir['./app/**/*.rb'].each { |file| require file }
43
+ require 'mongoid_silo'
44
+ FactoryGirl.find_definitions
45
+ # Dir['./app/**/*.rb'].each { |file| require file }
46
+ Dir['./spec/support/**/*.rb'].each{ |file| require file }
40
47
  end
41
48
 
42
49
 
@@ -0,0 +1,13 @@
1
+ require_relative './block_project_item'
2
+ class BlockProject
3
+ include Mongoid::Document
4
+ include Mongoid::Silo
5
+
6
+ silo :block do |conf|
7
+ conf.generator = "MakeBlockProjectGrainBelt"
8
+ conf.dependents << {class_name: "BlockProjectItem"}
9
+ end
10
+
11
+ field :name, type: String
12
+ has_many :block_project_items
13
+ end
@@ -0,0 +1,8 @@
1
+ class BlockProjectItem
2
+ include Mongoid::Document
3
+
4
+ field :name, type: String
5
+
6
+ belongs_to :block_project
7
+
8
+ end
@@ -4,7 +4,7 @@ class MultiSiloProject
4
4
 
5
5
  field :name, type: String
6
6
  field :city, type: String
7
- field :country, type: String
7
+ field :county, type: String
8
8
 
9
9
  silo :name, generator: "MakeNameGrainBelt"
10
10
  silo :location, generator: "MakeLocationGrainBelt"
@@ -0,0 +1,16 @@
1
+ class MakeBlockProjectGrainBelt < MongoidSilo::GrainBelt
2
+ def generate
3
+ {
4
+ name: name,
5
+ items: item_array
6
+ }
7
+ end
8
+
9
+ def item_array
10
+ block_project_items.each_with_object([]) do |itm, arr|
11
+ arr << {
12
+ name: itm.name
13
+ }
14
+ end
15
+ end
16
+ end
@@ -2,7 +2,7 @@ class MakeLocationGrainBelt < MongoidSilo::GrainBelt
2
2
  def generate
3
3
  {
4
4
  city: city,
5
- country: country
5
+ county: county
6
6
  }
7
7
  end
8
8
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mongoid_silo
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.2.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-01-14 00:00:00.000000000 Z
12
+ date: 2013-01-16 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activesupport
@@ -82,6 +82,8 @@ files:
82
82
  - lib/mongoid_silo/railtie.rb
83
83
  - lib/mongoid_silo/version.rb
84
84
  - mongoid_silo.gemspec
85
+ - spec/factories/block_project_factory.rb
86
+ - spec/factories/block_project_item_factory.rb
85
87
  - spec/factories/complex_project_factory.rb
86
88
  - spec/factories/multi_silo_project_factory.rb
87
89
  - spec/factories/project_factory.rb
@@ -89,9 +91,12 @@ files:
89
91
  - spec/models/silo_spec.rb
90
92
  - spec/mongoid.yml
91
93
  - spec/spec_helper.rb
94
+ - spec/support/models/block_project.rb
95
+ - spec/support/models/block_project_item.rb
92
96
  - spec/support/models/complex_project.rb
93
97
  - spec/support/models/multi_silo_project.rb
94
98
  - spec/support/models/project.rb
99
+ - spec/support/silovators/make_block_project_grain_belt.rb
95
100
  - spec/support/silovators/make_complex_grain_belt.rb
96
101
  - spec/support/silovators/make_location_grain_belt.rb
97
102
  - spec/support/silovators/make_name_grain_belt.rb
@@ -120,6 +125,8 @@ signing_key:
120
125
  specification_version: 3
121
126
  summary: MongoidSilo is a bit like a Grain Elevator, but without the grain.
122
127
  test_files:
128
+ - spec/factories/block_project_factory.rb
129
+ - spec/factories/block_project_item_factory.rb
123
130
  - spec/factories/complex_project_factory.rb
124
131
  - spec/factories/multi_silo_project_factory.rb
125
132
  - spec/factories/project_factory.rb
@@ -127,9 +134,12 @@ test_files:
127
134
  - spec/models/silo_spec.rb
128
135
  - spec/mongoid.yml
129
136
  - spec/spec_helper.rb
137
+ - spec/support/models/block_project.rb
138
+ - spec/support/models/block_project_item.rb
130
139
  - spec/support/models/complex_project.rb
131
140
  - spec/support/models/multi_silo_project.rb
132
141
  - spec/support/models/project.rb
142
+ - spec/support/silovators/make_block_project_grain_belt.rb
133
143
  - spec/support/silovators/make_complex_grain_belt.rb
134
144
  - spec/support/silovators/make_location_grain_belt.rb
135
145
  - spec/support/silovators/make_name_grain_belt.rb