Class: Roulette

Inherits:
Object
  • Object
show all
Defined in:
lib/roulette.rb

Defined Under Namespace

Classes: EachStore, Transaction

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#storesObject

Returns the value of attribute stores.



15
16
17
# File 'lib/roulette.rb', line 15

def stores
  @stores
end

Instance Method Details

#eachObject



40
41
42
# File 'lib/roulette.rb', line 40

def each
  EachStore.new(stores)
end

#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