Module: EasyRepl::Repl

Included in:
EasyRepl
Defined in:
lib/easy_repl/repl.rb

Instance Method Summary collapse

Instance Method Details

#commandsObject



61
62
63
# File 'lib/easy_repl/repl.rb', line 61

def commands
  @commands ||= [EasyRepl::Commands::Exit, EasyRepl::Commands::Reload]
end

#getsObject



50
51
52
53
54
55
56
57
58
59
# File 'lib/easy_repl/repl.rb', line 50

def gets
  input = prompt.gets
  command = commands.find {|c| c.matches(input)}

  if command
    return command.run(input)
  else
    return input
  end
end

#startObject



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/easy_repl/repl.rb', line 18

def start
  IRB::HistorySavingAbility.extend(IRB::HistorySavingAbility) unless IRB::HistorySavingAbility === IRB::HistorySavingAbility
  loop do #outer loop
    setup if respond_to? :setup
    begin
      exit_inner_loop_value = catch(:exit_inner_loop) do
        loop do #inner loop
          begin
            catch(:skip_process_input) do
              before_input if respond_to? :before_input
              if block_given?
                yield self.gets
              elsif respond_to? :process_input
                process_input(self.gets)
              else
                puts self.gets
              end
            end
          ensure
            after_input if respond_to? :after_input
          end
        end
      end
      return if exit_inner_loop_value == :exit
    ensure
      teardown if respond_to? :teardown
    end
  end
ensure
  IRB::HistorySavingAbility.save_history
end