Class: BCDice::REPL

Inherits:
Object
  • Object
show all
Defined in:
lib/bcdice/repl.rb

Overview

Read-Eval-Print Loopクラス

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeREPL

Returns a new instance of REPL.



9
10
11
12
13
# File 'lib/bcdice/repl.rb', line 9

def initialize()
  @game_system = GameSystem::DiceBot
  @hisoty_file = nil
  @debug = false
end

Class Attribute Details

.commandsObject (readonly)

Returns the value of attribute commands.



109
110
111
# File 'lib/bcdice/repl.rb', line 109

def commands
  @commands
end

Instance Attribute Details

#debug=(value) ⇒ Boolean (writeonly)

Parameters:

  • value (Boolean)

Returns:

  • (Boolean)


17
18
19
# File 'lib/bcdice/repl.rb', line 17

def debug=(value)
  @debug = value
end

Class Method Details

.command(*name, &block) ⇒ Object



111
112
113
114
115
116
# File 'lib/bcdice/repl.rb', line 111

def command(*name, &block)
  @commands ||= {}
  name.each do |key|
    @commands[key] = block
  end
end

Instance Method Details

#history_file=(path) ⇒ void

This method returns an undefined value.

コマンド履歴を保存するファイルのパスを指定する

Parameters:

  • path (String)


23
24
25
26
27
28
29
30
31
32
# 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) ⇒ Object Also known as: game_system=

ゲームシステムを切り替える

Parameters:

  • game_system (Game)


36
37
38
39
40
41
42
43
# 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

#quitvoid

This method returns an undefined value.

コマンドの履歴ファイルを保存してREPLを終了する



49
50
51
52
53
54
55
56
# 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

#runvoid

This method returns an undefined value.

REPLを開始する



60
61
62
63
64
65
66
# File 'lib/bcdice/repl.rb', line 60

def run
  puts "BCDice REPL"
  puts '>> "help" shows help messages.'
  loop do
    run_once()
  end
end