Class: Roulette
- Inherits:
-
Object
- Object
- Roulette
- Defined in:
- lib/roulette.rb
Defined Under Namespace
Classes: EachStore, Transaction
Instance Attribute Summary collapse
-
#stores ⇒ Object
Returns the value of attribute stores.
Instance Method Summary collapse
- #each ⇒ Object
- #extract_key(*args) ⇒ Object
-
#initialize(*stores) ⇒ Roulette
constructor
A new instance of Roulette.
- #method_missing(method_name, *args, &blk) ⇒ Object
- #select_store(*args) ⇒ Object
- #store_for_key(key) ⇒ Object
Constructor Details
#initialize(*stores) ⇒ Roulette
Returns a new instance of Roulette.
17 18 19 |
# File 'lib/roulette.rb', line 17 def initialize(*stores) self.stores = stores.flatten end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *args, &blk) ⇒ Object
21 22 23 |
# File 'lib/roulette.rb', line 21 def method_missing(method_name, *args, &blk) Transaction.new(:args => args, :store => select_store(args), :method => method_name).fire end |
Instance Attribute Details
#stores ⇒ Object
Returns the value of attribute stores.
15 16 17 |
# File 'lib/roulette.rb', line 15 def stores @stores end |
Instance Method Details
#extract_key(*args) ⇒ Object
25 26 27 |
# File 'lib/roulette.rb', line 25 def extract_key(*args) args.flatten.first.to_s end |
#select_store(*args) ⇒ Object
29 30 31 32 |
# File 'lib/roulette.rb', line 29 def select_store(*args) key = extract_key(*args) store_for_key(key) end |
#store_for_key(key) ⇒ Object
34 35 36 37 38 |
# File 'lib/roulette.rb', line 34 def store_for_key(key) val = Digest::SHA1.hexdigest(key).unpack('Q').join.to_i store_index = val % stores.count self.stores[store_index] end |