lib/archer.rb in archer-rails-0.1.1 vs lib/archer.rb in archer-rails-0.2.0

- old
+ new

@@ -18,11 +18,12 @@ def self.clear quietly do Archer::History.where(user: user).delete_all end - Readline::HISTORY.clear + Readline::HISTORY.clear if defined?(Readline) + Reline::HISTORY.clear if defined?(Reline) true end def self.start history = nil @@ -33,21 +34,36 @@ rescue ActiveRecord::StatementInvalid warn "[archer] Create table to enable history" end if history - Readline::HISTORY.push(*history.commands.split("\n")) + commands = history.commands.split("\n") + # can't use reline? yet, so push to all + Readline::HISTORY.push(*commands) if defined?(Readline) + Reline::HISTORY.push(*commands) if defined?(Reline) end end def self.save quietly do history = Archer::History.where(user: user).first_or_initialize - history.commands = Readline::HISTORY.to_a.last(limit).join("\n") - history.save + history.commands = history_object.to_a.last(limit).join("\n") + history.save! end rescue ActiveRecord::StatementInvalid warn "[archer] Unable to save history" + end + + # private + def self.history_object + reline? ? Reline::HISTORY : Readline::HISTORY + end + + # private + def self.reline? + IRB.CurrentContext.io.is_a?(IRB::ReidlineInputMethod) + rescue + false end # private def self.quietly ActiveRecord::Base.logger.silence do