Module: Rib::API
- Included in:
- Shell
- Defined in:
- lib/rib/api.rb
Class Method Summary collapse
-
.format_error(err) ⇒ Object
Format error raised in #loop_eval with #get_error.
-
.get_error(err) ⇒ Object
Get error message and backtrace from a particular error.
Instance Method Summary collapse
-
#after_loop ⇒ Object
Called after shell finishes looping.
-
#before_loop ⇒ Object
Called before shell starts looping.
-
#eval_binding ⇒ Object
The binding for evaluation.
-
#eval_input(input) ⇒ Object
Evaluate the input using #loop_eval and handle it.
- #format_backtrace(backtrace) ⇒ Object
-
#format_result(result) ⇒ Object
Format result using #result_prompt.
-
#get_input ⇒ Object
Get user input.
-
#handle_interrupt ⇒ Object
Handle interrupt (control-c).
-
#in_loop ⇒ Object
Main loop.
- #inspect_result(result) ⇒ Object
-
#line ⇒ Object
The line number for next evaluation.
-
#loop_eval(input) ⇒ Object
Evaluate user input with #eval_binding, name and line.
-
#loop_once ⇒ Object
Loop iteration: REPL.
-
#name ⇒ Object
The name of this shell.
-
#print_eval_error(err) ⇒ Object
Print evaluated error using #format_error.
-
#print_result(result) ⇒ Object
Print result using #format_result.
-
#prompt ⇒ Object
The prompt string of this shell.
- #puts(str = '') ⇒ Object
-
#result_prompt ⇒ Object
The result prompt string of this shell.
-
#started_at ⇒ Object
When the application loaded.
- #warn(message) ⇒ Object
Class Method Details
.format_error(err) ⇒ Object
Format error raised in #loop_eval with #get_error
121 122 123 124 |
# File 'lib/rib/api.rb', line 121 def format_error err , backtrace = get_error(err) "#{}\n #{backtrace.join("\n ")}" end |
.get_error(err) ⇒ Object
Get error message and backtrace from a particular error
128 129 130 |
# File 'lib/rib/api.rb', line 128 def get_error err ["#{err.class}: #{err.}", format_backtrace(err.backtrace)] end |
Instance Method Details
#after_loop ⇒ Object
Called after shell finishes looping
9 10 11 |
# File 'lib/rib/api.rb', line 9 def after_loop self end |
#before_loop ⇒ Object
Called before shell starts looping
4 5 6 |
# File 'lib/rib/api.rb', line 4 def before_loop self end |
#eval_binding ⇒ Object
The binding for evaluation
22 |
# File 'lib/rib/api.rb', line 22 def eval_binding ; config[:binding] ; end |
#eval_input(input) ⇒ Object
Evaluate the input using #loop_eval and handle it
67 68 69 70 71 72 73 74 75 |
# File 'lib/rib/api.rb', line 67 def eval_input input [loop_eval(input), nil] rescue SystemExit throw(:rib_exit, input) rescue Exception => e [nil, e] ensure config[:line] += 1 end |
#format_backtrace(backtrace) ⇒ Object
133 134 135 |
# File 'lib/rib/api.rb', line 133 def format_backtrace backtrace backtrace end |
#format_result(result) ⇒ Object
Format result using #result_prompt
109 110 111 |
# File 'lib/rib/api.rb', line 109 def format_result result "#{result_prompt}#{inspect_result(result)}" end |
#get_input ⇒ Object
Get user input. This is most likely overrided in Readline plugin
57 58 59 60 61 62 63 64 |
# File 'lib/rib/api.rb', line 57 def get_input print(prompt) if input = $stdin.gets input.chomp else nil end end |
#handle_interrupt ⇒ Object
Handle interrupt (control-c)
14 |
# File 'lib/rib/api.rb', line 14 def handle_interrupt; puts ; end |
#in_loop ⇒ Object
Main loop
29 30 31 32 |
# File 'lib/rib/api.rb', line 29 def in_loop input = catch(:rib_exit){ loop_once while running? } puts if input == nil && running? end |
#inspect_result(result) ⇒ Object
113 114 115 116 117 118 |
# File 'lib/rib/api.rb', line 113 def inspect_result result string = result.inspect warn("#{result.class}#inspect is not returning a string") unless string.kind_of?(String) string end |
#line ⇒ Object
The line number for next evaluation
24 |
# File 'lib/rib/api.rb', line 24 def line ; config[:line] ; end |
#loop_eval(input) ⇒ Object
Evaluate user input with #eval_binding, name and line
78 79 80 81 82 83 84 |
# File 'lib/rib/api.rb', line 78 def loop_eval input if eval_binding.kind_of?(Binding) eval_binding.eval(input, "(#{name})", line) else eval_binding.instance_eval(input, "(#{name})", line) end end |
#loop_once ⇒ Object
Loop iteration: REPL
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/rib/api.rb', line 35 def loop_once input, result, err = get_input, nil, nil throw(:rib_exit, input) if config[:exit].include?(input) result, err = eval_input(input) if err print_eval_error(err) elsif input.strip != '' && !equal_rib_skip(result) print_result(result) else # print nothing for blank input or Rib::Skip end flush_warnings [result, err] rescue Interrupt handle_interrupt end |
#name ⇒ Object
The name of this shell
20 |
# File 'lib/rib/api.rb', line 20 def name ; config[:name] ; end |
#print_eval_error(err) ⇒ Object
Print evaluated error using #format_error
98 99 100 101 102 |
# File 'lib/rib/api.rb', line 98 def print_eval_error err puts(format_error(err)) rescue StandardError, SyntaxError => e warn("Error while printing error:\n #{format_error(e)}") end |
#print_result(result) ⇒ Object
Print result using #format_result
91 92 93 94 95 |
# File 'lib/rib/api.rb', line 91 def print_result result puts(format_result(result)) rescue StandardError, SyntaxError => e warn("Error while printing result:\n #{format_error(e)}") end |
#prompt ⇒ Object
The prompt string of this shell
16 |
# File 'lib/rib/api.rb', line 16 def prompt ; config[:prompt] ; end |
#puts(str = '') ⇒ Object
86 87 88 |
# File 'lib/rib/api.rb', line 86 def puts str='' super end |
#result_prompt ⇒ Object
The result prompt string of this shell
18 |
# File 'lib/rib/api.rb', line 18 def result_prompt; config[:result_prompt] ; end |
#started_at ⇒ Object
When the application loaded
26 |
# File 'lib/rib/api.rb', line 26 def started_at ; config[:started_at] ; end |
#warn(message) ⇒ Object
104 105 106 |
# File 'lib/rib/api.rb', line 104 def warn warnings << end |