Class: OllamaChat::CommandConcern::Command
- Inherits:
-
Object
- Object
- OllamaChat::CommandConcern::Command
- Defined in:
- lib/ollama_chat/command_concern.rb
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.
98 99 100 101 102 103 |
# File 'lib/ollama_chat/command_concern.rb', line 98 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.
109 110 111 |
# File 'lib/ollama_chat/command_concern.rb', line 109 def help @help end |
#name ⇒ Symbol (readonly)
Returns The command name.
106 107 108 |
# File 'lib/ollama_chat/command_concern.rb', line 106 def name @name end |
#options ⇒ String? (readonly)
Returns Options description.
112 113 114 |
# File 'lib/ollama_chat/command_concern.rb', line 112 def @options end |
Instance Method Details
#arguments ⇒ Array<Array>
Returns Array of argument placeholders.
146 147 148 |
# File 'lib/ollama_chat/command_concern.rb', line 146 def arguments Array(@complete[1..-1]) end |
#command_names ⇒ Array<Symbol>
Returns Array of command names (first element of @complete).
141 142 143 |
# File 'lib/ollama_chat/command_concern.rb', line 141 def command_names Array(@complete.first) end |
#completions ⇒ Array<Array>
Returns All possible completions for this command.
151 152 153 154 155 156 157 |
# File 'lib/ollama_chat/command_concern.rb', line 151 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.
122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/ollama_chat/command_concern.rb', line 122 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.
136 137 138 |
# File 'lib/ollama_chat/command_concern.rb', line 136 def optional? !!@optional end |