class ConsoleUtils::ReplState

Constants

ALREADY_EXTENDED_ALERT
CONTEXT_DEBUG_MSG
EMPTY_CONTEXT_ALERT
IVAR
MODULE_EXTENDS_MSG

Public Class Methods

logger() click to toggle source
# File lib/console_utils/repl_state.rb, line 44
def self.logger
  ConsoleUtils.logger
end
new() click to toggle source
# File lib/console_utils/repl_state.rb, line 48
def initialize
  @version    = VERSION
  @extensions = []
  @persisted  = false
end
setup(context) click to toggle source
# File lib/console_utils/repl_state.rb, line 9
def self.setup(context)
  state = (context.instance_variable_defined?(IVAR) ? context.instance_variable_get(IVAR) : nil) || ReplState.new

  return true if state.frozen?

  logger.tagged("console_utils-#{VERSION}") do
    if context.nil?
      logger.warn { EMPTY_CONTEXT_ALERT }
      return
    end

    unless state.persisted?
      logger.level = Logger::WARN

      if ENV["CONSOLE_UTILS_DEBUG"]
        logger.level = Logger::DEBUG
        logger.debug { CONTEXT_DEBUG_MSG % context }
      end
    end

    if state.fully_extended?
      logger.warn { ALREADY_EXTENDED_ALERT }
    else
      ConsoleUtils.enabled_modules do |mod|
        state.extending(mod.to_s) do
          logger.debug { MODULE_EXTENDS_MSG }
          context.extend(mod)
        end
      end
    end
  end

  context.instance_variable_set(IVAR, state.persist!)
end

Public Instance Methods

extended_with?(mod)
Alias for: include?
extending(mod_name) { || ... } click to toggle source
# File lib/console_utils/repl_state.rb, line 67
def extending(mod_name)
  if include?(mod_name)
    true
  else
    ConsoleUtils.logger.tagged(mod_name) { yield }
    @extensions << mod_name
  end
end
fully_extended?() click to toggle source
# File lib/console_utils/repl_state.rb, line 63
def fully_extended?
  @persisted && @extensions.size == ConsoleUtils.enabled_modules.size
end
include?(mod) click to toggle source
# File lib/console_utils/repl_state.rb, line 76
def include?(mod)
  @extensions.include?(mod.to_s)
end
Also aliased as: extended_with?
persist!() click to toggle source
# File lib/console_utils/repl_state.rb, line 58
def persist!
  @persisted = true
  fully_extended? ? freeze : self
end
persisted?() click to toggle source
# File lib/console_utils/repl_state.rb, line 54
def persisted?
  @persisted
end