class GemOfThrones

Constants

VERSION

Public Class Methods

new(options) click to toggle source
# File lib/gem_of_thrones.rb, line 4
def initialize(options)
  @options = {
    :timeout => 10 * 60,
    :cache_key => "GemOfThrones."
  }.merge(options)

  raise "Set a :cache" unless options[:cache]
  raise "Only integers are supported for timeout" if options[:timeout].is_a?(Float)
end

Public Instance Methods

rise_to_power() click to toggle source
# File lib/gem_of_thrones.rb, line 14
def rise_to_power
  i_am_king! :try => !in_power?
end

Private Instance Methods

current_king() click to toggle source
# File lib/gem_of_thrones.rb, line 31
def current_king
  @options[:cache].read(@options[:cache_key])
end
i_am_king!(options) click to toggle source
# File lib/gem_of_thrones.rb, line 20
def i_am_king!(options)
  @options[:cache].write(@options[:cache_key], myself,
    :expires_in => @options[:timeout],
    :unless_exist => options[:try]
  )
end
in_power?() click to toggle source
# File lib/gem_of_thrones.rb, line 27
def in_power?
  current_king == myself
end
myself() click to toggle source
# File lib/gem_of_thrones.rb, line 35
def myself
  @myself ||= "#{Process.pid}-#{object_id}-#{Time.now.to_f}"
end