module Brice::History::Tee

Public Class Methods

extended(base) click to toggle source
# File lib/brice/history.rb, line 126
def self.extended(base)
  class << base
    alias_method :push_without_tee,   :<<
    alias_method :push_m_without_tee, :push

    alias_method :<<,   :push_with_tee
    alias_method :push, :push_m_with_tee
  end
end

Public Instance Methods

push_m_with_tee(*args) click to toggle source
# File lib/brice/history.rb, line 153
def push_m_with_tee(*args)
  _tee_delete(*args)

  tee.concat(args)
  push_m_without_tee(*args)
end
push_with_tee(arg) click to toggle source
# File lib/brice/history.rb, line 146
def push_with_tee(arg)
  _tee_delete(arg)

  tee << arg
  push_without_tee(arg)
end
tee() click to toggle source
# File lib/brice/history.rb, line 136
def tee
  @tee ||= []
end
tee!() { |tee| ... } click to toggle source
# File lib/brice/history.rb, line 140
def tee!
  yield tee
ensure
  tee.clear
end

Private Instance Methods

_tee_delete(*args) click to toggle source
# File lib/brice/history.rb, line 162
def _tee_delete(*args)
  args.each { |arg| tee.delete(arg) }

  indexes = []

  each_with_index { |line, index|
    indexes << index if args.include?(line)
  }

  indexes.reverse_each { |index| delete_at(index) }
end