lib/archer.rb in archer-rails-0.3.0 vs lib/archer.rb in archer-rails-0.4.0

- old
+ new

@@ -1,12 +1,12 @@ # dependencies require "active_support/core_ext/module/attribute_accessors" # modules -require "archer/engine" if defined?(Rails) -require "archer/irb" -require "archer/version" +require_relative "archer/engine" if defined?(Rails) +require_relative "archer/irb" +require_relative "archer/version" module Archer autoload :History, "archer/history" mattr_accessor :limit @@ -16,60 +16,65 @@ self.user = ENV["USER"] mattr_accessor :save_session self.save_session = true - def self.clear - quietly do - Archer::History.where(user: user).delete_all + class << self + def start + if !history_object + warn "[archer] Skipping history" + return + end + + history = nil + begin + quietly do + history = Archer::History.find_by(user: user) + end + rescue ActiveRecord::StatementInvalid + warn "[archer] Create table to enable history" + end + + if history + commands = history.commands.split("\n") + history_object.clear + history_object.push(*commands) + end + + IRB.conf[:AT_EXIT].push(proc { Archer.save if Archer.save_session }) end - Readline::HISTORY.clear if defined?(Readline) - Reline::HISTORY.clear if defined?(Reline) - true - end - def self.start - history = nil - begin + def save + return false unless history_object + quietly do - history = Archer::History.find_by(user: user) + history = Archer::History.where(user: user).first_or_initialize + history.commands = history_object.to_a.last(limit).join("\n") + history.save! end rescue ActiveRecord::StatementInvalid - warn "[archer] Create table to enable history" + warn "[archer] Unable to save history" + false end - if history - commands = history.commands.split("\n") - history_object.push(*commands) + def clear + quietly do + Archer::History.where(user: user).delete_all + end + history_object.clear if history_object + true end - end - def self.save - quietly do - history = Archer::History.where(user: user).first_or_initialize - history.commands = history_object.to_a.last(limit).join("\n") - history.save! + private + + def history_object + cls = IRB.CurrentContext&.io&.class + cls && cls.const_defined?(:HISTORY) ? cls::HISTORY : nil end - rescue ActiveRecord::StatementInvalid - warn "[archer] Unable to save history" - end - # private - # TODO use IRB.CurrentContext.io.class::HISTORY - 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 - yield + def quietly + ActiveRecord::Base.logger.silence do + yield + end end end end