class GameOfThrones

Constants

VERSION

Public Class Methods

new(options) click to toggle source
# File lib/game_of_thrones.rb, line 4
def initialize(options)
  @options = {
    :timeout => 10 * 60,
    :cache_key => "GameOfThrones.",
    :cache => "You have to set :cache",
    :mutex_timeout => 0.1
  }.merge(options)
end

Public Instance Methods

rise_to_power() click to toggle source
# File lib/game_of_thrones.rb, line 13
def rise_to_power
  if no_king? or in_power?
    take_power
    sleep mutex_timeout # multiple people could try to take power simultaneously
    in_power?
  else
    false
  end
end

Private Instance Methods

current_king() click to toggle source
# File lib/game_of_thrones.rb, line 41
def current_king
  @options[:cache].read(@options[:cache_key])
end
in_power?() click to toggle source
# File lib/game_of_thrones.rb, line 29
def in_power?
  current_king == myself
end
mutex_timeout() click to toggle source
# File lib/game_of_thrones.rb, line 33
def mutex_timeout
  @options[:mutex_timeout] + (@options[:mutex_timeout] * rand)
end
myself() click to toggle source
# File lib/game_of_thrones.rb, line 45
def myself
  @myself ||= "#{Process.pid}-#{object_id}-#{Time.now.to_f}"
end
no_king?() click to toggle source
# File lib/game_of_thrones.rb, line 25
def no_king?
  not current_king
end
take_power() click to toggle source
# File lib/game_of_thrones.rb, line 37
def take_power
  @options[:cache].write(@options[:cache_key], myself, :expires_in => @options[:timeout])
end