Skip to content

Instantly share code, notes, and snippets.

@ochaochaocha3
Last active May 5, 2016 01:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ochaochaocha3/d3f3c572447eb55e3b350969b91eed61 to your computer and use it in GitHub Desktop.
Save ochaochaocha3/d3f3c572447eb55e3b350969b91eed61 to your computer and use it in GitHub Desktop.
どどんとふのコマンドテーブルのプロトタイプ
require_relative 'command_table'
# クライアントからのコマンドを定義するクラス
class ClientCommand
include CommandTable
# 部屋の状態を取得するコマンド
define_command('getPlayRoomStates') do
{
minRoom: 0,
maxRoom: 10,
playRoomStates: []
}
end
end
# コマンドを管理するテーブルの機能を提供するモジュール
module CommandTable
# include されたときの処理
#
# コマンドを格納する Hash を初期化し、クラスメソッドを定義する。
# @param [Class] base include を行ったクラス
def self.included(base)
base.class_eval do
@commands ||= {}
def base.commands
@commands
end
extend(CommandTable)
end
end
# コマンドを定義する
# @param [String] name コマンド名
# @param [Proc] handler コマンド実行時の処理
# @return [String] コマンド名
def define_command(name, &handler)
commands[name] = handler
name
end
# コマンドの手続きオブジェクトを返す
# @param [String] name コマンド名
# @return [Proc]
# @raise KeyError 指定された名前のコマンドが定義されていなかったときに発生する
def fetch(name)
commands.fetch(name)
end
alias [] fetch
# コマンド名の配列を返す
# @return [Array]
def list
commands.keys
end
end
require_relative 'client_commad'
# 部屋の情報を取得する
response = ClientCommand['getPlayRoomStates'].call
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment