Class: OllamaChat::CommandConcern::Command
- Inherits:
-
Object
- Object
- OllamaChat::CommandConcern::Command
- 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
-
#help ⇒ String
readonly
Help text for the command.
-
#name ⇒ Symbol
readonly
The command name.
-
#options ⇒ String?
readonly
Options description.
Instance Method Summary collapse
-
#arguments ⇒ Array<Array>
Array of argument placeholders.
-
#command_names ⇒ Array<Symbol>
Array of command names (first element of @complete).
-
#completions ⇒ Array<Array>
All possible completions for this command.
-
#execute_if_match?(content) {|context| ... } ⇒ Boolean
Execute the command block if the content matches the regexp.
-
#initialize(name:, regexp:, complete: nil, optional: false, options: nil, help:) {|context| ... } ⇒ Command
constructor
Create a new Command instance.
-
#optional? ⇒ Boolean
True if the command is optional.
Constructor Details
#initialize(name:, regexp:, complete: nil, optional: false, options: nil, help:) {|context| ... } ⇒ Command
Create a new Command instance.
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, , help, block @complete = Array(complete || name.to_s).map { Array(_1) } end |
Instance Attribute Details
#help ⇒ String (readonly)
Returns Help text for the command.
127 128 129 |
# File 'lib/ollama_chat/command_concern.rb', line 127 def help @help end |
#name ⇒ Symbol (readonly)
Returns The command name.
124 125 126 |
# File 'lib/ollama_chat/command_concern.rb', line 124 def name @name end |
#options ⇒ String? (readonly)
Returns Options description.
130 131 132 |
# File 'lib/ollama_chat/command_concern.rb', line 130 def @options end |
Instance Method Details
#arguments ⇒ Array<Array>
Returns Array of argument placeholders.
164 165 166 |
# File 'lib/ollama_chat/command_concern.rb', line 164 def arguments Array(@complete[1..-1]) end |
#command_names ⇒ Array<Symbol>
Returns 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 |
#completions ⇒ Array<Array>
Returns 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.
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.
154 155 156 |
# File 'lib/ollama_chat/command_concern.rb', line 154 def optional? !!@optional end |