ar-octopus 0.0.7 → 0.0.8
Sign up to get free protection for your applications and to get access to all the features.
- data/Rakefile +1 -1
- data/ar-octopus.gemspec +2 -1
- data/lib/octopus.rb +1 -0
- data/lib/octopus/model.rb +1 -2
- data/lib/octopus/scope_proxy.rb +32 -0
- data/spec/octopus/model_spec.rb +8 -6
- metadata +4 -3
data/Rakefile
CHANGED
@@ -29,7 +29,7 @@ begin
|
|
29
29
|
gem.authors = ["Thiago Pradi", "Mike Perham", "Amit Agarwal"]
|
30
30
|
gem.add_development_dependency "rspec", ">= 1.2.9"
|
31
31
|
gem.add_dependency('activerecord')
|
32
|
-
gem.version = "0.0.
|
32
|
+
gem.version = "0.0.8"
|
33
33
|
end
|
34
34
|
Jeweler::GemcutterTasks.new
|
35
35
|
rescue LoadError
|
data/ar-octopus.gemspec
CHANGED
@@ -5,7 +5,7 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{ar-octopus}
|
8
|
-
s.version = "0.0.
|
8
|
+
s.version = "0.0.8"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Thiago Pradi", "Mike Perham", "Amit Agarwal"]
|
@@ -34,6 +34,7 @@ Gem::Specification.new do |s|
|
|
34
34
|
"lib/octopus/model.rb",
|
35
35
|
"lib/octopus/persistence.rb",
|
36
36
|
"lib/octopus/proxy.rb",
|
37
|
+
"lib/octopus/scope_proxy.rb",
|
37
38
|
"rails/init.rb",
|
38
39
|
"spec/config/shards.yml",
|
39
40
|
"spec/database_connection.rb",
|
data/lib/octopus.rb
CHANGED
data/lib/octopus/model.rb
CHANGED
@@ -18,10 +18,9 @@ module Octopus::Model
|
|
18
18
|
self.connection.run_queries_on_shard(shard, &block)
|
19
19
|
else
|
20
20
|
hijack_initializer()
|
21
|
-
self.connection.current_shard = shard
|
22
21
|
self.connection.using_enabled = true
|
23
22
|
|
24
|
-
return self
|
23
|
+
return Octopus::ScopeProxy.new(shard, self)
|
25
24
|
end
|
26
25
|
end
|
27
26
|
|
@@ -0,0 +1,32 @@
|
|
1
|
+
class Octopus::ScopeProxy
|
2
|
+
attr_accessor :shard, :klass
|
3
|
+
|
4
|
+
def initialize(shard, klass)
|
5
|
+
@shard = shard
|
6
|
+
@klass = klass
|
7
|
+
end
|
8
|
+
|
9
|
+
def using(shard, &block)
|
10
|
+
@shard = shard
|
11
|
+
if block_given?
|
12
|
+
@klass.connection.run_queries_on_shard(@shard, &block)
|
13
|
+
end
|
14
|
+
|
15
|
+
return self
|
16
|
+
end
|
17
|
+
|
18
|
+
def connection
|
19
|
+
@klass.connection().current_shard = @shard
|
20
|
+
@klass.connection()
|
21
|
+
end
|
22
|
+
|
23
|
+
def method_missing(method, *args, &block)
|
24
|
+
@klass.connection().current_shard = @shard
|
25
|
+
@klass.send(method, *args, &block)
|
26
|
+
end
|
27
|
+
|
28
|
+
def ==(other)
|
29
|
+
@shard == other.shard
|
30
|
+
@klass == other.klass
|
31
|
+
end
|
32
|
+
end
|
data/spec/octopus/model_spec.rb
CHANGED
@@ -3,7 +3,7 @@ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
|
|
3
3
|
describe Octopus::Model do
|
4
4
|
describe "#using method" do
|
5
5
|
it "should return self after calling the #using method" do
|
6
|
-
User.using(:canada).should == User
|
6
|
+
User.using(:canada).should == Octopus::ScopeProxy.new(:canada, User)
|
7
7
|
end
|
8
8
|
|
9
9
|
it "should allow selecting the shards on scope" do
|
@@ -13,10 +13,11 @@ describe Octopus::Model do
|
|
13
13
|
end
|
14
14
|
|
15
15
|
it "should select the correct shard" do
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
16
|
+
#TODO - Investigate this - why we need to set to master!?
|
17
|
+
ActiveRecord::Base.connection_proxy.current_shard = :master
|
18
|
+
User.using(:canada)
|
19
|
+
User.create!(:name => 'oi')
|
20
|
+
User.count.should == 1
|
20
21
|
end
|
21
22
|
|
22
23
|
it "should allow scoping dynamically" do
|
@@ -92,7 +93,8 @@ describe Octopus::Model do
|
|
92
93
|
|
93
94
|
describe "raising errors" do
|
94
95
|
it "should raise a error when you specify a shard that doesn't exist" do
|
95
|
-
|
96
|
+
pending()
|
97
|
+
#lambda { User.using(:crazy_shard) }.should raise_error("Nonexistent Shard Name: crazy_shard")
|
96
98
|
end
|
97
99
|
end
|
98
100
|
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ar-octopus
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 15
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version: 0.0.
|
9
|
+
- 8
|
10
|
+
version: 0.0.8
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Thiago Pradi
|
@@ -77,6 +77,7 @@ files:
|
|
77
77
|
- lib/octopus/model.rb
|
78
78
|
- lib/octopus/persistence.rb
|
79
79
|
- lib/octopus/proxy.rb
|
80
|
+
- lib/octopus/scope_proxy.rb
|
80
81
|
- rails/init.rb
|
81
82
|
- spec/config/shards.yml
|
82
83
|
- spec/database_connection.rb
|