roulette 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.2
1
+ 0.0.3
@@ -0,0 +1,13 @@
1
+ class Roulette::Transaction
2
+ attr_accessor :args, :store, :method
3
+
4
+ def initialize(options = {})
5
+ self.store = options[:store]
6
+ self.args = *options[:args]
7
+ self.method = options[:method]
8
+ end
9
+
10
+ def fire(method_name = nil )
11
+ store.send (method_name||method).to_sym, *args
12
+ end
13
+ end
data/lib/roulette.rb CHANGED
@@ -19,36 +19,23 @@ class Roulette
19
19
  end
20
20
 
21
21
  def method_missing(method_name, *args, &blk)
22
- Transaction.new(@stores, *args).fire(method_name)
22
+ Transaction.new(:args => args, :store => select_store(args), :method => method_name).fire
23
23
  end
24
24
 
25
- class Transaction
26
- attr_accessor :key, :args, :stores
27
-
28
-
29
- def initialize(stores, *args)
30
- self.stores = stores
31
- self.args = *args
32
- extract_key
33
- end
34
-
35
- def extract_key
36
- # there is a 'bug' with something in the rails library that when loaded will
37
- # produce Roulette::Transaction.new([], "key") # => "k" instead of "key"
38
- # this is a fix for that, but i don't know what in rails env causes this discrepency
39
- self.key = args.is_a?(Array) ? args.first.to_s : args.to_s
40
- end
41
-
42
- def fire(method)
43
- select_store.send method.to_sym, *args
44
- end
45
-
46
- private
47
- def select_store
48
- val = Digest::SHA1.hexdigest(self.key).unpack('Q').join.to_i
49
- store_index = val % stores.count
50
- @stores[store_index]
51
- end
25
+ def extract_key(*args)
26
+ args.flatten.first.to_s
52
27
  end
53
28
 
54
- end
29
+ def select_store(*args)
30
+ key = extract_key(*args)
31
+ store_for_key(key)
32
+ end
33
+
34
+ def store_for_key(key)
35
+ val = Digest::SHA1.hexdigest(key).unpack('Q').join.to_i
36
+ store_index = val % stores.count
37
+ self.stores[store_index]
38
+ end
39
+ end
40
+
41
+ require 'roulette/transaction'
Binary file
data/roulette.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{roulette}
8
- s.version = "0.0.2"
8
+ s.version = "0.0.3"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Schneems"]
12
- s.date = %q{2011-07-11}
12
+ s.date = %q{2011-07-14}
13
13
  s.description = %q{
14
14
  Super easy pre-sharding for your ruby NOSQL store. Presharding is used to naively scale your data-stores horizontally.
15
15
  }
@@ -26,12 +26,14 @@ Gem::Specification.new do |s|
26
26
  "VERSION",
27
27
  "autotest/discover.rb",
28
28
  "lib/roulette.rb",
29
+ "lib/roulette/transaction.rb",
29
30
  "license.txt",
30
31
  "pkg/roulette-.gem",
31
32
  "pkg/roulette-0.0.1.gem",
33
+ "pkg/roulette-0.0.2.gem",
32
34
  "roulette.gemspec",
33
- "spec/roulette/roulette_spec.rb",
34
35
  "spec/roulette/transaction_spec.rb",
36
+ "spec/roulette_spec.rb",
35
37
  "spec/spec_helper.rb"
36
38
  ]
37
39
  s.homepage = %q{http://github.com/Schnems/roulette}
@@ -40,8 +42,8 @@ Gem::Specification.new do |s|
40
42
  s.rubygems_version = %q{1.6.2}
41
43
  s.summary = %q{Pre-Sharding so simple you'll shoot yourself :p}
42
44
  s.test_files = [
43
- "spec/roulette/roulette_spec.rb",
44
45
  "spec/roulette/transaction_spec.rb",
46
+ "spec/roulette_spec.rb",
45
47
  "spec/spec_helper.rb"
46
48
  ]
47
49
 
@@ -13,46 +13,17 @@ describe Roulette::Transaction do
13
13
 
14
14
  describe '#new' do
15
15
  it "keeps args, stores, and key" do
16
- transaction = Roulette::Transaction.new([@kv_store], *@args)
17
- transaction.stores.should eq([@kv_store])
18
- transaction.key.should eq(@key)
16
+ transaction = Roulette::Transaction.new(:store => @kv_store, :args => @args)
17
+ transaction.store.should eq(@kv_store)
19
18
  transaction.args.should eq(@args)
20
19
  end
21
20
  end
22
21
 
23
- describe '#extract_key' do
24
- it "pulls out the key even if there is only one arg" do
25
- key = "foo"
26
- transaction = Roulette::Transaction.new([], key)
27
- transaction.extract_key.should eq(key)
28
- end
29
-
30
- it "pulls out the key" do
31
- transaction = Roulette::Transaction.new([], *@args)
32
- transaction.extract_key.should eq(@key)
33
- end
34
- end
35
-
36
- describe '#select_store' do
37
- it "picks a store" do
38
- transaction = Roulette::Transaction.new([@kv_store], *@args)
39
- store = transaction.send :select_store
40
- store.should eq(@kv_store)
41
- end
42
-
43
- it "selects different stores for different keys" do
44
- @kv_store_deux = FakeKeyValueStore.new
45
- store = Roulette::Transaction.new([@kv_store, @kv_store_deux], "key").send :select_store
46
- store_deux = Roulette::Transaction.new([@kv_store, @kv_store_deux], "different_key").send :select_store
47
- store.should_not eq(store_deux)
48
- end
49
- end
50
-
51
22
  describe "#fire" do
52
23
  it "sends a method to a kv_store" do
53
24
  fired_method = :sadd
54
25
  @kv_store.should_receive(fired_method)
55
- transaction = Roulette::Transaction.new([@kv_store], *@args)
26
+ transaction = Roulette::Transaction.new(:store => @kv_store, :args => @args)
56
27
  transaction.fire(fired_method)
57
28
  end
58
29
  end
@@ -43,4 +43,42 @@ describe Roulette do
43
43
  @roulette.stores.should eq(store_array)
44
44
  end
45
45
  end
46
+
47
+
48
+ describe '#extract_key' do
49
+ before do
50
+ @kv_store = FakeKeyValueStore.new
51
+ @roulette = Roulette.new(@kv_store)
52
+ end
53
+
54
+ it "pulls out the key even if there is only one arg" do
55
+ key = "foo"
56
+ @roulette.extract_key(key).should eq(key)
57
+ end
58
+
59
+ it "pulls out the key" do
60
+ key = "foo"
61
+ @roulette.extract_key([key, "some", "other", "stuff"]).should eq(key)
62
+ end
63
+ end
64
+
65
+ describe '#select_store' do
66
+ before do
67
+ @kv_store = FakeKeyValueStore.new
68
+ @roulette = Roulette.new(@kv_store)
69
+ end
70
+
71
+ it "picks a store" do
72
+ @roulette.select_store("key").should eq(@kv_store)
73
+ end
74
+
75
+ it "selects different stores for different keys" do
76
+ @kv_store_deux = FakeKeyValueStore.new
77
+ multiple_store_roulette = Roulette.new([@kv_store, @kv_store_deux])
78
+ store = multiple_store_roulette.select_store("key")
79
+ store_deux = multiple_store_roulette.select_store("different_key")
80
+ store.should_not eq(store_deux)
81
+ end
82
+ end
83
+
46
84
  end
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: roulette
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.0.2
5
+ version: 0.0.3
6
6
  platform: ruby
7
7
  authors:
8
8
  - Schneems
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2011-07-11 00:00:00 -05:00
13
+ date: 2011-07-14 00:00:00 -05:00
14
14
  default_executable:
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
@@ -85,12 +85,14 @@ files:
85
85
  - VERSION
86
86
  - autotest/discover.rb
87
87
  - lib/roulette.rb
88
+ - lib/roulette/transaction.rb
88
89
  - license.txt
89
90
  - pkg/roulette-.gem
90
91
  - pkg/roulette-0.0.1.gem
92
+ - pkg/roulette-0.0.2.gem
91
93
  - roulette.gemspec
92
- - spec/roulette/roulette_spec.rb
93
94
  - spec/roulette/transaction_spec.rb
95
+ - spec/roulette_spec.rb
94
96
  - spec/spec_helper.rb
95
97
  has_rdoc: true
96
98
  homepage: http://github.com/Schnems/roulette
@@ -106,7 +108,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
106
108
  requirements:
107
109
  - - ">="
108
110
  - !ruby/object:Gem::Version
109
- hash: 2757344494909302037
111
+ hash: -4581068164256372196
110
112
  segments:
111
113
  - 0
112
114
  version: "0"
@@ -124,6 +126,6 @@ signing_key:
124
126
  specification_version: 3
125
127
  summary: Pre-Sharding so simple you'll shoot yourself :p
126
128
  test_files:
127
- - spec/roulette/roulette_spec.rb
128
129
  - spec/roulette/transaction_spec.rb
130
+ - spec/roulette_spec.rb
129
131
  - spec/spec_helper.rb