Class: OllamaChat::CommandConcern::Command

Inherits:
Object
  • Object
show all
Defined in:
lib/ollama_chat/command_concern.rb

Overview

Represents a registered command in the OllamaChat command DSL.

A Command instance stores

  • the command name(s) (@complete.first),
  • the matching regular expression (@regexp),
  • optional completion hints (@complete[1..]),
  • help text (@help), and
  • the block that is executed when the command matches.

It also exposes helpers for optionality (optional?), command names (command_names), arguments (arguments), and completions (completions).

The execute_if_match? method performs a regexp match and, if successful, yields the execution context to the stored block.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name:, regexp:, complete: nil, optional: false, options: nil, help:, category:) {|context| ... } ⇒ Command

Create a new Command instance.

Parameters:

  • name (Symbol) —

    The command name.

  • regexp (Regexp, nil) —

    Regular expression for matching.

  • complete (Array, nil) (defaults to: nil) —

    Completion hints.

  • optional (Boolean) (defaults to: false) —

    Whether the command is optional.

  • options (String, nil) (defaults to: nil) —

    Options description.

  • help (String) —

    Help text.

  • category (Symbol) —

    Category for grouping in help output.

Yields:

  • (context) —

    Execution block.

Raises:

  • (ArgumentError) —

    if no block is given.



152
153
154
155
156
157
# File 'lib/ollama_chat/command_concern.rb', line 152

def initialize(name:, regexp:, complete: nil, optional: false, options: nil, help:, category:, &block)
  block or raise ArgumentError, 'require &block'
  @name, @regexp, @optional, @options, @help, @category, @block =
    name, regexp, optional, options, help, category, block
  @complete = Array(complete || name.to_s).map { Array(_1) }
end

Instance Attribute Details

#category ⇒ Symbol (readonly)

Returns Category for grouping in help output.

Returns:

  • (Symbol) —

    Category for grouping in help output.



166
167
168
# File 'lib/ollama_chat/command_concern.rb', line 166

def category
  @category
end

#help ⇒ String (readonly)

Returns Help text for the command.

Returns:

  • (String) —

    Help text for the command.



163
164
165
# File 'lib/ollama_chat/command_concern.rb', line 163

def help
  @help
end

#name ⇒ Symbol (readonly)

Returns The command name.

Returns:

  • (Symbol) —

    The command name.



160
161
162
# File 'lib/ollama_chat/command_concern.rb', line 160

def name
  @name
end

#options ⇒ String? (readonly)

Returns Options description.

Returns:

  • (String, nil) —

    Options description.



169
170
171
# File 'lib/ollama_chat/command_concern.rb', line 169

def options
  @options
end

Instance Method Details

#arguments ⇒ Array<Array>

Returns Array of argument placeholders.

Returns:

  • (Array<Array>) —

    Array of argument placeholders.



203
204
205
# File 'lib/ollama_chat/command_concern.rb', line 203

def arguments
  Array(@complete[1..-1])
end

#command_names ⇒ Array<Symbol>

Returns Array of command names (first element of @complete).

Returns:

  • (Array<Symbol>) —

    Array of command names (first element of @complete).



198
199
200
# File 'lib/ollama_chat/command_concern.rb', line 198

def command_names
  Array(@complete.first)
end

#completions ⇒ Array<Array>

Returns All possible completions for this command.

Returns:

  • (Array<Array>) —

    All possible completions for this command.



208
209
210
211
212
213
214
# File 'lib/ollama_chat/command_concern.rb', line 208

def completions
  result = @complete&.first&.map { ?/ + _1 }&.product(*arguments)
  if result && optional?
    result += @complete&.first.map { [ ?/ + _1 ] }
  end
  result
end

#execute_if_match?(content) {|context| ... } ⇒ Boolean

Execute the command block if the content matches the regexp.

Parameters:

  • content (String, nil) —

    The content to match against.

Yields:

  • (context) —

    Context binding for execution.

Returns:

  • (Boolean) —

    true if the command was executed, false otherwise.

Raises:

  • (ArgumentError) —

    if no context block is provided.



179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/ollama_chat/command_concern.rb', line 179

def execute_if_match?(content, &context)
  context or raise ArgumentError, 'need &context block'
  # We invoke thee, Black Dragon of Eval, we invoke thee, O mighty force of
  # `instance_exec`, awake now from your aeonic slumber – rise from the
  # abyss!
  if @regexp.nil? && content.nil?
    context.binding.eval('self').instance_exec(&@block)
  else
    content =~ @regexp or return
    context.binding.eval('self').instance_exec(*$~.captures, &@block)
  end
end

#optional? ⇒ Boolean

Returns true if the command is optional.

Returns:

  • (Boolean) —

    true if the command is optional.



193
194
195
# File 'lib/ollama_chat/command_concern.rb', line 193

def optional?
  !!@optional
end