Class: RuVim::ExCommandRegistry
- Inherits:
-
Object
- Object
- RuVim::ExCommandRegistry
- Includes:
- Singleton
- Defined in:
- lib/ruvim/ex_command_registry.rb
Defined Under Namespace
Classes: ExCommandSpec
Instance Method Summary collapse
- #all ⇒ Object
- #clear! ⇒ Object
- #fetch(name) ⇒ Object
-
#initialize ⇒ ExCommandRegistry
constructor
A new instance of ExCommandRegistry.
- #register(name, call:, aliases: [], desc: "", nargs: :any, bang: false, raw_args: false, source: :builtin) ⇒ Object
- #registered?(name) ⇒ Boolean
- #resolve(name) ⇒ Object
- #unregister(name) ⇒ Object
Constructor Details
#initialize ⇒ ExCommandRegistry
Returns a new instance of ExCommandRegistry.
19 20 21 22 |
# File 'lib/ruvim/ex_command_registry.rb', line 19 def initialize @specs = {} @lookup = {} end |
Instance Method Details
#all ⇒ Object
62 63 64 |
# File 'lib/ruvim/ex_command_registry.rb', line 62 def all @specs.values.sort_by(&:name) end |
#clear! ⇒ Object
79 80 81 82 |
# File 'lib/ruvim/ex_command_registry.rb', line 79 def clear! @specs.clear @lookup.clear end |
#fetch(name) ⇒ Object
58 59 60 |
# File 'lib/ruvim/ex_command_registry.rb', line 58 def fetch(name) resolve(name) || raise(RuVim::CommandError, "Not an editor command: #{name}") end |
#register(name, call:, aliases: [], desc: "", nargs: :any, bang: false, raw_args: false, source: :builtin) ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/ruvim/ex_command_registry.rb', line 24 def register(name, call:, aliases: [], desc: "", nargs: :any, bang: false, raw_args: false, source: :builtin) canonical = name.to_s if @specs.key?(canonical) raise RuVim::CommandError, "Ex command already exists: #{canonical}" end spec = ExCommandSpec.new( name: canonical, call: call, aliases: aliases.map(&:to_s), desc: desc, nargs: nargs, bang: bang, raw_args: raw_args, source: source ) names = [canonical, *spec.aliases] names.each do |n| existing = @lookup[n] if existing && existing != canonical raise RuVim::CommandError, "Ex command name collision: #{n}" end end @specs[canonical] = spec names.each { |n| @lookup[n] = canonical } spec end |
#registered?(name) ⇒ Boolean
66 67 68 |
# File 'lib/ruvim/ex_command_registry.rb', line 66 def registered?(name) !!resolve(name) end |
#resolve(name) ⇒ Object
53 54 55 56 |
# File 'lib/ruvim/ex_command_registry.rb', line 53 def resolve(name) canonical = @lookup[name.to_s] canonical && @specs[canonical] end |
#unregister(name) ⇒ Object
70 71 72 73 74 75 76 77 |
# File 'lib/ruvim/ex_command_registry.rb', line 70 def unregister(name) spec = resolve(name.to_s) return nil unless spec @specs.delete(spec.name) @lookup.delete_if { |_k, v| v == spec.name } spec end |