Last active
May 5, 2016 01:39
-
-
Save ochaochaocha3/d3f3c572447eb55e3b350969b91eed61 to your computer and use it in GitHub Desktop.
どどんとふのコマンドテーブルのプロトタイプ
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require_relative 'command_table' | |
# クライアントからのコマンドを定義するクラス | |
class ClientCommand | |
include CommandTable | |
# 部屋の状態を取得するコマンド | |
define_command('getPlayRoomStates') do | |
{ | |
minRoom: 0, | |
maxRoom: 10, | |
playRoomStates: [] | |
} | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# コマンドを管理するテーブルの機能を提供するモジュール | |
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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