Skip to content

Instantly share code, notes, and snippets.

@reactormonk
Created January 28, 2010 15:47
Show Gist options
  • Save reactormonk/288847 to your computer and use it in GitHub Desktop.
Save reactormonk/288847 to your computer and use it in GitHub Desktop.
module Presenters
class Game
include Presenter
def initialize(game)
@game = game
self
end
def prepare
generate_messages
end
def generate_messages
unless @player
@join_message = @leave_message = nil
@create_message = "Erstelle zuerst einen Player <hier>."
end
end
def default
@template = "games/presenters/#{template_for_state(@game.state)}"
self
end
def widget
@template = "games/presenters/widget/#{template_for_state(@game.state)}"
self
end
private
def template_for_state(state)
templates = Hash.new {|h,k| k }
templates.merge({
"scourge_won" => "finished",
"sentinel_won" => "finished",
})
templates[state]
end
end
end
require "rango/mixins/rendering"
module Presenters
module Mixin
def present(type=:default)
Presenters.present(self, type)
end
end
def present(model, type)
const_get(model.class.to_s).new(model).send(type)
end
module_function :present
end
module Presenter
include Rango::ImplicitRendering
include Rango::Helpers
def to_s
prepare if respond_to? :prepare
render @template
end
def with(hash)
hash.each {|k,v| instance_variable_set("@#{k}", v) }
self
end
end
%w(game).each {|file| require_relative("presenters/" + file)}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment