Skip to content

Instantly share code, notes, and snippets.

@colindean
Created November 5, 2011 00:24
Show Gist options
  • Save colindean/1340859 to your computer and use it in GitHub Desktop.
Save colindean/1340859 to your computer and use it in GitHub Desktop.
stack overflowing on method_missing call
=begin
This class exists primarily to act as a bridge between a tournament and its
remote API interface, if one exists.
=end
class RemoteTournament < ActiveRecord::Base
belongs_to :tournament, :polymorphic => true
validates :remote_type, :presence => :true
validates :remote_id, :presence => :true
#tournament, #remote_type, #remote_id
def type=(c)
raise Error if c.class != Class
remote_type = c.to_s
end
def type
resolve_class
end
def resolve_class
remote_type.to_s.classify.constantize
end
def get
#I'm not sure I want this here...
if remote_type == 'Challonge::Tournament'
challonge_init
end
resolve_class.find(remote_id)
end
def set(remote_instance)
self.type = remote_instance.class
self.remote_id = remote_instance.id
get
end
private
def challonge_init
u = Pcfg.get('challonge.api.username')
k = Pcfg.get('challonge.api.key')
msg = _("The Challonge API credentials are not set.")
raise PLAS::Exceptions::RemoteTournamentError, msg if !(u and k)
Challonge::API.username = u
Challonge::API.key = k
end
end
class Tournament < ActiveRecord::Base
belongs_to :event
has_and_belongs_to_many :participants, :class_name => "User"
has_one :remote_tournament, :dependent => :destroy
validates :name, :presence => true, :uniqueness => true, :length => {:minimum => 2 }
validates :max_participants, :numericality => { :greater_than => 0 }
validates :event, :presence => true
#name, description, max_participants,
#started_at, start_time,
#remote_tournament, event
def remote?
remote_tournament != nil
end
def started?
started_at != nil
end
def full?
participants.size <= max_participants
end
def remaining
max_participants - participants.size
end
def remote
@remote ||= remote_tournament.get
end
def method_missing(method, *args, &block)
if pass_method_to_remote?(method)
return remote.call(method, *args, &block)
end
super(method, *args, &block)
end
def respond_to?(method)
pass_method_to_remote?(method) || super(method)
end
private
def pass_method_to_remote?(method)
remote? and remote.respond_to? method
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment