Sha256: 2b407764bf917b1aa27214cd70ade7a823d05f35fffee5a77b4f0a80c4f3cb1e

Contents?: true

Size: 1.71 KB

Versions: 2

Compression:

Stored size: 1.71 KB

Contents

# dependencies
require "active_support/core_ext/module/attribute_accessors"

# modules
require_relative "archer/engine" if defined?(Rails)
require_relative "archer/irb"
require_relative "archer/version"

module Archer
  autoload :History, "archer/history"

  mattr_accessor :limit
  self.limit = 200

  mattr_accessor :user
  self.user = ENV["USER"]

  mattr_accessor :save_session
  self.save_session = true

  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

    def save
      return false unless history_object

      quietly do
        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] Unable to save history"
      false
    end

    def clear
      quietly do
        Archer::History.where(user: user).delete_all
      end
      history_object.clear if history_object
      true
    end

    private

    def history_object
      cls = IRB.CurrentContext&.io&.class
      cls && cls.const_defined?(:HISTORY) ? cls::HISTORY : nil
    end

    def quietly
      ActiveRecord::Base.logger.silence do
        yield
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
archer-rails-0.4.1 lib/archer.rb
archer-rails-0.4.0 lib/archer.rb