Class: Ukiryu::Shell::Registry

Inherits:
Object
  • Object
show all
Defined in:
lib/ukiryu/shell.rb,
lib/ukiryu/shell/registry.rb

Overview

Registry for shell implementations

Provides a self-registration pattern for shells, making it easy to add new shells without modifying the core Shell module.

Usage

# Define a new shell class Ukiryu::Shell::Nushell < Base

PLATFORM = :unix
SHELL_NAME = :nushell
EXECUTABLE = 'nu'

end

# Auto-registers with the registry Shell::Registry.register(Nushell)

# Now you can use it Shell.class_for(:nushell) # => Nushell

Class Method Summary collapse

Class Method Details

.all_shellsArray<Class<Base>>

Get all registered shells

Returns:

  • (Array<Class<Base>>)

    all registered shell classes



106
107
108
# File 'lib/ukiryu/shell/registry.rb', line 106

def all_shells
  @registry.values.flatten.uniq
end

.clearObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Clear all registered shells (mainly for testing)



95
96
97
# File 'lib/ukiryu/shell.rb', line 95

def clear
  @shells = nil
end

.default_for_platform(platform) ⇒ Class<Base>?

Get the default shell for a platform

Parameters:

  • platform (Symbol)

    the platform

Returns:

  • (Class<Base>, nil)

    the default shell class



72
73
74
75
# File 'lib/ukiryu/shell/registry.rb', line 72

def default_for_platform(platform)
  shells = for_platform(platform)
  shells.first
end

.for_name(name) ⇒ Class<Base>

Get a shell class by name

Parameters:

  • name (Symbol)

    the shell name

Returns:

  • (Class<Base>)

    the shell class

Raises:

  • (UnknownShellError)

    if shell is not registered



54
55
56
# File 'lib/ukiryu/shell/registry.rb', line 54

def for_name(name)
  @registry[name.to_sym] || raise(Errors::UnknownShellError, "Unknown shell: #{name}")
end

.for_platform(platform) ⇒ Array<Class<Base>>

Get all shells for a platform

Parameters:

  • platform (Symbol)

    the platform (:unix, :windows, :powershell)

Returns:

  • (Array<Class<Base>>)

    shell classes for the platform



63
64
65
# File 'lib/ukiryu/shell/registry.rb', line 63

def for_platform(platform)
  @registry[platform] || []
end

.lookup(name) ⇒ Class?

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Lookup a shell class by name

Parameters:

  • name (Symbol)

    the shell name

Returns:

  • (Class, nil)

    the shell class or nil if not registered



83
84
85
# File 'lib/ukiryu/shell.rb', line 83

def lookup(name)
  shells[name.to_sym]
end

.platform_namesArray<Symbol>

Get all registered platform names

Returns:

  • (Array<Symbol>)

    platform names



98
99
100
# File 'lib/ukiryu/shell/registry.rb', line 98

def platform_names
  @registry.keys.select { |k| %i[unix windows powershell].include?(k) }
end

.register(shell_class) ⇒ true

Register a shell class

Parameters:

  • shell_class (Class<Base>)

    the shell class to register

Returns:

  • (true)


73
74
75
76
77
# File 'lib/ukiryu/shell.rb', line 73

def register(name, shell_class)
  raise ArgumentError, 'Shell class must inherit from Ukiryu::Shell::Base' unless shell_class.ancestors.include?(Base)

  shells[name.to_sym] = shell_class
end

.register_builtin_shellsObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Register all built-in shells

This is called automatically when the module is loaded.



124
125
126
127
128
129
130
131
132
133
# File 'lib/ukiryu/shell/registry.rb', line 124

def register_builtin_shells
  register(Bash)
  register(Zsh)
  register(Fish)
  register(Sh)
  register(Dash)
  register(Tcsh)
  register(PowerShell)
  register(Cmd)
end

.registered?(name) ⇒ Boolean

Check if a shell is registered

Parameters:

  • name (Symbol)

    the shell name

Returns:

  • (Boolean)


82
83
84
# File 'lib/ukiryu/shell/registry.rb', line 82

def registered?(name)
  @registry.key?(name.to_sym)
end

.registered_shellsArray<Symbol>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Get all registered shell names

Returns:

  • (Array<Symbol>)

    list of registered shell names



90
91
92
# File 'lib/ukiryu/shell.rb', line 90

def registered_shells
  shells.keys
end

.resetObject

Reset the registry (for testing)



112
113
114
115
116
# File 'lib/ukiryu/shell/registry.rb', line 112

def reset
  @mutex.synchronize do
    @registry = {}
  end
end

.shell_namesArray<Symbol>

Get all registered shell names

Returns:

  • (Array<Symbol>)

    shell names



90
91
92
# File 'lib/ukiryu/shell/registry.rb', line 90

def shell_names
  @registry.keys.select { |k| k.is_a?(Symbol) && !%i[unix windows powershell].include?(k) }
end

.shellsObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



64
65
66
# File 'lib/ukiryu/shell.rb', line 64

def shells
  @shells ||= {}
end