lib/rib/api.rb in rib-1.5.1 vs lib/rib/api.rb in rib-1.5.2
- old
+ new
@@ -1,8 +1,7 @@
-module Rib; end
-module Rib::API
+module Rib; module API
# Called before shell starts looping
def before_loop
self
end
@@ -21,29 +20,36 @@
def name ; config[:name] ; end
# The binding for evaluation
def eval_binding ; config[:binding] ; end
# The line number for next evaluation
def line ; config[:line] ; end
+ # When the application loaded
+ def started_at ; config[:started_at] ; end
# Main loop
def in_loop
- input = catch(:rib_exit){ loop_once while true }
- puts if input == nil
+ input = catch(:rib_exit){ loop_once while running? }
+ puts if input == nil && running?
end
# Loop iteration: REPL
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
@@ -79,29 +85,40 @@
# Print result using #format_result
def print_result result
puts(format_result(result))
rescue StandardError, SyntaxError => e
- Rib.warn("Error while printing result:\n #{format_error(e)}")
+ warn("Error while printing result:\n #{format_error(e)}")
end
# Print evaluated error using #format_error
def print_eval_error err
puts(format_error(err))
rescue StandardError, SyntaxError => e
- Rib.warn("Error while printing error:\n #{format_error(e)}")
+ warn("Error while printing error:\n #{format_error(e)}")
end
def puts str
super
end
+ def warn message
+ warnings << message
+ end
+
# Format result using #result_prompt
def format_result result
- result_prompt + result.inspect
+ "#{result_prompt}#{inspect_result(result)}"
end
+ def inspect_result result
+ string = result.inspect
+ warn("#{result.class}#inspect is not returning a string") unless
+ string.kind_of?(String)
+ string
+ end
+
# Format error raised in #loop_eval with #get_error
def format_error err
message, backtrace = get_error(err)
"#{message}\n #{backtrace.join("\n ")}"
end
@@ -121,6 +138,10 @@
def equal_rib_skip result
result == Rib::Skip
rescue
# do nothing, it cannot respond to == correctly, it can't be Rib::Skip
end
-end
+
+ def flush_warnings
+ Rib.warn(warnings.shift) until warnings.empty?
+ end
+end; end