class BCDice::REPL
Read-Eval-Print Loopクラス
Attributes
commands[R]
debug[W]
@param value [Boolean] @return [Boolean]
Public Class Methods
new()
click to toggle source
# File lib/bcdice/repl.rb, line 9 def initialize() @game_system = GameSystem::DiceBot @hisoty_file = nil @debug = false end
Private Class Methods
command(*name, &block)
click to toggle source
# File lib/bcdice/repl.rb, line 111 def command(*name, &block) @commands ||= {} name.each do |key| @commands[key] = block end end
Public Instance Methods
history_file=(path)
click to toggle source
コマンド履歴を保存するファイルのパスを指定する
@param path [String] @return [void]
# File lib/bcdice/repl.rb, line 23 def history_file=(path) @hisoty_file = path if File.exist?(@hisoty_file) File.open(@hisoty_file) do |f| history = f.readlines.map(&:chomp) Readline::HISTORY.push(*history) end end end
load_game_system(game_system)
click to toggle source
ゲームシステムを切り替える @param game_system [Game]
# File lib/bcdice/repl.rb, line 36 def load_game_system(game_system) klass = BCDice.dynamic_load(game_system) if klass @game_system = klass else puts "#{game_system.inspect} not found" end end
Also aliased as: game_system=
quit()
click to toggle source
コマンドの履歴ファイルを保存してREPLを終了する @return [void]
# File lib/bcdice/repl.rb, line 49 def quit if @hisoty_file last_100_comamnds = Readline::HISTORY.to_a.reverse[0, 100].reverse File.write(@hisoty_file, last_100_comamnds.join("\n")) end exit end
run()
click to toggle source
REPLを開始する @return [void]
# File lib/bcdice/repl.rb, line 60 def run puts "BCDice REPL" puts '>> "help" shows help messages.' loop do run_once() end end
Private Instance Methods
eval_command(input)
click to toggle source
# File lib/bcdice/repl.rb, line 92 def eval_command(input) args = input.split(" ") if args.empty? return end command = args.shift block = REPL.commands[command] if block instance_exec(*args, &block) else eval_game_system(input) end end
eval_game_system(command)
click to toggle source
# File lib/bcdice/repl.rb, line 148 def eval_game_system(command) gs = @game_system.new(command) gs.enable_debug if @debug puts gs.eval()&.text || "<nil>" rescue StandardError => e puts e puts e.backtrace end
header()
click to toggle source
# File lib/bcdice/repl.rb, line 87 def header() debug_mode = "(Debug) " if @debug "[#{debug_mode}#{@game_system::ID}]> " end
run_once()
click to toggle source
# File lib/bcdice/repl.rb, line 70 def run_once input = Readline.readline(header())&.strip unless input puts return end if input.empty? return end Readline::HISTORY.push(input) eval_command(input) rescue Interrupt quit() end