Sha256: cfa50741000a3fdcb62eb0ba0b77dea0cedd825d23b0b7706bb66d9c1f645b26

Contents?: true

Size: 1.49 KB

Versions: 2

Compression:

Stored size: 1.49 KB

Contents

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

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

module Archer
  autoload :History, "archer/history"

  mattr_accessor :limit
  self.limit = 200

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

  # TODO remove in 0.3.0
  mattr_accessor :history_file

  def self.clear
    quietly do
      Archer::History.where(user: user).delete_all
    end
    Readline::HISTORY.clear if defined?(Readline)
    Reline::HISTORY.clear if defined?(Reline)
    true
  end

  def self.start
    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.push(*commands)
    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!
    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
      yield
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
archer-rails-0.2.2 lib/archer.rb
archer-rails-0.2.1 lib/archer.rb