mirror of
https://github.com/pry/pry.git
synced 2022-11-09 12:35:05 -05:00
09ebd358e1
This change is aimed to simplify #1843 (Rework the Pry config). Current command state implementation gets in the way. We would like to simplify the Config class. The current implementation penetrates Pry codebase everywhere, and during my rework of the config I discovered that `watch` uses global command state. It means the state should survive `Pry.new` calls. With my (unpublished yet) implementation, the `watch` command fails to do so. I realised that we can refactor command state implementation to be global. It makes sense to me and also helps with the Config refactoring. With help of a dedicated class we can easily manage the command state (resetting).
29 lines
620 B
Ruby
29 lines
620 B
Ruby
require 'ostruct'
|
|
|
|
class Pry
|
|
# CommandState is a data structure to hold per-command state.
|
|
#
|
|
# Pry commands can store arbitrary state here. This state persists between
|
|
# subsequent command invocations. All state saved here is unique to the
|
|
# command.
|
|
#
|
|
# @since ?.?.?
|
|
# @api private
|
|
class CommandState
|
|
def self.default
|
|
@default ||= new
|
|
end
|
|
|
|
def initialize
|
|
@command_state = {}
|
|
end
|
|
|
|
def state_for(command_name)
|
|
@command_state[command_name] ||= OpenStruct.new
|
|
end
|
|
|
|
def reset(command_name)
|
|
@command_state[command_name] = OpenStruct.new
|
|
end
|
|
end
|
|
end
|