Class: ConsoleUtils::ReplState
- Inherits:
-
Object
- Object
- ConsoleUtils::ReplState
- Defined in:
- lib/console_utils/repl_state.rb
Constant Summary collapse
- IVAR =
:"@__console_utils__"
- EMPTY_CONTEXT_ALERT =
"Trying to setup with empty context".freeze
- ALREADY_EXTENDED_ALERT =
"Trying to setup again on fully extended context".freeze
- CONTEXT_DEBUG_MSG =
"Console instance: %p".freeze
- MODULE_EXTENDS_MSG =
"extending context...".freeze
Class Method Summary collapse
Instance Method Summary collapse
- #extending(mod_name) ⇒ Object
- #fully_extended? ⇒ Boolean
- #include?(mod) ⇒ Boolean (also: #extended_with?)
-
#initialize ⇒ ReplState
constructor
A new instance of ReplState.
- #persist! ⇒ Object
- #persisted? ⇒ Boolean
Constructor Details
Class Method Details
.logger ⇒ Object
44 45 46 |
# File 'lib/console_utils/repl_state.rb', line 44 def self.logger ConsoleUtils.logger end |
.setup(context) ⇒ Object
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# 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 |
Instance Method Details
#extending(mod_name) ⇒ Object
67 68 69 70 71 72 73 74 |
# 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? ⇒ Boolean
63 64 65 |
# File 'lib/console_utils/repl_state.rb', line 63 def fully_extended? @persisted && @extensions.size == ConsoleUtils.enabled_modules.size end |
#include?(mod) ⇒ Boolean Also known as: extended_with?
76 77 78 |
# File 'lib/console_utils/repl_state.rb', line 76 def include?(mod) @extensions.include?(mod.to_s) end |
#persist! ⇒ Object
58 59 60 61 |
# File 'lib/console_utils/repl_state.rb', line 58 def persist! @persisted = true fully_extended? ? freeze : self end |
#persisted? ⇒ Boolean
54 55 56 |
# File 'lib/console_utils/repl_state.rb', line 54 def persisted? @persisted end |