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:) {|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.

Yields:

  • (context)

    Execution block.

Raises:

  • (ArgumentError)

    if no block is given.



116
117
118
119
120
121
# File 'lib/ollama_chat/command_concern.rb', line 116

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

Instance Attribute Details

#helpString (readonly)

Returns Help text for the command.

Returns:

  • (String)

    Help text for the command.



127
128
129
# File 'lib/ollama_chat/command_concern.rb', line 127

def help
  @help
end

#nameSymbol (readonly)

Returns The command name.

Returns:

  • (Symbol)

    The command name.



124
125
126
# File 'lib/ollama_chat/command_concern.rb', line 124

def name
  @name
end

#optionsString? (readonly)

Returns Options description.

Returns:

  • (String, nil)

    Options description.



130
131
132
# File 'lib/ollama_chat/command_concern.rb', line 130

def options
  @options
end

Instance Method Details

#argumentsArray<Array>

Returns Array of argument placeholders.

Returns:

  • (Array<Array>)

    Array of argument placeholders.



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

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

#command_namesArray<Symbol>

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

Returns:

  • (Array<Symbol>)

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



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

def command_names
  Array(@complete.first)
end

#completionsArray<Array>

Returns All possible completions for this command.

Returns:

  • (Array<Array>)

    All possible completions for this command.



169
170
171
172
173
174
175
# File 'lib/ollama_chat/command_concern.rb', line 169

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.



140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/ollama_chat/command_concern.rb', line 140

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.



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

def optional?
  !!@optional
end