Class: OllamaChat::CommandConcern::Command

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

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.



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, 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.



109
110
111
# File 'lib/ollama_chat/command_concern.rb', line 109

def help
  @help
end

#nameSymbol (readonly)

Returns The command name.

Returns:

  • (Symbol)

    The command name.



106
107
108
# File 'lib/ollama_chat/command_concern.rb', line 106

def name
  @name
end

#optionsString? (readonly)

Returns Options description.

Returns:

  • (String, nil)

    Options description.



112
113
114
# File 'lib/ollama_chat/command_concern.rb', line 112

def options
  @options
end

Instance Method Details

#argumentsArray<Array>

Returns Array of argument placeholders.

Returns:

  • (Array<Array>)

    Array of argument placeholders.



146
147
148
# File 'lib/ollama_chat/command_concern.rb', line 146

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



141
142
143
# File 'lib/ollama_chat/command_concern.rb', line 141

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.



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.

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.



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.

Returns:

  • (Boolean)

    true if the command is optional.



136
137
138
# File 'lib/ollama_chat/command_concern.rb', line 136

def optional?
  !!@optional
end